import configService from '../../../services/config.service.js';
import environmentService from '../../../services/environment.service.js';
import webhookService from '../../../services/notification/webhook.service.js';
import telemetry, { LogTypes } from '../../../utils/telemetry.js';
import { LogActionEnum } from '../../../models/Activity.js';
import { internalNango } from './internal-nango.js';
import { getLogger } from '@nangohq/utils';
import * as webhookHandlers from './index.js';
const logger = getLogger('Webhook.Manager');
const handlers = webhookHandlers;
async function execute(environmentUuid, providerConfigKey, headers, body, rawBody, logContextGetter) {
    if (!body) {
        return;
    }
    const provider = await configService.getProviderName(providerConfigKey);
    const integration = await configService.getProviderConfigByUuid(providerConfigKey, environmentUuid);
    if (!provider || !integration) {
        return;
    }
    const accountId = await environmentService.getAccountIdFromEnvironment(integration.environment_id);
    const handler = handlers[`${provider.replace(/-/g, '')}Webhook`];
    let res = undefined;
    try {
        if (handler) {
            res = await handler(internalNango, integration, headers, body, rawBody, logContextGetter);
        }
    }
    catch (e) {
        logger.error(`error processing incoming webhook for ${providerConfigKey} - `, e);
        await telemetry.log(LogTypes.INCOMING_WEBHOOK_FAILED_PROCESSING, 'Incoming webhook failed processing', LogActionEnum.WEBHOOK, {
            accountId: String(accountId),
            environmentId: String(integration.environment_id),
            provider: integration.provider,
            providerConfigKey: integration.unique_key,
            payload: JSON.stringify(body),
            error: String(e)
        });
    }
    const webhookBodyToForward = res?.parsedBody || body;
    const connectionIds = res?.connectionIds || [];
    await webhookService.forward(integration.environment_id, providerConfigKey, connectionIds, provider, webhookBodyToForward, headers, logContextGetter);
    await telemetry.log(LogTypes.INCOMING_WEBHOOK_PROCESSED_SUCCESSFULLY, 'Incoming webhook was processed successfully', LogActionEnum.WEBHOOK, {
        accountId: String(accountId),
        environmentId: String(integration.environment_id),
        provider: integration.provider,
        providerConfigKey: integration.unique_key,
        payload: JSON.stringify(webhookBodyToForward)
    });
    if (res) {
        return res.acknowledgementResponse;
    }
}
export default execute;
//# sourceMappingURL=webhook.manager.js.map