import yaml from 'js-yaml';
import path from 'path';
import fs from 'fs';
import { dirname } from '../utils/utils.js';
import { getPublicConfig } from './sync/config/config.service.js';
import { loadStandardConfig } from './nango-config.service.js';
import { errorManager } from '../index.js';
import { stringifyError } from '@nangohq/utils';
class FlowService {
    // TODO: cache this
    getAllAvailableFlows() {
        try {
            const flowPath = path.join(dirname(import.meta.url), '../../flows.yaml');
            const flows = yaml.load(fs.readFileSync(flowPath).toString());
            if (flows === undefined || !('integrations' in flows) || Object.keys(flows.integrations).length <= 0) {
                throw new Error('empty_flows');
            }
            return flows;
        }
        catch (err) {
            errorManager.report(`failed_to_find_flows, ${stringifyError(err)}`);
            return {};
        }
    }
    // TODO: cache this
    getAllAvailableFlowsAsStandardConfig() {
        const config = this.getAllAvailableFlows();
        const { integrations: allIntegrations } = config;
        const standardConfig = [];
        for (const providerConfigKey in allIntegrations) {
            const integrations = allIntegrations[providerConfigKey];
            const { models, rawName, ...flow } = integrations;
            const nangoConfig = {
                integrations: {
                    [providerConfigKey]: flow
                },
                models: models
            };
            const { success, response } = loadStandardConfig(nangoConfig);
            if (success && response) {
                if (rawName) {
                    const responseWithRaw = response.map((standardConfigItem) => {
                        return { ...standardConfigItem, rawName };
                    });
                    standardConfig.push(...responseWithRaw);
                }
                else {
                    standardConfig.push(...response);
                }
            }
        }
        return standardConfig;
    }
    getFlowByIntegrationAndName({ provider, type, scriptName }) {
        const availablePublicFlows = this.getAllAvailableFlowsAsStandardConfig();
        const flows = availablePublicFlows.filter((flow) => flow.providerConfigKey === provider);
        if (flows.length <= 0 || !flows[0]) {
            return null;
        }
        const flow = flows[0][`${type}s`].find((flow) => flow.name === scriptName);
        return flow || null;
    }
    getFlow(name) {
        const integrations = this.getAllAvailableFlowsAsStandardConfig();
        for (const integration of integrations) {
            for (const syncs of integration.syncs) {
                if (syncs.name === name) {
                    return syncs;
                }
            }
            for (const actions of integration.actions) {
                if (actions.name === name) {
                    return actions;
                }
            }
        }
        return null;
    }
    getSingleFlowAsStandardConfig(name) {
        const integrations = this.getAllAvailableFlowsAsStandardConfig();
        let standardConfig = {};
        for (const integration of integrations) {
            for (const syncs of integration.syncs) {
                if (syncs.name === name) {
                    standardConfig = integration;
                    standardConfig.syncs = [syncs];
                    standardConfig.actions = [];
                    return standardConfig;
                }
            }
            for (const actions of integration.actions) {
                if (actions.name === name) {
                    standardConfig = integration;
                    standardConfig.actions = [actions];
                    standardConfig.syncs = [];
                    return standardConfig;
                }
            }
        }
        return null;
    }
    async getAddedPublicFlows(environmentId) {
        return getPublicConfig(environmentId);
    }
}
export default new FlowService();
//# sourceMappingURL=flow.service.js.map