"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.verify = void 0;
var verifier_1 = require("../ffi/verifier");
var logger_1 = require("../logger");
var arguments_1 = require("./arguments");
var argumentMapper_1 = require("../ffi/argumentMapper");
var VERIFICATION_SUCCESSFUL = 0;
var VERIFICATION_FAILED = 1;
var INVALID_ARGUMENTS = 4;
var LOG_ENV_VAR_NAME = 'PACT_LOG_LEVEL';
var verify = function (opts) {
    var verifierLib = verifier_1.getVerifierLib();
    return new Promise(function (resolve, reject) {
        process.env[LOG_ENV_VAR_NAME] = opts.logLevel;
        verifierLib.pactffi_init(LOG_ENV_VAR_NAME);
        var request = argumentMapper_1.argumentMapper(arguments_1.argMapping, opts, arguments_1.ignoredArguments)
            .map(function (s) { return s.replace('\n', ''); })
            .join('\n');
        logger_1.default.debug('sending arguments to FFI:');
        logger_1.default.debug(request);
        verifierLib.pactffi_verify.async(request, function (err, res) {
            logger_1.default.debug("response from verifier: " + err + ", " + res);
            if (err) {
                if (typeof err === 'string') {
                    logger_1.default.error(err);
                }
                else if (err.message) {
                    logger_1.default.error(err.message);
                }
                logger_1.default.pactCrash('The underlying pact core returned an error through the ffi interface');
                reject(err);
            }
            else {
                switch (res) {
                    case VERIFICATION_SUCCESSFUL:
                        logger_1.default.info('Verification successful');
                        resolve("finished: " + res);
                        break;
                    case VERIFICATION_FAILED:
                        logger_1.default.error('Verification unsuccessful');
                        reject(new Error('Verfication failed'));
                        break;
                    case INVALID_ARGUMENTS:
                        logger_1.default.pactCrash('The underlying pact core was invoked incorrectly.');
                        reject(new Error('Verification was unable to run'));
                        break;
                    default:
                        logger_1.default.pactCrash('The underlying pact core crashed in an unexpected way.');
                        reject(new Error('Pact core crashed'));
                        break;
                }
            }
        });
    });
};
exports.verify = verify;
//# sourceMappingURL=nativeVerifier.js.map