"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTrackingProviders = void 0;
const tracking_spi_1 = require("./tracking_spi");
const trackingProviderProxy_1 = __importDefault(require("./trackingProviderProxy"));
const console_tracking_provider_1 = require("./console_tracking_provider");
const segment_tracking_provider_1 = require("./segment_tracking_provider");
const posthog_tracking_provider_1 = require("./posthog_tracking_provider");
const umami_tracking_provider_1 = require("./umami_tracking_provider");
const getTrackingProviders = (initProps) => {
    const providers = [];
    if (initProps.activeProviders) {
        let tmpProps = initProps.activeProviders;
        // Theoretically we get an array of provider names, but it could also be a CSV string...
        if (!Array.isArray(initProps.activeProviders)) {
            const tmpString = initProps.activeProviders;
            if (tmpString && tmpString.indexOf(',') !== -1) {
                tmpProps = tmpString.split(',');
            }
            else {
                tmpProps = [tmpString];
            }
        }
        tmpProps.forEach((provider) => {
            switch (tracking_spi_1.Providers[provider]) {
                case tracking_spi_1.Providers.Segment:
                    providers.push(new segment_tracking_provider_1.SegmentTrackingProvider());
                    break;
                case tracking_spi_1.Providers.Umami:
                    providers.push(new umami_tracking_provider_1.UmamiTrackingProvider());
                    break;
                case tracking_spi_1.Providers.Posthog:
                    providers.push(new posthog_tracking_provider_1.PosthogTrackingProvider());
                    break;
                case tracking_spi_1.Providers.Console:
                    providers.push(new console_tracking_provider_1.ConsoleTrackingProvider());
                    break;
                case tracking_spi_1.Providers.None: // Do nothing, just a placeholder
                    break;
                default:
                    if (providers.length > 1) {
                        if (initProps.verbose) {
                            // eslint-disable-next-line no-console
                            console.error("Unknown provider '" + provider);
                        }
                    }
                    break;
            }
        });
    }
    // Initialize them
    for (const provider of providers) {
        try {
            provider.initialize(initProps);
        }
        catch (e) {
            // eslint-disable-next-line no-console
            console.error(e);
        }
    }
    return new trackingProviderProxy_1.default(providers);
};
exports.getTrackingProviders = getTrackingProviders;
exports.default = exports.getTrackingProviders;
