"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runEjectCommand = runEjectCommand;
exports.ejectSystemWorkflows = ejectSystemWorkflows;
exports.ejectSystemPrompts = ejectSystemPrompts;
exports.ejectSystemCatalog = ejectSystemCatalog;
const node_path_1 = __importDefault(require("node:path"));
const paths_1 = require("../core/paths");
const eject_packaged_assets_1 = require("../core/eject-packaged-assets");
const branding_1 = require("../core/branding");
const paths_2 = require("../system-prompts/paths");
const template_registry_1 = require("../workflows/template-registry");
const SYSTEM_WORKFLOW_FILE_EXTENSION = ".md";
const SYSTEM_PROMPT_FILE_EXTENSION = ".md";
async function runEjectCommand(cwd, args) {
    switch (args.target) {
        case "workflows":
            await runEjectSystemWorkflowsCommand(cwd, toEjectOptions(args));
            return 0;
        case "system-prompts":
            await runEjectSystemPromptsCommand(cwd, toEjectOptions(args));
            return 0;
        case "catalog":
            await runEjectSystemCatalogCommand(cwd, toEjectOptions(args));
            return 0;
        case "all":
            await runEjectSystemWorkflowsCommand(cwd, toEjectOptions(args));
            await runEjectSystemPromptsCommand(cwd, toEjectOptions(args));
            await runEjectSystemCatalogCommand(cwd, toEjectOptions(args));
            return 0;
    }
}
function toEjectOptions(args) {
    return args.force === undefined ? {} : { force: args.force };
}
async function ejectSystemWorkflows(options) {
    const sourceDirectory = options.sourceDirectory ?? (0, paths_1.getPackageSystemWorkflowsDirectory)();
    const targetDirectory = node_path_1.default.join((0, template_registry_1.getWorkflowTemplatesDirectoryPath)(options.cwd), paths_1.SYSTEM_WORKFLOW_TEMPLATE_DIRECTORY);
    return (0, eject_packaged_assets_1.ejectPackagedAssets)({
        sourceDirectory,
        targetDirectory,
        fileExtension: SYSTEM_WORKFLOW_FILE_EXTENSION,
        assetLabel: "system workflow",
        invalidPathLabel: "system workflow",
        ...(options.force === undefined ? {} : { force: options.force }),
    });
}
async function ejectSystemPrompts(options) {
    const sourceDirectory = options.sourceDirectory ?? (0, paths_1.getPackageSystemPromptsDirectory)();
    return (0, eject_packaged_assets_1.ejectPackagedAssets)({
        sourceDirectory,
        targetDirectory: (0, paths_2.getRepoSystemPromptsDirectoryPath)(options.cwd),
        fileExtension: SYSTEM_PROMPT_FILE_EXTENSION,
        assetLabel: "system prompt",
        invalidPathLabel: "system prompt",
        ...(options.force === undefined ? {} : { force: options.force }),
    });
}
async function runEjectSystemWorkflowsCommand(cwd, options = {}) {
    const result = await ejectSystemWorkflows({ cwd, ...options });
    console.log((0, eject_packaged_assets_1.formatEjectPackagedAssetsSummary)({
        cwd,
        label: "System workflows",
        result,
    }));
    return 0;
}
async function ejectSystemCatalog(options) {
    const sourceDirectory = options.sourceDirectory ?? (0, paths_1.getPackageSystemCatalogDirectory)();
    return (0, eject_packaged_assets_1.ejectPackagedAssets)({
        sourceDirectory,
        targetDirectory: node_path_1.default.join(options.cwd, branding_1.REPO_HOME_DIRECTORY_NAME, paths_1.CATALOG_DIRECTORY, paths_1.SYSTEM_CATALOG_DIRECTORY),
        fileExtension: ".json",
        assetLabel: "system catalog",
        invalidPathLabel: "system catalog",
        ...(options.force === undefined ? {} : { force: options.force }),
    });
}
async function runEjectSystemCatalogCommand(cwd, options = {}) {
    const result = await ejectSystemCatalog({
        cwd,
        ...(options.force === undefined ? {} : { force: options.force }),
    });
    console.log((0, eject_packaged_assets_1.formatEjectPackagedAssetsSummary)({
        cwd,
        label: "System catalog",
        result,
    }));
    return 0;
}
async function runEjectSystemPromptsCommand(cwd, options = {}) {
    const result = await ejectSystemPrompts({ cwd, ...options });
    console.log((0, eject_packaged_assets_1.formatEjectPackagedAssetsSummary)({
        cwd,
        label: "System prompts",
        result,
    }));
    return 0;
}
