/** * Class model for Event Interface */ import type { EventInterface, EventLocationPhysicalInterface, EventLocationVirtualInterface, EventOrganizerInterface, EventParticipantInterface, EventNotificationInterface, EventOccurrence, EventMutation } from "@/types/event"; import { EventLocationPhysicalObject } from "./event-location-physical"; import { EventLocationVirtualObject } from "./event-location-virtual"; import { EventOrganizerObject } from "./event-organizer"; import { EventParticipantObject } from "./event-participant"; import { EventNotificationObject } from "./event-notification"; import { EventOccurrenceObject } from "./event-occurrence"; import { EventMutationObject } from "./event-mutation"; import { generateKey } from "../utils/key-generator"; export class EventObject implements EventInterface { _data!: EventInterface; constructor() { this._data = { type: 'event', version: 1, urid: null, created: null, modified: null, sequence: null, timeZone: null, startsOn: null, startsTZ: null, endsOn: null, endsTZ: null, duration: null, timeless: null, label: null, description: null, locationsPhysical: {}, locationsVirtual: {}, availability: null, sensitivity: null, priority: null, color: null, tags: [], organizer: null, participants: {}, notifications: {}, pattern: null, mutations: {} }; } fromJson(data: EventInterface): EventObject { // Deep copy the data to avoid reference issues this._data = JSON.parse(JSON.stringify(data)); // Convert organizer to object instance if (this._data.organizer) { this._data.organizer = new EventOrganizerObject().fromJson(this._data.organizer) as any; } // Convert locations physical to object instances for (const key in this._data.locationsPhysical) { const loc = this._data.locationsPhysical[key]; if (loc) { this._data.locationsPhysical[key] = new EventLocationPhysicalObject().fromJson(loc) as any; } } // Convert locations virtual to object instances for (const key in this._data.locationsVirtual) { const loc = this._data.locationsVirtual[key]; if (loc) { this._data.locationsVirtual[key] = new EventLocationVirtualObject().fromJson(loc) as any; } } // Convert participants to object instances for (const key in this._data.participants) { const participant = this._data.participants[key]; if (participant) { this._data.participants[key] = new EventParticipantObject().fromJson(participant) as any; } } // Convert notifications to object instances for (const key in this._data.notifications) { const notification = this._data.notifications[key]; if (notification) { this._data.notifications[key] = new EventNotificationObject().fromJson(notification) as any; } } // Convert pattern to object instance if (this._data.pattern) { this._data.pattern = new EventOccurrenceObject().fromJson(this._data.pattern) as any; } // Convert mutations to object instances for (const key in this._data.mutations) { const mutation = this._data.mutations[key]; if (mutation) { this._data.mutations[key] = new EventMutationObject().fromJson(mutation) as any; } } return this; } toJson(): EventInterface { // Return a deep copy to avoid external modifications return JSON.parse(JSON.stringify(this._data)); } clone(): EventObject { const cloned = new EventObject(); cloned._data = JSON.parse(JSON.stringify(this._data)); return cloned; } /** Properties */ get type(): string { return this._data.type; } get version(): number { return this._data.version; } set version(value: number) { this._data.version = value; } get urid(): string | null { return this._data.urid; } set urid(value: string | null) { this._data.urid = value; } get created(): string | null { return this._data.created; } set created(value: string | null) { this._data.created = value; } get modified(): string | null { return this._data.modified; } set modified(value: string | null) { this._data.modified = value; } get sequence(): number | null { return this._data.sequence; } set sequence(value: number | null) { this._data.sequence = value; } get timeZone(): string | null { return this._data.timeZone; } set timeZone(value: string | null) { this._data.timeZone = value; } get startsOn(): string | null { return this._data.startsOn; } set startsOn(value: string | null) { this._data.startsOn = value; } get startsTZ(): string | null { return this._data.startsTZ; } set startsTZ(value: string | null) { this._data.startsTZ = value; } get endsOn(): string | null { return this._data.endsOn; } set endsOn(value: string | null) { this._data.endsOn = value; } get endsTZ(): string | null { return this._data.endsTZ; } set endsTZ(value: string | null) { this._data.endsTZ = value; } get duration(): string | null { return this._data.duration; } set duration(value: string | null) { this._data.duration = value; } get timeless(): boolean | null { return this._data.timeless; } set timeless(value: boolean | null) { this._data.timeless = value; } get label(): string | null { return this._data.label; } set label(value: string | null) { this._data.label = value; } get description(): string | null { return this._data.description; } set description(value: string | null) { this._data.description = value; } get availability(): 'free' | 'busy' | null { return this._data.availability; } set availability(value: 'free' | 'busy' | null) { this._data.availability = value; } get sensitivity(): 'public' | 'private' | 'secret' | null { return this._data.sensitivity; } set sensitivity(value: 'public' | 'private' | 'secret' | null) { this._data.sensitivity = value; } get priority(): number | null { return this._data.priority; } set priority(value: number | null) { this._data.priority = value; } get color(): string | null { return this._data.color; } set color(value: string | null) { this._data.color = value; } get locationsPhysical(): Record { return this._data.locationsPhysical as any; } set locationsPhysical(value: Record) { this._data.locationsPhysical = {} as any; for (const key in value) { const loc = value[key]; if (loc) { if (loc instanceof EventLocationPhysicalObject) { (this._data.locationsPhysical as any)[key] = loc; } else { (this._data.locationsPhysical as any)[key] = new EventLocationPhysicalObject().fromJson(loc); } } } } addLocationPhysical(value?: EventLocationPhysicalInterface|EventLocationPhysicalObject|null, key?: string): string { if (!key) { key = generateKey(); } let location: EventLocationPhysicalObject; if (!value) { location = new EventLocationPhysicalObject(); location.identifier = key; } else if (value instanceof EventLocationPhysicalObject) { location = value; location.identifier = key; } else { location = new EventLocationPhysicalObject().fromJson(value); location.identifier = key; } (this._data.locationsPhysical as any)[key] = location; return key; } removeLocationPhysical(key: string): void { delete this._data.locationsPhysical[key]; } getLocationPhysical(key: string): EventLocationPhysicalObject | undefined { return this._data.locationsPhysical[key] as any; } get locationsVirtual(): Record { return this._data.locationsVirtual as any; } set locationsVirtual(value: Record) { this._data.locationsVirtual = {} as any; for (const key in value) { const loc = value[key]; if (loc) { if (loc instanceof EventLocationVirtualObject) { (this._data.locationsVirtual as any)[key] = loc; } else { (this._data.locationsVirtual as any)[key] = new EventLocationVirtualObject().fromJson(loc); } } } } addLocationVirtual(value?: EventLocationVirtualInterface|EventLocationVirtualObject|null, key?: string): string { if (!key) { key = generateKey(); } let location: EventLocationVirtualObject; if (!value) { location = new EventLocationVirtualObject(); location.identifier = key; } else if (value instanceof EventLocationVirtualObject) { location = value; location.identifier = key; } else { location = new EventLocationVirtualObject().fromJson(value); location.identifier = key; } (this._data.locationsVirtual as any)[key] = location; return key; } removeLocationVirtual(key: string): void { delete this._data.locationsVirtual[key]; } getLocationVirtual(key: string): EventLocationVirtualObject | undefined { return this._data.locationsVirtual[key] as any; } get tags(): string[] { return this._data.tags; } set tags(value: string[]) { this._data.tags = value; } addTag(tag: string): void { if (!this._data.tags.includes(tag)) { this._data.tags.push(tag); } } removeTag(tag: string): void { const index = this._data.tags.indexOf(tag); if (index > -1) { this._data.tags.splice(index, 1); } } get organizer(): EventOrganizerObject { return this._data.organizer as any; } set organizer(value: EventOrganizerInterface|EventOrganizerObject) { if (value instanceof EventOrganizerObject) { (this._data.organizer as any) = value; } else { (this._data.organizer as any) = new EventOrganizerObject().fromJson(value); } } get participants(): Record { return this._data.participants as any; } set participants(value: Record) { this._data.participants = {} as any; for (const key in value) { const participant = value[key]; if (participant) { if (participant instanceof EventParticipantObject) { (this._data.participants as any)[key] = participant; } else { (this._data.participants as any)[key] = new EventParticipantObject().fromJson(participant); } } } } getParticipant(key: string): EventParticipantObject | undefined { return this._data.participants[key] as any; } addParticipant(value?: EventParticipantInterface|EventParticipantObject|null, key?: string): string { if (!key) { key = generateKey(); } let participant: EventParticipantObject; if (!value) { participant = new EventParticipantObject(); participant.identifier = key; } else if (value instanceof EventParticipantObject) { participant = value; participant.identifier = key; } else { participant = new EventParticipantObject().fromJson(value); participant.identifier = key; } (this._data.participants as any)[key] = participant; return key; } removeParticipant(key: string): void { delete this._data.participants[key]; } get notifications(): Record { return this._data.notifications as any; } set notifications(value: Record) { this._data.notifications = {} as any; for (const key in value) { const notification = value[key]; if (notification) { if (notification instanceof EventNotificationObject) { (this._data.notifications as any)[key] = notification; } else { (this._data.notifications as any)[key] = new EventNotificationObject().fromJson(notification); } } } } addNotification(value?: EventNotificationInterface|EventNotificationObject|null, key?: string): string { if (!key) { key = generateKey(); } let notification: EventNotificationObject; if (!value) { notification = new EventNotificationObject(); } else if (value instanceof EventNotificationObject) { notification = value; } else { notification = new EventNotificationObject().fromJson(value); } (this._data.notifications as any)[key] = notification; return key; } removeNotification(key: string): void { delete this._data.notifications[key]; } getNotification(key: string): EventNotificationObject | undefined { return this._data.notifications[key] as any; } get pattern(): EventOccurrenceObject | null { return this._data.pattern as any; } set pattern(value: EventOccurrence|EventOccurrenceObject|null) { if (!value) { this._data.pattern = null; } else if (value instanceof EventOccurrenceObject) { this._data.pattern = value as any; } else { this._data.pattern = new EventOccurrenceObject().fromJson(value) as any; } } get mutations(): Record { return this._data.mutations as any; } set mutations(value: Record) { this._data.mutations = {} as any; for (const key in value) { const mutation = value[key]; if (mutation) { if (mutation instanceof EventMutationObject) { (this._data.mutations as any)[key] = mutation; } else { (this._data.mutations as any)[key] = new EventMutationObject().fromJson(mutation); } } } } addMutation(value?: EventMutation|EventMutationObject|null, key?: string): string { if (!key) { key = generateKey(); } let mutation: EventMutationObject; if (!value) { mutation = new EventMutationObject(); } else if (value instanceof EventMutationObject) { mutation = value; } else { mutation = new EventMutationObject().fromJson(value); } (this._data.mutations as any)[key] = mutation; return key; } removeMutation(key: string): void { delete this._data.mutations[key]; } getMutation(key: string): EventMutationObject | undefined { return this._data.mutations[key] as any; } }