"use strict";
/**
 * FileWorkflowProvider — load workflow YAMLs from the filesystem.
 *
 * Two roots are searched in priority order:
 *   1. <process.cwd()>/.catalog/workflows/        (project-local override)
 *   2. <packageRoot>/dist-templates/workflows/    (npm-shipped defaults)
 *
 * IDs collide → local catalog wins. The full list is the union of both
 * roots, de-duplicated by id (id read from `workflow.id` after parse).
 *
 * Story 4.4 — OpenLife v1.2 Royal Stack.
 */
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.FileWorkflowProvider = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const WorkflowParser_1 = require("../workflow/WorkflowParser");
class FileWorkflowProvider {
    roots;
    constructor(opts = {}) {
        const cwd = opts.cwd || process.cwd();
        const packageRoot = opts.packageRoot || path.resolve(__dirname, '..', '..', '..');
        this.roots = [
            { dir: path.join(cwd, '.catalog', 'workflows'), source: 'catalog' },
            { dir: path.join(packageRoot, 'dist-templates', 'workflows'), 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 f of fs.readdirSync(dir)) {
                if (!f.endsWith('.yaml') && !f.endsWith('.yml'))
                    continue;
                const filePath = path.join(dir, f);
                const parsed = (0, WorkflowParser_1.parseWorkflowFile)(filePath);
                if (!parsed.ok || !parsed.workflow)
                    continue;
                const wf = parsed.workflow;
                if (seen.has(wf.id))
                    continue; // local catalog wins
                seen.add(wf.id);
                out.push({
                    id: wf.id,
                    name: wf.name,
                    type: wf.type,
                    version: wf.version,
                    filePath,
                    source,
                });
            }
        }
        return out;
    }
    load(id) {
        for (const entry of this.list()) {
            if (entry.id !== id)
                continue;
            const parsed = (0, WorkflowParser_1.parseWorkflowFile)(entry.filePath);
            if (!parsed.ok || !parsed.workflow)
                return null;
            return { entry, workflow: parsed.workflow };
        }
        return null;
    }
}
exports.FileWorkflowProvider = FileWorkflowProvider;
