import { readJsonFile, writeJsonFile, getDataDir } from './json-store.js';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
const FILE = () => join(getDataDir(), 'speech-recognition.json');
function load() {
    return readJsonFile(FILE()) ?? [];
}
function save(items) {
    writeJsonFile(FILE(), items);
}
export function listSpeechConfigs() {
    return load();
}
export function getSpeechConfig(id) {
    return load().find(c => c.id === id);
}
export function getDefaultSpeechConfig() {
    const items = load();
    return items.find(c => c.enabled !== false) ?? undefined;
}
export function createSpeechConfig(input) {
    const items = load();
    const now = new Date().toISOString();
    const item = {
        id: randomUUID(),
        provider: input.provider,
        label: input.label || input.provider,
        enabled: true,
        credentials: input.credentials,
        createdAt: now,
        updatedAt: now,
    };
    items.push(item);
    save(items);
    return item;
}
export function updateSpeechConfig(id, patch) {
    const items = load();
    const idx = items.findIndex(c => c.id === id);
    if (idx === -1)
        return undefined;
    if (patch.label !== undefined)
        items[idx].label = patch.label;
    if (patch.credentials !== undefined)
        items[idx].credentials = patch.credentials;
    if (patch.enabled !== undefined)
        items[idx].enabled = patch.enabled;
    items[idx].updatedAt = new Date().toISOString();
    save(items);
    return items[idx];
}
export function deleteSpeechConfig(id) {
    const items = load();
    const idx = items.findIndex(c => c.id === id);
    if (idx === -1)
        return false;
    items.splice(idx, 1);
    save(items);
    return true;
}
//# sourceMappingURL=speech-recognition-store.js.map