/** * User Profile Model */ import type { ProfileFieldInterface, UserProfileInterface } from '@KTXC/types/user/userProfileTypes'; export class UserProfile { private _data: UserProfileInterface; constructor(data: UserProfileInterface = {}) { this._data = data; } fromJson(data: UserProfileInterface): UserProfile { this._data = data; return this; } toJson(): UserProfileInterface { return { ...this._data }; } clone(): UserProfile { return new UserProfile(JSON.parse(JSON.stringify(this._data))); } /** * Get a profile field value */ get(key: string): any { return this._data[key]?.value ?? null; } /** * Set a profile field value (only if editable) */ set(key: string, value: any): boolean { if (!this._data[key]) { console.warn(`Profile field "${key}" does not exist`); return false; } if (!this._data[key].editable) { console.warn(`Profile field "${key}" is managed by ${this._data[key].provider} and cannot be edited`); return false; } this._data[key].value = value; return true; } /** * Check if a field is editable */ isEditable(key: string): boolean { return this._data[key]?.editable ?? false; } /** * Get field metadata */ getField(key: string): ProfileFieldInterface | null { return this._data[key] ?? null; } /** * Get all fields */ get fields(): UserProfileInterface { return this._data; } /** * Get only editable fields */ get editableFields(): UserProfileInterface { return Object.entries(this._data) .filter(([_, field]) => (field as ProfileFieldInterface).editable) .reduce((acc, [key, field]) => ({ ...acc, [key]: field }), {} as UserProfileInterface); } /** * Get only managed (non-editable) fields */ get managedFields(): UserProfileInterface { return Object.entries(this._data) .filter(([_, field]) => !(field as ProfileFieldInterface).editable) .reduce((acc, [key, field]) => ({ ...acc, [key]: field }), {} as UserProfileInterface); } /** * Get provider managing this profile */ get provider(): string | null { const managedField = Object.values(this._data).find(f => !(f as ProfileFieldInterface).editable); return (managedField as ProfileFieldInterface)?.provider ?? null; } }