import { join } from 'node:path';
import { v4 as uuid } from 'uuid';
import { getDataDir, ensureDir, readJsonFile, writeJsonFile } from './json-store.js';
const FILE = () => {
    const dir = join(getDataDir(), 'subscriptions');
    ensureDir(dir);
    return join(dir, 'configs.json');
};
export function listSubscriptions() {
    return readJsonFile(FILE()) ?? [];
}
function saveAll(items) {
    writeJsonFile(FILE(), items);
}
export function getSubscription(id) {
    return listSubscriptions().find(s => s.id === id);
}
export function createSubscription(data) {
    const items = listSubscriptions();
    const now = new Date().toISOString();
    const item = { ...data, id: uuid(), createdAt: now, updatedAt: now };
    items.push(item);
    saveAll(items);
    return item;
}
export function updateSubscription(id, patch) {
    const items = listSubscriptions();
    const idx = items.findIndex(s => s.id === id);
    if (idx < 0)
        return undefined;
    Object.assign(items[idx], patch, { updatedAt: new Date().toISOString() });
    saveAll(items);
    return items[idx];
}
export function deleteSubscription(id) {
    const items = listSubscriptions();
    const len = items.length;
    const filtered = items.filter(s => s.id !== id);
    if (filtered.length === len)
        return false;
    saveAll(filtered);
    return true;
}
//# sourceMappingURL=subscription-store.js.map