"use strict";
// Per-skill usage metadata, persisted in .harness/skill-usage.json so the
// Curator has something to grade staleness on. Also tracks pin (curator may
// not touch) and archive (skill moved out of the active library).
//
// Kept as a single JSON file rather than a sidecar per skill so the Curator
// can scan the whole library cheaply and so manual edits stay easy to review.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSkillUsage = loadSkillUsage;
exports.saveSkillUsage = saveSkillUsage;
exports.recordSkillView = recordSkillView;
exports.recordSkillUse = recordSkillUse;
exports.setSkillPinned = setSkillPinned;
exports.setSkillArchived = setSkillArchived;
exports.listSkillUsage = listSkillUsage;
exports.getSkillUsage = getSkillUsage;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
function usageFilePath(projectDir) {
    return path.join(projectDir, '.harness', 'skill-usage.json');
}
async function loadSkillUsage(projectDir) {
    try {
        const raw = await fs.readFile(usageFilePath(projectDir), 'utf-8');
        const parsed = JSON.parse(raw);
        if (parsed.version !== 1 || typeof parsed.records !== 'object' || parsed.records === null) {
            return { version: 1, records: {} };
        }
        return parsed;
    }
    catch {
        return { version: 1, records: {} };
    }
}
async function saveSkillUsage(projectDir, store) {
    await fs.mkdir(path.dirname(usageFilePath(projectDir)), { recursive: true });
    await fs.writeFile(usageFilePath(projectDir), JSON.stringify(store, null, 2), 'utf-8');
}
function ensureRecord(store, name, now) {
    let record = store.records[name];
    if (!record) {
        record = { name, useCount: 0, viewCount: 0, pinned: false, archived: false, firstSeenAt: now, updatedAt: now };
        store.records[name] = record;
    }
    return record;
}
async function recordSkillView(projectDir, name, now = new Date()) {
    const store = await loadSkillUsage(projectDir);
    const iso = now.toISOString();
    const record = ensureRecord(store, name, iso);
    record.viewCount += 1;
    record.lastViewedAt = iso;
    record.updatedAt = iso;
    await saveSkillUsage(projectDir, store);
    return record;
}
async function recordSkillUse(projectDir, name, now = new Date()) {
    const store = await loadSkillUsage(projectDir);
    const iso = now.toISOString();
    const record = ensureRecord(store, name, iso);
    record.useCount += 1;
    record.lastUsedAt = iso;
    record.updatedAt = iso;
    await saveSkillUsage(projectDir, store);
    return record;
}
async function setSkillPinned(projectDir, name, pinned, now = new Date()) {
    const store = await loadSkillUsage(projectDir);
    const iso = now.toISOString();
    const record = ensureRecord(store, name, iso);
    record.pinned = pinned;
    record.updatedAt = iso;
    await saveSkillUsage(projectDir, store);
    return record;
}
async function setSkillArchived(projectDir, name, archived, now = new Date()) {
    const store = await loadSkillUsage(projectDir);
    const iso = now.toISOString();
    const record = ensureRecord(store, name, iso);
    record.archived = archived;
    record.updatedAt = iso;
    await saveSkillUsage(projectDir, store);
    return record;
}
async function listSkillUsage(projectDir) {
    const store = await loadSkillUsage(projectDir);
    return Object.values(store.records);
}
async function getSkillUsage(projectDir, name) {
    const store = await loadSkillUsage(projectDir);
    return store.records[name];
}
//# sourceMappingURL=skillUsage.js.map