/** * Class model for Collection Interface */ import type { CollectionContentTypes, CollectionInterface, CollectionPropertiesInterface } from "@/types/collection"; export class CollectionObject implements CollectionInterface { _data!: CollectionInterface; constructor() { this._data = { provider: '', service: '', collection: null, identifier: '', signature: null, created: null, modified: null, properties: new CollectionPropertiesObject(), }; } fromJson(data: CollectionInterface): CollectionObject { this._data = data; if (data.properties) { this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface); } return this; } toJson(): CollectionInterface { const json = { ...this._data }; if (this._data.properties instanceof CollectionPropertiesObject) { json.properties = this._data.properties.toJson(); } return json; } clone(): CollectionObject { const cloned = new CollectionObject(); cloned._data = { ...this._data }; cloned._data.properties = this.properties.clone(); return cloned; } /** Immutable Properties */ get provider(): string { return this._data.provider; } get service(): string | number { return this._data.service; } get collection(): string | number | null { return this._data.collection; } get identifier(): string | number { return this._data.identifier; } get signature(): string | null | undefined { return this._data.signature; } get created(): string | null | undefined { return this._data.created; } get modified(): string | null | undefined { return this._data.modified; } get properties(): CollectionPropertiesObject { if (this._data.properties instanceof CollectionPropertiesObject) { return this._data.properties; } if (this._data.properties) { const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface); this._data.properties = hydrated; return hydrated; } return new CollectionPropertiesObject(); } set properties(value: CollectionPropertiesObject) { if (value instanceof CollectionPropertiesObject) { this._data.properties = value as any; } else { this._data.properties = value; } } } export class CollectionPropertiesObject implements CollectionPropertiesInterface { _data!: CollectionPropertiesInterface; constructor() { this._data = { '@type': 'chrono:collection', version: 1, content: [], label: '', description: null, rank: null, visibility: null, color: null, }; } fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject { this._data = data; return this; } toJson(): CollectionPropertiesInterface { return this._data; } clone(): CollectionPropertiesObject { const cloned = new CollectionPropertiesObject(); cloned._data = { ...this._data }; return cloned; } /** Immutable Properties */ get '@type'(): string { return this._data['@type']; } get version(): number { return this._data.version; } get content(): CollectionContentTypes[] { return this._data.content || []; } /** Mutable Properties */ get label(): string { return this._data.label || ''; } set label(value: string) { this._data.label = value; } get description(): string | null { return this._data.description; } set description(value: string | null) { this._data.description = value; } get rank(): number | null { return this._data.rank; } set rank(value: number | null) { this._data.rank = value; } get visibility(): boolean | null { return this._data.visibility; } set visibility(value: boolean | null) { this._data.visibility = value; } get color(): string | null { return this._data.color; } set color(value: string | null) { this._data.color = value; } }