"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 });
exports.listJobs = listJobs;
exports.exportWorkflowState = exportWorkflowState;
exports.exportWorkflowExecution = exportWorkflowExecution;
exports.getWorkflowStatus = getWorkflowStatus;
exports.getWorkflowState = getWorkflowState;
const exportService = __importStar(require("../services/export"));
const resolve_1 = require("../services/task/resolve");
/**
 * List export jobs with optional filtering, sorting, and pagination.
 *
 * @param input.limit — maximum number of jobs to return
 * @param input.offset — number of jobs to skip for pagination
 * @param input.entity — filter by entity/workflow type
 * @param input.search — free-text search across job fields
 * @param input.status — filter by job status
 * @param input.sort_by — field name to sort results by
 * @param input.order — sort direction (asc or desc)
 * @param input.registered — filter by registration status
 * @returns `{ status: 200, data: { jobs, total, ... } }` on success
 */
async function listJobs(input) {
    try {
        const result = await exportService.listJobs(input);
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Export the stored state (hash data) of a workflow.
 *
 * Resolves the workflow handle from its ID, then exports its state fields.
 * Fields can be filtered via allow/block lists. Returns 404 if the workflow
 * cannot be found or its data has expired.
 *
 * @param input.workflowId — the workflow's unique identifier
 * @param input.allow — whitelist of field names to include in the export
 * @param input.block — blacklist of field names to exclude from the export
 * @param input.values — when true, include field values (not just field names)
 * @returns `{ status: 200, data: ExportedState }` on success
 */
async function exportWorkflowState(input) {
    try {
        let resolved;
        try {
            resolved = await (0, resolve_1.resolveWorkflowHandle)(input.workflowId);
        }
        catch {
            return { status: 404, error: 'Workflow not found' };
        }
        const exported = await exportService.exportWorkflow(input.workflowId, resolved.taskQueue, resolved.workflowName, { allow: input.allow, block: input.block, values: input.values });
        return { status: 200, data: exported };
    }
    catch (err) {
        const msg = err.message ?? '';
        if (msg.includes('not found') || msg.includes('undefined')) {
            return {
                status: 404,
                error: 'Workflow data is no longer available (job may have expired)',
            };
        }
        return { status: 500, error: msg };
    }
}
/**
 * Export the full execution tree of a workflow.
 *
 * Resolves the workflow handle, then exports its execution graph including
 * activity inputs and results. Returns 404 if the workflow cannot be found
 * or its data has expired.
 *
 * @param input.workflowId — the workflow's unique identifier
 * @param input.excludeSystem — when true, omit system-generated activities from the export
 * @param input.omitResults — when true, exclude activity result payloads
 * @param input.mode — export mode controlling output format (e.g., tree, flat)
 * @param input.maxDepth — maximum depth to traverse in the execution tree
 * @returns `{ status: 200, data: ExecutionExport }` on success
 */
async function exportWorkflowExecution(input) {
    try {
        let resolved;
        try {
            resolved = await (0, resolve_1.resolveWorkflowHandle)(input.workflowId);
        }
        catch {
            return { status: 404, error: 'Workflow not found' };
        }
        const execution = await exportService.exportWorkflowExecution(input.workflowId, resolved.taskQueue, resolved.workflowName, {
            exclude_system: input.excludeSystem ?? false,
            omit_results: input.omitResults ?? false,
            mode: input.mode || undefined,
            max_depth: input.maxDepth,
            enrich_inputs: true,
        });
        return { status: 200, data: execution };
    }
    catch (err) {
        const status = err.status === 404 || err.message?.includes('Not Found') ? 404 : 500;
        return {
            status,
            error: status === 404
                ? 'Workflow data is no longer available (job may have expired)'
                : err.message,
        };
    }
}
/**
 * Get the current status of a workflow (e.g., running, completed, failed).
 *
 * Resolves the workflow handle, then queries its status. Returns 404 if the
 * workflow cannot be found or its data has expired.
 *
 * @param input.workflowId — the workflow's unique identifier
 * @returns `{ status: 200, data: WorkflowStatus }` on success
 */
async function getWorkflowStatus(input) {
    try {
        let resolved;
        try {
            resolved = await (0, resolve_1.resolveWorkflowHandle)(input.workflowId);
        }
        catch {
            return { status: 404, error: 'Workflow not found' };
        }
        const result = await exportService.getWorkflowStatus(input.workflowId, resolved.taskQueue, resolved.workflowName);
        return { status: 200, data: result };
    }
    catch (err) {
        const status = err.status === 404 || err.message?.includes('Not Found') ? 404 : 500;
        return {
            status,
            error: status === 404
                ? 'Workflow data is no longer available (job may have expired)'
                : err.message,
        };
    }
}
/**
 * Get the current state data of a workflow.
 *
 * Resolves the workflow handle, then retrieves its full state. Returns 404
 * if the workflow cannot be found or its data has expired.
 *
 * @param input.workflowId — the workflow's unique identifier
 * @returns `{ status: 200, data: WorkflowState }` on success
 */
async function getWorkflowState(input) {
    try {
        let resolved;
        try {
            resolved = await (0, resolve_1.resolveWorkflowHandle)(input.workflowId);
        }
        catch {
            return { status: 404, error: 'Workflow not found' };
        }
        const result = await exportService.getWorkflowState(input.workflowId, resolved.taskQueue, resolved.workflowName);
        return { status: 200, data: result };
    }
    catch (err) {
        const status = err.status === 404 || err.message?.includes('Not Found') ? 404 : 500;
        return {
            status,
            error: status === 404
                ? 'Workflow data is no longer available (job may have expired)'
                : err.message,
        };
    }
}
