"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ffiFnMapping = void 0;
const logger_1 = __importDefault(require("../../logger"));
const fs = require("fs");
const types_1 = require("./types");
const url_1 = require("url");
const DEFAULT_TIMEOUT = 30000;
const objArrayToStringArray = (obj) => {
    return obj.map((o) => JSON.stringify(o));
};
exports.ffiFnMapping = {
    pactffiVerifierAddCustomHeader: {
        validateAndExecute(ffi, handle, options) {
            if (options.customProviderHeaders) {
                if (options.customProviderHeaders) {
                    Object.entries(options.customProviderHeaders).forEach(([key, value]) => {
                        ffi.pactffiVerifierAddCustomHeader(handle, key, value);
                    });
                }
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierAddDirectorySource: {
        validateAndExecute(ffi, handle, options) {
            const messages = [];
            if (options.pactUrls) {
                options.pactUrls.forEach((file) => {
                    logger_1.default.debug(`checking source type of given pactUrl: ${file}`);
                    try {
                        const u = new url_1.URL(file);
                        if (u.hostname) {
                            logger_1.default.debug(`adding ${file} as a Url source`);
                            ffi.pactffiVerifierUrlSource(handle, file, options.pactBrokerUsername ||
                                process.env.PACT_BROKER_USERNAME ||
                                '', options.pactBrokerPassword ||
                                process.env.PACT_BROKER_PASSWORD ||
                                '', options.pactBrokerToken || process.env.PACT_BROKER_TOKEN || '');
                        }
                    }
                    catch {
                        messages.push(`${file} is not a valid URL`);
                    }
                    try {
                        const f = fs.lstatSync(file);
                        if (f.isDirectory()) {
                            logger_1.default.debug(`adding ${file} as Directory source`);
                            ffi.pactffiVerifierAddDirectorySource(handle, file);
                        }
                        else if (f.isFile() || f.isSymbolicLink()) {
                            logger_1.default.debug(`adding ${file} as File source`);
                            ffi.pactffiVerifierAddFileSource(handle, file);
                        }
                    }
                    catch {
                        messages.push(`'${file}' does not exist, or is not a file or directory`);
                    }
                });
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            if (messages.length > 0) {
                return { status: types_1.FnValidationStatus.FAIL, messages };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierBrokerSourceWithSelectors: {
        validateAndExecute(ffi, handle, opts) {
            const brokerUrl = opts.pactBrokerUrl || process.env.PACT_BROKER_BASE_URL;
            if (brokerUrl && opts.provider) {
                ffi.pactffiVerifierBrokerSourceWithSelectors(handle, brokerUrl, opts.pactBrokerUsername || process.env.PACT_BROKER_USERNAME || '', opts.pactBrokerPassword || process.env.PACT_BROKER_PASSWORD || '', opts.pactBrokerToken || process.env.PACT_BROKER_TOKEN || '', opts.enablePending || false, opts.includeWipPactsSince || '', opts.providerVersionTags || [], opts.providerBranch || '', opts.consumerVersionSelectors
                    ? objArrayToStringArray(opts.consumerVersionSelectors)
                    : [], opts.consumerVersionTags || []);
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierSetConsumerFilters: {
        validateAndExecute(ffi, handle, options) {
            if (options.consumerFilters && options.consumerFilters.length > 0) {
                ffi.pactffiVerifierSetConsumerFilters(handle, options.consumerFilters);
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierSetFilterInfo: {
        validateAndExecute(ffi, handle, options) {
            if (process.env.PACT_DESCRIPTION ||
                process.env.PACT_PROVIDER_STATE ||
                process.env.PACT_PROVIDER_NO_STATE) {
                const filterDescription = process.env.PACT_DESCRIPTION || '';
                const filterState = process.env.PACT_PROVIDER_STATE || '';
                const filterNoState = process.env.PACT_PROVIDER_NO_STATE ? true : false;
                ffi.pactffiVerifierSetFilterInfo(handle, filterDescription, filterState, filterNoState);
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierSetProviderInfo: {
        validateAndExecute(ffi, handle, options) {
            const uri = new url_1.URL(options.providerBaseUrl);
            ffi.pactffiVerifierSetProviderInfo(handle, options.provider || '', uri.protocol.split(':')[0], uri.hostname, parseInt(uri.port, 10), uri.pathname);
            return { status: types_1.FnValidationStatus.SUCCESS };
        },
    },
    pactffiVerifierSetProviderState: {
        validateAndExecute(ffi, handle, options) {
            if (options.providerStatesSetupUrl) {
                ffi.pactffiVerifierSetProviderState(handle, options.providerStatesSetupUrl, true, true);
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierSetPublishOptions: {
        validateAndExecute(ffi, handle, options) {
            if (options.publishVerificationResult && options.providerVersion) {
                ffi.pactffiVerifierSetPublishOptions(handle, options.providerVersion, options.buildUrl || '', options.providerVersionTags || [], options.providerBranch || '');
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
    pactffiVerifierSetVerificationOptions: {
        validateAndExecute(ffi, handle, opts) {
            if (opts.disableSslVerification || opts.timeout) {
                ffi.pactffiVerifierSetVerificationOptions(handle, opts.disableSslVerification || false, opts.timeout || DEFAULT_TIMEOUT);
                return { status: types_1.FnValidationStatus.SUCCESS };
            }
            return { status: types_1.FnValidationStatus.IGNORE };
        },
    },
};
//# sourceMappingURL=arguments.js.map