import { ArtifactStore } from '@pellux/goodvibes-sdk/platform/artifacts/index';
import { ConfigManager } from '../config/manager.js';
import { ServiceRegistry } from '../config/service-registry.js';
import { createDiscordDeliveryStrategy, createGoogleChatDeliveryStrategy, createNtfyDeliveryStrategy, createSlackDeliveryStrategy, createTelegramDeliveryStrategy, createWebControlPlaneDeliveryStrategy, createWebhookDeliveryStrategy, } from './delivery/strategies-core.js';
import { createBlueBubblesDeliveryStrategy, createIMessageDeliveryStrategy, createSignalDeliveryStrategy, createWhatsAppDeliveryStrategy, } from './delivery/strategies-bridge.js';
import { createMSTeamsDeliveryStrategy, createMattermostDeliveryStrategy, createMatrixDeliveryStrategy, } from './delivery/strategies-enterprise.js';
import { resolveChannelDeliverySurfaceKind } from './delivery/shared.js';
export { resolveChannelDeliverySurfaceKind } from './delivery/shared.js';
export function createDefaultChannelDeliveryStrategies(configManager, serviceRegistry, artifactStore, getControlPlaneGateway) {
    return [
        createWebhookDeliveryStrategy(configManager, artifactStore),
        createSlackDeliveryStrategy(serviceRegistry, configManager, artifactStore),
        createDiscordDeliveryStrategy(serviceRegistry, configManager, artifactStore),
        createNtfyDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createWebControlPlaneDeliveryStrategy(configManager, artifactStore, getControlPlaneGateway),
        createTelegramDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createGoogleChatDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createSignalDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createWhatsAppDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createIMessageDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createMSTeamsDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createBlueBubblesDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createMattermostDeliveryStrategy(configManager, serviceRegistry, artifactStore),
        createMatrixDeliveryStrategy(configManager, serviceRegistry, artifactStore),
    ];
}
export class ChannelDeliveryRouter {
    strategies;
    controlPlaneGateway;
    constructor(config = {}) {
        this.controlPlaneGateway = config.controlPlaneGateway ?? null;
        if (config.strategies) {
            this.strategies = [...config.strategies];
            return;
        }
        if (!config.configManager || !config.serviceRegistry || !config.artifactStore) {
            throw new Error('ChannelDeliveryRouter requires configManager, serviceRegistry, and artifactStore when using builtin delivery strategies.');
        }
        this.strategies = createDefaultChannelDeliveryStrategies(config.configManager, config.serviceRegistry, config.artifactStore, () => this.controlPlaneGateway);
    }
    setControlPlaneGateway(gateway) {
        this.controlPlaneGateway = gateway;
    }
    listStrategies() {
        return [...this.strategies];
    }
    registerStrategy(strategy, options = {}) {
        const existingIndex = this.strategies.findIndex((entry) => entry.id === strategy.id);
        if (existingIndex >= 0) {
            if (!options.replace) {
                throw new Error(`Channel delivery strategy already registered: ${strategy.id}`);
            }
            this.strategies.splice(existingIndex, 1, strategy);
            return;
        }
        this.strategies.push(strategy);
    }
    unregisterStrategy(strategyId) {
        const existingIndex = this.strategies.findIndex((entry) => entry.id === strategyId);
        if (existingIndex < 0)
            return false;
        this.strategies.splice(existingIndex, 1);
        return true;
    }
    async deliver(request) {
        const strategy = this.strategies.find((entry) => entry.canHandle(request));
        if (!strategy) {
            const surfaceKind = resolveChannelDeliverySurfaceKind(request.target);
            throw new Error(`Unsupported channel delivery target: ${request.target.kind}:${surfaceKind ?? 'unknown'}`);
        }
        const result = await strategy.deliver(request);
        return result.responseId;
    }
}
