"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveOutputPath = resolveOutputPath;
exports.writeCompiledYaml = writeCompiledYaml;
exports.printDiscovery = printDiscovery;
exports.printCompiled = printCompiled;
exports.printSummary = printSummary;
exports.printError = printError;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const picocolors_1 = __importDefault(require("picocolors"));
/**
 * Build the output file path for a compiled workflow.
 * Default: adjacent to source — assembly-line.ts → assembly-line.compiled.yaml
 */
function resolveOutputPath(sourcePath, outputDir) {
    const base = path.basename(sourcePath, path.extname(sourcePath));
    const outputFile = `${base}.compiled.yaml`;
    if (outputDir) {
        fs.mkdirSync(outputDir, { recursive: true });
        return path.join(outputDir, outputFile);
    }
    return path.join(path.dirname(sourcePath), outputFile);
}
/**
 * Write the compiled YAML with a header comment.
 */
function writeCompiledYaml(outputPath, result, sourcePath, functionName, model) {
    const header = [
        `# Compiled from: ${path.basename(sourcePath)}`,
        `# Function: ${functionName}`,
        `# Generated: ${new Date().toISOString()}`,
        `# Model: ${model}`,
        '#',
        '# This YAML DAG is functionally equivalent to the source durable workflow',
        '# but runs without replay overhead. Each step executes exactly once.',
        '',
    ].join('\n');
    fs.writeFileSync(outputPath, header + result.yaml, 'utf-8');
}
/**
 * Print a discovery summary (dry-run mode).
 */
function printDiscovery(workflows) {
    console.log();
    console.log(picocolors_1.default.bold(`  Found ${workflows.length} workflow${workflows.length === 1 ? '' : 's'}:`));
    console.log();
    for (const wf of workflows) {
        const framework = wf.framework === 'hotmesh-durable'
            ? picocolors_1.default.cyan('HotMesh Durable')
            : wf.framework === 'temporal'
                ? picocolors_1.default.magenta('Temporal')
                : picocolors_1.default.dim('unknown');
        console.log(`  ${picocolors_1.default.green('●')} ${picocolors_1.default.bold(wf.relativePath)}`);
        console.log(`    Function: ${picocolors_1.default.white(wf.functionName)}  ·  ${framework}`);
        if (wf.activities.length > 0) {
            console.log(`    Activities: ${wf.activities.join(', ')}`);
        }
        if (wf.primitives.length > 0) {
            console.log(`    Control flow: ${wf.primitives.join(', ')}`);
        }
        console.log();
    }
}
/**
 * Print a compilation success line.
 */
function printCompiled(relativePath, outputRelative, result) {
    const activityCount = result.activityManifest.filter((a) => a.type === 'worker').length;
    const inputKeys = Object.keys(result.inputSchema?.properties || {});
    const inputs = inputKeys.length > 0 ? inputKeys.join(', ') : 'none';
    console.log(`  ${picocolors_1.default.green('✓')} ${relativePath} ${picocolors_1.default.dim('→')} ${picocolors_1.default.bold(outputRelative)}`);
    console.log(`    ${activityCount} activities · ${inputKeys.length} inputs (${inputs}) · topic: ${picocolors_1.default.cyan(result.graphTopic)}`);
}
/**
 * Print final summary.
 */
function printSummary(count, elapsedMs) {
    const seconds = (elapsedMs / 1000).toFixed(1);
    console.log();
    console.log(`  ${picocolors_1.default.bold(`Compiled ${count} workflow${count === 1 ? '' : 's'}`)} in ${seconds}s`);
    console.log();
}
/**
 * Print an error for a single workflow.
 */
function printError(relativePath, error) {
    console.log(`  ${picocolors_1.default.red('✗')} ${relativePath}`);
    console.log(`    ${picocolors_1.default.red(error)}`);
}
