"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.listPipelines = listPipelines;
exports.getPipeline = getPipeline;
exports.deployPipeline = deployPipeline;
exports.invokePipeline = invokePipeline;
exports.archivePipeline = archivePipeline;
const picocolors_1 = __importDefault(require("picocolors"));
const client_1 = require("../client");
const format_1 = require("../format");
const COLUMNS = [
    { key: 'id', label: 'ID', width: 12, format: (v) => v?.slice(0, 12) || '' },
    { key: 'name', label: 'Name', width: 24 },
    { key: 'app_id', label: 'Namespace', width: 14 },
    { key: 'status', label: 'Status', width: 10, format: format_1.formatStatus },
    { key: 'updated_at', label: 'Updated', width: 12, format: format_1.formatTime },
];
async function listPipelines(opts) {
    const params = new URLSearchParams();
    if (opts.status)
        params.set('status', opts.status);
    if (opts.limit)
        params.set('limit', opts.limit);
    const data = await (0, client_1.apiFetch)(`/yaml-workflows?${params}`);
    const workflows = data.workflows || data;
    (0, format_1.output)(data, Array.isArray(workflows) ? workflows : [], COLUMNS, opts);
}
async function getPipeline(id, opts) {
    const data = await (0, client_1.apiFetch)(`/yaml-workflows/${id}`);
    if (opts.json) {
        console.log(JSON.stringify(data, null, 2));
        return;
    }
    console.log(`\n  ${picocolors_1.default.bold(data.name)} ${picocolors_1.default.dim(data.id)}`);
    console.log(`  Namespace: ${data.app_id}  Topic: ${data.graph_topic}  Status: ${(0, format_1.formatStatus)(data.status)}`);
    if (data.description)
        console.log(`  ${data.description}`);
    console.log(`  Activities: ${data.activity_manifest?.length || 0}  Version: ${data.app_version || '—'}`);
    console.log(`  Created: ${(0, format_1.formatTime)(data.created_at)}  Updated: ${(0, format_1.formatTime)(data.updated_at)}`);
    console.log();
}
async function deployPipeline(id) {
    const data = await (0, client_1.apiFetch)(`/yaml-workflows/${id}/deploy`, { method: 'POST' });
    console.log(`\n  ${picocolors_1.default.green('✓')} Deployed ${picocolors_1.default.bold(data.name || id)} → ${(0, format_1.formatStatus)(data.status || 'active')}\n`);
}
async function invokePipeline(id, opts) {
    const inputData = opts.data ? JSON.parse(opts.data) : {};
    const body = { id, data: inputData };
    if (opts.sync)
        body.sync = true;
    const data = await (0, client_1.apiFetch)(`/yaml-workflows/${id}/invoke`, {
        method: 'POST',
        body: JSON.stringify(body),
    });
    if (opts.json) {
        console.log(JSON.stringify(data, null, 2));
        return;
    }
    console.log(`\n  ${picocolors_1.default.green('✓')} Invoked → ${picocolors_1.default.dim(data.job_id || data.workflowId || 'ok')}\n`);
}
async function archivePipeline(id) {
    await (0, client_1.apiFetch)(`/yaml-workflows/${id}/archive`, { method: 'POST' });
    console.log(`\n  ${picocolors_1.default.green('✓')} Archived ${picocolors_1.default.dim(id)}\n`);
}
