"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.nockTrackInternalEndpoint = void 0;
exports.getDefaultMappings = getDefaultMappings;
exports.testRunner = testRunner;
const nock_1 = __importDefault(require("nock"));
const mapValues_1 = __importDefault(require("lodash/mapValues"));
const actions_core_1 = require("@segment/actions-core");
const index_1 = __importDefault(require("./index"));
const utils_1 = require("./utils");
const testDestination = (0, actions_core_1.createTestIntegration)(index_1.default);
var EndpointType;
(function (EndpointType) {
    EndpointType["BATCH"] = "batch";
    EndpointType["SINGLE"] = "entity";
})(EndpointType || (EndpointType = {}));
const endpointByType = {
    [EndpointType.BATCH]: 'batch',
    [EndpointType.SINGLE]: 'entity'
};
const trackServiceByRegion = {
    [utils_1.AccountRegion.US]: (0, nock_1.default)('https://track.customer.io'),
    [utils_1.AccountRegion.EU]: (0, nock_1.default)('https://track-eu.customer.io')
};
function wrapFn(fn, type, region) {
    return () => {
        const settings = {
            siteId: '12345',
            apiKey: 'abcde',
            accountRegion: region
        };
        const action = async (name, args) => {
            const actionFn = {
                [EndpointType.BATCH]: testDestination.testBatchAction,
                [EndpointType.SINGLE]: testDestination.testAction
            }[type];
            if (type === EndpointType.BATCH) {
                args.events = [args.event];
            }
            const responses = (await actionFn?.call(testDestination, name, args));
            if (!responses.length) {
                if (type === EndpointType.BATCH) {
                    await testDestination.testAction(name, args);
                }
                throw new Error(`No responses received.`);
            }
            expect(responses.length).toBe(1);
            expect(responses[0].status).toBe(200);
            expect(responses[0].headers.toJSON()).toMatchObject({
                'content-type': 'application/json'
            });
            expect(responses[0].data).toMatchObject({});
            if (type === EndpointType.BATCH) {
                return responses[0].options.json.batch[0];
            }
            return responses[0].options.json;
        };
        return fn(settings, action);
    };
}
const nockTrackInternalEndpoint = (region) => trackServiceByRegion[region];
exports.nockTrackInternalEndpoint = nockTrackInternalEndpoint;
function getDefaultMappings(action) {
    const fields = testDestination.definition.actions[action].fields;
    const defaultMappings = (0, mapValues_1.default)(fields, 'default');
    return defaultMappings;
}
function testRunner(fn) {
    describe.each(Object.values(endpointByType))(`when using %s requests`, (type) => {
        describe.each(Object.values(utils_1.AccountRegion))(`when using the %s region`, (region) => {
            beforeEach(() => {
                trackServiceByRegion[region].post(`/api/v2/${type}`).reply(200, {});
            });
            afterEach(() => {
                nock_1.default.cleanAll();
            });
            return wrapFn(fn, type, region)();
        });
    });
}
//# sourceMappingURL=test-helper.js.map