"use strict";
/**
 * FileCapabilityProvider — load Capability Packs from the filesystem.
 *
 * Two roots searched in priority order (same pattern as FileWorkflowProvider):
 *   1. <cwd>/.catalog/capabilities/        (project-local override)
 *   2. <packageRoot>/dist-templates/capabilities/  (npm-shipped defaults)
 *
 * Local catalog wins on id collision. Honors `.catalog/`-as-runtime-SoT.
 *
 * Story 1.2 — OpenLife v1.3 Agent OS Integration.
 */
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.FileCapabilityProvider = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const CapabilityPackParser_1 = require("../capability/CapabilityPackParser");
class FileCapabilityProvider {
    roots;
    constructor(opts = {}) {
        const cwd = opts.cwd || process.cwd();
        const packageRoot = opts.packageRoot || path.resolve(__dirname, '..', '..', '..');
        const catalogRoot = opts.catalogRoot || path.join(cwd, '.catalog', 'capabilities');
        const templateRoot = opts.templateRoot || path.join(packageRoot, 'dist-templates', 'capabilities');
        this.roots = [
            { dir: catalogRoot, source: 'catalog' },
            { dir: templateRoot, source: 'dist-templates' },
        ];
    }
    list() {
        const seen = new Set();
        const out = [];
        for (const { dir, source } of this.roots) {
            if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory())
                continue;
            for (const entry of fs.readdirSync(dir)) {
                if (entry.startsWith('.'))
                    continue;
                const packDir = path.join(dir, entry);
                const stat = fs.statSync(packDir);
                if (!stat.isDirectory())
                    continue;
                const manifest = path.join(packDir, 'capability.yaml');
                if (!fs.existsSync(manifest))
                    continue;
                const parsed = (0, CapabilityPackParser_1.parseCapabilityPackFile)(manifest);
                // Tolerate parse failures at list-time: skip with a console hint
                // (the registry layer will re-validate when actually loading).
                if (!parsed.ok || !parsed.pack)
                    continue;
                const pack = parsed.pack;
                if (seen.has(pack.id))
                    continue; // catalog wins
                seen.add(pack.id);
                out.push({
                    id: pack.id,
                    name: pack.name,
                    description: pack.description,
                    version: pack.version,
                    status: pack.status,
                    filePath: manifest,
                    packDir,
                    source,
                });
            }
        }
        return out;
    }
    load(id) {
        return this.list().find((e) => e.id === id) || null;
    }
}
exports.FileCapabilityProvider = FileCapabilityProvider;
