"use strict";
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.SquadRegistry = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const FileSquadProvider_1 = require("./providers/FileSquadProvider");
const CloudSquadProvider_1 = require("./providers/CloudSquadProvider");
const CompositeSquadProvider_1 = require("./providers/CompositeSquadProvider");
// v1.8.3+: prefer the user's project-local .catalog/squads (override) and
// fall back to the bundled package .catalog/squads when running from a
// globally-installed @openlife/cli. FileSquadProvider takes a single string,
// so we pick the right one upfront rather than refactoring the provider.
const defaultSquadRoot = () => {
    if (process.env.OPENLIFE_SQUAD_ROOT)
        return process.env.OPENLIFE_SQUAD_ROOT;
    const cwdRoot = path.join(process.cwd(), '.catalog', 'squads');
    if (fs.existsSync(cwdRoot))
        return cwdRoot;
    return path.join(__dirname, '..', '..', '.catalog', 'squads');
};
class SquadRegistry {
    provider;
    squads = [];
    constructor(provider = new CompositeSquadProvider_1.CompositeSquadProvider(new CloudSquadProvider_1.CloudSquadProvider(process.env.OPENLIFE_CLOUD_SQUADS_FILE || ''), new FileSquadProvider_1.FileSquadProvider(defaultSquadRoot()))) {
        this.provider = provider;
        this.load();
    }
    load() {
        this.squads = this.provider.load();
    }
    all() {
        return this.squads;
    }
    findByCapability(capability) {
        const normalized = capability.toLowerCase();
        return this.squads.filter(squad => squad.capabilities.some(cap => cap.toLowerCase().includes(normalized)) ||
            squad.domain.toLowerCase().includes(normalized) ||
            squad.name.toLowerCase().includes(normalized));
    }
}
exports.SquadRegistry = SquadRegistry;
