import { packageContext } from 'kody:runtime' import { loadAccount, saveAccount, type AudibleRow, type FakeAccount } from './state.ts' const rows = (value: unknown, name: string): AudibleRow[] => { if (!Array.isArray(value) || value.some((r) => !r || typeof r !== 'object' || typeof r.asin !== 'string')) throw new Error(`${name} must be an array of rows with an asin.`) return value as AudibleRow[] } /** * Mock-only: replace the fake account, or disconnect with `{ connected: false }`. * Refuses outside a local run, so it cannot write to a hosted bucket. */ export default async function seed(input: { connected?: boolean locale?: string library?: unknown wishlist?: unknown accountInformation?: unknown }) { const id = String(packageContext?.packageId ?? '') if (!id.startsWith('local')) throw new Error(`The audible-auth fake seeds only local runs; packageId is ${JSON.stringify(id)}.`) if (input.connected === false) { await saveAccount(null) return { connected: false, bucket: id } } const account: FakeAccount = { locale: input.locale ?? 'ca', connectedAt: new Date().toISOString(), library: rows(input.library ?? [], 'library'), wishlist: rows(input.wishlist ?? [], 'wishlist'), accountInformation: input.accountInformation ?? {}, } await saveAccount(account) const saved = await loadAccount() return { connected: true, bucket: id, library: saved?.library.length ?? 0, wishlist: saved?.wishlist.length ?? 0 } }