// storage/license-store.ts — Persistence layer for license state (data/global/license.json).
import { readdir } from 'node:fs/promises';
import { readJson, writeJson, globalDataDir } from './base-store.js';
// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------
function licenseFile() {
    return `${globalDataDir()}/license.json`;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
export async function getLicenseState() {
    return readJson(licenseFile(), null);
}
export async function saveLicenseState(state) {
    await writeJson(licenseFile(), state);
}
export async function deleteLicenseState() {
    const { unlink } = await import('node:fs/promises');
    try {
        await unlink(licenseFile());
    }
    catch (err) {
        if (isNodeError(err) && err.code === 'ENOENT') {
            return;
        }
        throw err;
    }
}
export async function countActiveProjects() {
    try {
        const entries = await readdir('data/projects', { withFileTypes: true });
        return entries.filter((e) => e.isDirectory()).length;
    }
    catch (err) {
        if (isNodeError(err) && err.code === 'ENOENT') {
            return 0;
        }
        throw err;
    }
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function isNodeError(err) {
    return err instanceof Error && 'code' in err;
}
//# sourceMappingURL=license-store.js.map