"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateFlowsList = exports.flowsEndpoint = void 0;
const crypto_1 = require("crypto");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const guards_1 = require("./guards");
const get_step_language_1 = require("./get-step-language");
const flowsEndpoint = (lockedData, app) => {
    app.get('/flows', async (_, res) => {
        const list = (0, exports.generateFlowsList)(lockedData);
        res.status(200).send(list.map(({ id, name }) => ({ id, name })));
    });
    app.get('/flows/:id', async (req, res) => {
        const list = (0, exports.generateFlowsList)(lockedData);
        const { id } = req.params;
        const flow = list.find((flow) => flow.id === id);
        if (!flow) {
            res.status(404).send({ error: 'Flow not found' });
        }
        else {
            res.status(200).send(flow);
        }
    });
};
exports.flowsEndpoint = flowsEndpoint;
const generateFlowsList = (lockedData) => {
    return Object.entries(lockedData.flows).map(([flowId, flow]) => generateFlow(flowId, flow.steps));
};
exports.generateFlowsList = generateFlowsList;
// Helper functions
const getNodeComponentPath = (filePath) => {
    const filePathWithoutExtension = filePath.replace(/\.[^/.]+$/, '');
    const tsxPath = filePathWithoutExtension + '.tsx';
    return fs_1.default.existsSync(tsxPath) ? tsxPath : undefined;
};
const getRelativePath = (filePath) => {
    const baseDir = process.cwd();
    return path_1.default.relative(baseDir, filePath);
};
const createEdge = (sourceId, targetId, topic, label, variant, conditional) => ({
    id: `${sourceId}-${targetId}`,
    source: sourceId,
    target: targetId,
    data: {
        variant,
        label,
        topic,
        labelVariant: conditional ? 'conditional' : 'default',
    },
});
const processEmit = (emit) => {
    const isString = typeof emit === 'string';
    return {
        topic: isString ? emit : emit.topic,
        label: isString ? undefined : emit.label,
        conditional: isString ? undefined : emit.conditional,
    };
};
const createEdgesForEmits = (sourceStep, targetSteps, emits, variant) => {
    const edges = [];
    emits.forEach((emit) => {
        const { topic, label, conditional } = processEmit(emit);
        targetSteps.forEach((targetStep) => {
            if (targetStep.subscribes?.includes(topic)) {
                edges.push(createEdge(sourceStep.id, targetStep.id, topic, label, variant, conditional));
            }
        });
    });
    return edges;
};
const createBaseStepResponse = (step, id) => ({
    id,
    name: step.config.name,
    description: step.config.description,
    nodeComponentPath: getNodeComponentPath(step.filePath),
    filePath: getRelativePath(step.filePath),
    language: (0, get_step_language_1.getStepLanguage)(step.filePath),
});
const createApiStepResponse = (step, id) => {
    if (!(0, guards_1.isApiStep)(step)) {
        throw new Error('Attempted to create API step response with non-API step');
    }
    return {
        ...createBaseStepResponse(step, id),
        type: 'api',
        emits: step.config.emits,
        virtualEmits: step.config.virtualEmits ?? [],
        subscribes: step.config.virtualSubscribes ?? undefined,
        action: 'webhook',
        webhookUrl: `${step.config.method} ${step.config.path}`,
        bodySchema: step.config.bodySchema ?? undefined,
    };
};
const createEventStepResponse = (step, id) => {
    if (!(0, guards_1.isEventStep)(step)) {
        throw new Error('Attempted to create Event step response with non-Event step');
    }
    return {
        ...createBaseStepResponse(step, id),
        type: 'event',
        emits: step.config.emits,
        virtualEmits: step.config.virtualEmits ?? [],
        subscribes: step.config.subscribes,
    };
};
const createNoopStepResponse = (step, id) => {
    if (!(0, guards_1.isNoopStep)(step)) {
        throw new Error('Attempted to create Noop step response with non-Noop step');
    }
    return {
        ...createBaseStepResponse(step, id),
        type: 'noop',
        emits: [],
        virtualEmits: step.config.virtualEmits,
        subscribes: step.config.virtualSubscribes,
    };
};
const createCronStepResponse = (step, id) => {
    if (!(0, guards_1.isCronStep)(step)) {
        throw new Error('Attempted to create Cron step response with non-Cron step');
    }
    return {
        ...createBaseStepResponse(step, id),
        type: 'cron',
        emits: step.config.emits,
        cronExpression: step.config.cron,
    };
};
const createStepResponse = (step) => {
    const id = (0, crypto_1.randomUUID)();
    if ((0, guards_1.isApiStep)(step))
        return createApiStepResponse(step, id);
    if ((0, guards_1.isEventStep)(step))
        return createEventStepResponse(step, id);
    if ((0, guards_1.isNoopStep)(step))
        return createNoopStepResponse(step, id);
    if ((0, guards_1.isCronStep)(step))
        return createCronStepResponse(step, id);
    throw new Error(`Unknown step type for step: ${step.config.name}`);
};
const createEdgesForStep = (sourceStep, allSteps) => {
    const regularEdges = createEdgesForEmits(sourceStep, allSteps, sourceStep.emits, 'event');
    const virtualEdges = sourceStep.virtualEmits
        ? createEdgesForEmits(sourceStep, allSteps, sourceStep.virtualEmits, 'virtual')
        : [];
    return [...regularEdges, ...virtualEdges];
};
const generateFlow = (flowId, flowSteps) => {
    const steps = flowSteps.map(createStepResponse);
    const edges = steps.flatMap((step) => createEdgesForStep(step, steps));
    return { id: flowId, name: flowId, steps, edges };
};
