"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 });
const express_1 = require("express");
const api = __importStar(require("../../api/yaml-workflows"));
const router = (0, express_1.Router)();
/**
 * GET /api/yaml-workflows
 * List YAML workflows with optional status filter.
 */
router.get('/', async (req, res) => {
    const result = await api.listYamlWorkflows({
        status: req.query.status,
        graph_topic: req.query.graph_topic,
        app_id: req.query.app_id,
        search: req.query.search,
        source_workflow_id: req.query.source_workflow_id,
        set_id: req.query.set_id,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/yaml-workflows
 * Generate a YAML workflow from a completed execution.
 * Body: { workflow_id, task_queue, workflow_name, name, description? }
 */
router.post('/', async (req, res) => {
    const result = await api.createYamlWorkflow(req.body);
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/yaml-workflows/app-ids
 * Return distinct app_id values from non-archived workflows.
 */
router.get('/app-ids', async (_req, res) => {
    const result = await api.getAppIds();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/yaml-workflows/direct
 * Create a YAML workflow from raw YAML content (workflow builder output).
 * Unlike POST /, this does not require a source execution — the YAML is provided directly.
 * Body: { name, description?, yaml_content, input_schema?, activity_manifest?, tags?, app_id? }
 */
router.post('/direct', async (req, res) => {
    const result = await api.createYamlWorkflowDirect(req.body);
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/yaml-workflows/from-durable
 * Compile a durable TypeScript workflow into a YAML DAG.
 * Body: { source, is_file_path?, workflow_name, name, description?, app_id?, subscribes?, tags? }
 */
router.post('/from-durable', async (req, res) => {
    const result = await api.createYamlWorkflowFromDurable(req.body);
    res.status(result.status).json(result.data ?? { error: result.error });
});
// -- Parameterized routes --
/**
 * GET /api/yaml-workflows/:id
 * Get a single YAML workflow.
 */
router.get('/:id', async (req, res) => {
    const result = await api.getYamlWorkflow({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/yaml-workflows/:id
 * Update a YAML workflow's metadata.
 */
router.put('/:id', async (req, res) => {
    const result = await api.updateYamlWorkflow({ id: req.params.id, ...req.body });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/yaml-workflows/:id/regenerate
 * Re-generate the YAML from the original source execution.
 * Only allowed for non-archived workflows.
 */
router.post('/:id/regenerate', async (req, res) => {
    const result = await api.regenerateYamlWorkflow({
        id: req.params.id,
        task_queue: req.body.task_queue,
        compilation_feedback: req.body.compilation_feedback,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/yaml-workflows/:id
 * Delete a YAML workflow (must be draft or archived).
 */
router.delete('/:id', async (req, res) => {
    const result = await api.deleteYamlWorkflow({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
