"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.flowsConfigEndpoint = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const flowsConfigEndpoint = (app, baseDir) => {
    const configPath = path_1.default.join(baseDir, 'motia-workbench.json');
    const getConfig = () => {
        if (fs_1.default.existsSync(configPath)) {
            return JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
        }
        return {};
    };
    app.post('/flows/:id/config', (req, res) => {
        const newFlowConfig = req.body;
        try {
            const existingConfig = getConfig();
            const updatedConfig = {
                ...existingConfig,
            };
            Object.entries(newFlowConfig).forEach(([flowName, filePathPositions]) => {
                updatedConfig[flowName] = {
                    ...(updatedConfig[flowName] || {}),
                    ...filePathPositions,
                };
            });
            fs_1.default.writeFileSync(configPath, JSON.stringify(updatedConfig, null, 2));
            res.status(200).send({ message: 'Flow config saved successfully' });
        }
        catch (error) {
            console.error('Error saving flow config:', error.message);
            res.status(500).json({ error: 'Failed to save flow config' });
        }
    });
    app.get('/flows/:id/config', (req, res) => {
        const { id } = req.params;
        try {
            const allFlowsConfig = getConfig();
            const flowConfig = allFlowsConfig[id] || {};
            res.status(200).send(flowConfig);
        }
        catch (error) {
            console.error('Error reading flow config:', error.message);
            res.status(400).send({ error: 'Failed to read flow config' });
        }
    });
};
exports.flowsConfigEndpoint = flowsConfigEndpoint;
