import { loadProfile, saveProfile } from '$lib/db.js'; import { getUserId } from '$lib/crypto.js'; import type { Tag, UserProfile } from '$lib/types.js'; let profile = $state(null); let loading = $state(true); export function getProfile() { return { get current() { return profile; }, get loading() { return loading; } }; } export async function initProfile(): Promise { loading = true; const existing = await loadProfile(); if (existing) { profile = existing; loading = false; return existing; } const userId = await getUserId(); const newProfile: UserProfile = { id: userId, name: '', bio: '', tags: [], updatedAt: Date.now(), signature: '' }; await saveProfile(newProfile); profile = newProfile; loading = false; return newProfile; } export async function updateProfile(updates: Partial>): Promise { if (!profile) return; profile = { ...profile, ...updates, updatedAt: Date.now() }; await saveProfile(profile); } export async function addTag(tag: Tag): Promise { if (!profile) return; const tags = [...profile.tags, tag]; await updateProfile({ tags }); } export async function removeTag(index: number): Promise { if (!profile) return; const tags = profile.tags.filter((_, i) => i !== index); await updateProfile({ tags }); }