/** * Class model for Collection Interface */ import type { CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface, CollectionPropertiesModelInterface } from "@/types/collection"; import { clonePlain } from './clone-plain'; import type { CollectionIdentifier, ServiceIdentifier } from "@/services"; export class CollectionObject implements CollectionModelInterface { _data!: CollectionInterface; _properties: CollectionPropertiesObject | undefined = undefined; constructor() { this._data = { '@type': 'mail:collection', version: 1, provider: '', service: '', collection: null, identifier: '', properties: {'@type': 'mail:folder', label: ''}, }; } fromJson(data: CollectionInterface): CollectionObject { this._data = clonePlain(data); this._properties = undefined; return this; } toJson(): CollectionInterface { const json = this._properties ? { ...this._data, properties: this._properties.toJson(), } : this._data; return clonePlain(json); } clone(): CollectionObject { return new CollectionObject().fromJson(this.toJson()); } /** Immutable Properties */ get provider(): string { return this._data.provider; } get service(): ServiceIdentifier { return this._data.service as ServiceIdentifier; } get collection(): CollectionIdentifier | null { return this._data.collection as CollectionIdentifier | null; } get identifier(): CollectionIdentifier { return this._data.identifier as CollectionIdentifier; } 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._properties) { return this._properties; } else if (this._data.properties) { const properties = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface); this._properties = properties; return properties; } return new CollectionPropertiesObject(); } set properties(value: CollectionPropertiesObject) { this._properties = value; } } export class CollectionPropertiesObject implements CollectionPropertiesModelInterface { private _data!: CollectionPropertiesInterface; constructor() { this._data = { '@type': 'mail:folder', total: 0, unread: 0, role: null, label: '', rank: 0, subscribed: true, }; } fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject { this._data = clonePlain(data); return this; } toJson(): CollectionPropertiesInterface { return clonePlain(this._data); } clone(): CollectionPropertiesObject { return new CollectionPropertiesObject().fromJson(this.toJson()); } /** Immutable Properties */ get role(): string | null | undefined { return this._data.role; } get total(): number | undefined { return this._data.total; } get unread(): number | undefined { return this._data.unread; } /** Mutable Properties */ get label(): string { return this._data.label || ''; } set label(value: string) { this._data.label = value; } get rank(): number | undefined { return this._data.rank; } set rank(value: number) { this._data.rank = value; } get subscribed(): boolean | undefined { return this._data.subscribed; } set subscribed(value: boolean) { this._data.subscribed = value; } }