"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.getVersionHistory = getVersionHistory;
exports.getVersionSnapshot = getVersionSnapshot;
exports.getYamlContent = getYamlContent;
const yamlDb = __importStar(require("../../services/yaml-workflow/db"));
const helpers_1 = require("./helpers");
/**
 * Retrieve the version history for a YAML workflow.
 *
 * Returns a paginated list of version snapshots ordered by version number.
 *
 * @param input.id — UUID of the workflow
 * @param input.limit — max versions to return (defaults to 20)
 * @param input.offset — pagination offset (defaults to 0)
 * @returns `{ status: 200, data: VersionSnapshot[] }` paginated version history
 */
async function getVersionHistory(input) {
    try {
        const limit = input.limit ?? 20;
        const offset = input.offset ?? 0;
        const result = await yamlDb.getVersionHistory(input.id, limit, offset);
        return { status: 200, data: result };
    }
    catch (err) {
        if ((0, helpers_1.isNotFoundError)(err)) {
            return { status: 404, error: 'YAML workflow not found' };
        }
        return { status: 500, error: err.message };
    }
}
/**
 * Retrieve a specific version snapshot of a YAML workflow.
 *
 * Validates that the version number is a positive integer before querying.
 *
 * @param input.id — UUID of the workflow
 * @param input.version — 1-based version number to retrieve
 * @returns `{ status: 200, data: VersionSnapshot }` the snapshot at the requested version, or 404
 */
async function getVersionSnapshot(input) {
    try {
        const version = input.version;
        if (isNaN(version) || version < 1) {
            return { status: 400, error: 'Invalid version number' };
        }
        const snapshot = await yamlDb.getVersionSnapshot(input.id, version);
        if (!snapshot) {
            return { status: 404, error: `Version ${version} not found` };
        }
        return { status: 200, data: snapshot };
    }
    catch (err) {
        if ((0, helpers_1.isNotFoundError)(err)) {
            return { status: 404, error: 'YAML workflow not found' };
        }
        return { status: 500, error: err.message };
    }
}
/**
 * Retrieve the raw YAML content for a workflow, optionally at a specific version.
 *
 * When a version is provided, fetches from the version snapshot table. Otherwise
 * returns the current yaml_content from the live workflow record.
 *
 * @param input.id — UUID of the workflow
 * @param input.version — optional version number; when omitted, returns the current content
 * @returns `{ status: 200, data: string }` the raw YAML string
 */
async function getYamlContent(input) {
    try {
        if (input.version) {
            const snapshot = await yamlDb.getVersionSnapshot(input.id, input.version);
            if (!snapshot) {
                return { status: 404, error: `Version ${input.version} not found` };
            }
            return { status: 200, data: snapshot.yaml_content };
        }
        const wf = await yamlDb.getYamlWorkflow(input.id);
        if (!wf) {
            return { status: 404, error: 'YAML workflow not found' };
        }
        return { status: 200, data: wf.yaml_content };
    }
    catch (err) {
        if ((0, helpers_1.isNotFoundError)(err)) {
            return { status: 404, error: 'YAML workflow not found' };
        }
        return { status: 500, error: err.message };
    }
}
