import { PersistentStore } from '../../state/persistent-store.js';
import { resolveAutomationStorePath } from './paths.js';
function defaultSnapshot() {
    return {
        version: 1,
        routes: [],
    };
}
function validateSnapshot(snapshot) {
    if (!snapshot)
        return defaultSnapshot();
    if (snapshot.version !== 1 || !Array.isArray(snapshot.routes)) {
        throw new Error('Automation routes store snapshot is invalid.');
    }
    return {
        version: 1,
        routes: snapshot.routes,
    };
}
export class AutomationRouteStore {
    store;
    constructor(config = {}) {
        const path = typeof config === 'string'
            ? config
            : config.path ?? resolveAutomationStorePath('automation-routes.json', config.configManager ?? {});
        this.store = new PersistentStore(path);
    }
    async load() {
        return validateSnapshot(await this.store.load());
    }
    async save(routes) {
        await this.store.persist({
            version: 1,
            routes: [...routes],
        });
    }
}
