/**
 * Font asset persistence adapter (docs/architecture/02; scope doc §4 → fonts).
 *
 * The typed seam between the FontStore and IndexedDB's `fonts` object store.
 * Exposes plain async functions returning domain `FontAsset` objects and migrates
 * every record on read — same contract as dataset-store / theme-store. The
 * payload is binary (`ArrayBuffer`), stored verbatim by the structured-clone
 * algorithm the generic `put`/`getAll` already use; no special handling needed.
 */

import { CURRENT_FONT_VERSION, type FontAsset } from '@core/font-asset';
import { FONTS_STORE, del, getAll, put } from './db';
import { migrateFontAsset } from './font-migrations';

/** Load every uploaded font, upgrading each record to the current shape. */
export async function loadFonts(): Promise<FontAsset[]> {
  const records = await getAll<unknown>(FONTS_STORE);
  return records.map(migrateFontAsset);
}

/** Persist a font asset at the current schema version. Propagates failures. */
export async function saveFont(font: FontAsset): Promise<void> {
  await put(FONTS_STORE, { ...font, version: CURRENT_FONT_VERSION });
}

/** Permanently remove a font asset by id. */
export async function deleteFont(id: number): Promise<void> {
  await del(FONTS_STORE, id);
}
