/**
 * Authored-skill tracking.
 *
 * Records skills that the current user has *published* (as opposed to
 * *installed*). Source-origin skills don't appear in `vskill.lock` (only
 * installs do), so without a separate manifest the `vskill outdated`
 * poll silently ignores them — leaving publishers blind to their own
 * downstream version drift (project_vskill_source_origin_update_tracking.md).
 *
 * File: `vskill.authored.json` at project root.
 *
 * Increment 0794 — US-006 / T-006.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
const FILE_NAME = "vskill.authored.json";
const SCHEMA_VERSION = 1;
function authoredPath(projectRoot) {
    return join(projectRoot, FILE_NAME);
}
function emptyFile() {
    return { version: SCHEMA_VERSION, skills: {} };
}
function readFile(projectRoot) {
    const filePath = authoredPath(projectRoot);
    if (!existsSync(filePath))
        return emptyFile();
    try {
        const parsed = JSON.parse(readFileSync(filePath, "utf8"));
        // Backward-compatible read: tolerate older shapes.
        if (parsed && typeof parsed === "object" && parsed.skills && typeof parsed.skills === "object") {
            return {
                version: typeof parsed.version === "number" ? parsed.version : SCHEMA_VERSION,
                skills: parsed.skills,
            };
        }
        return emptyFile();
    }
    catch {
        return emptyFile();
    }
}
function writeFile(projectRoot, file) {
    const filePath = authoredPath(projectRoot);
    mkdirSync(dirname(filePath), { recursive: true });
    writeFileSync(filePath, JSON.stringify(file, null, 2) + "\n");
}
/**
 * Insert or update an authored-skill entry. Idempotent: same name on the
 * same source path is a no-op except for refreshing `publishedAt`.
 */
export function addAuthoredSkill(projectRoot, name, sourcePath, publishedAt) {
    const file = readFile(projectRoot);
    file.skills[name] = {
        sourcePath,
        publishedAt: publishedAt ?? new Date().toISOString(),
    };
    writeFile(projectRoot, file);
}
/**
 * Remove an authored-skill entry. No-op when the name is absent.
 */
export function removeAuthoredSkill(projectRoot, name) {
    const file = readFile(projectRoot);
    if (!(name in file.skills))
        return;
    delete file.skills[name];
    writeFile(projectRoot, file);
}
/**
 * Read all authored entries as a flat array. Returns `[]` when the
 * `vskill.authored.json` file is missing or unreadable (graceful degrade).
 */
export function readAuthored(projectRoot) {
    const file = readFile(projectRoot);
    return Object.entries(file.skills).map(([name, entry]) => ({
        name,
        sourcePath: entry.sourcePath,
        publishedAt: entry.publishedAt,
    }));
}
//# sourceMappingURL=authored.js.map