"use strict";
/**
 *  RaterFactory
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDummySession = exports.validate_session_template = exports.AsyncRaterFactory = exports.RateError = void 0;
/**
 * An error that occurred during rating
 */
var RateError = /** @class */ (function (_super) {
    __extends(RateError, _super);
    function RateError(message) {
        var _newTarget = this.constructor;
        var _this = _super.call(this, message) || this;
        Object.setPrototypeOf(_this, _newTarget.prototype);
        _this.name = RateError.name;
        return _this;
    }
    return RateError;
}(Error));
exports.RateError = RateError;
/**
 * Generates async raters on demand
 *
 * Wrap a rater, which is callback-based in this abstraction to use promises.
 */
var AsyncRaterFactory = /** @class */ (function () {
    /**
     * Create a new AsyncRaterFactory
     *
     * @param rater - path to rater
     */
    function AsyncRaterFactory(rater) {
        this.rater = rater;
    }
    /**
     * Create rater
     *
     * @return rater
     */
    AsyncRaterFactory.prototype.createRater = function () {
        /* eslint-disable @typescript-eslint/no-var-requires */
        var rater = require(this.rater);
        /* eslint-enable @typescript-eslint/no-var-requires */
        var alias = '';
        return {
            rate: function (quote, session) {
                return __awaiter(this, void 0, void 0, function () {
                    var on_failure;
                    return __generator(this, function (_a) {
                        on_failure = function (message) {
                            throw new RateError(message);
                        };
                        return [2 /*return*/, new Promise(function (resolve) {
                                rater.rate(quote, session, alias, resolve, on_failure);
                            })];
                    });
                });
            },
        };
    };
    return AsyncRaterFactory;
}());
exports.AsyncRaterFactory = AsyncRaterFactory;
/**
 * Determine if a session template contains proper fields
 *
 * @param template - hopefully a dummy session template
 *
 * @throws if any fields are not properly set
 */
exports.validate_session_template = function (template) {
    template = template !== null && template !== void 0 ? template : {};
    var missing = [
        'agentId',
        'agentName',
        'agentEntityId',
        'agencyNumber',
    ].filter(function (field) {
        return template[field] === undefined;
    });
    if (missing.length) {
        throw new Error("Missing session data for the following fields: " + missing.join(', '));
    }
};
/**
 * Get a dummy session
 */
exports.getDummySession = function (template) {
    return {
        agentId: function () {
            return template.agentId;
        },
        agentName: function () {
            return template.agentName;
        },
        agencyNumber: function () {
            return template.agencyNumber;
        },
        agentEntityId: function () {
            return template.agentEntityId;
        },
        isInternal: function () {
            return true;
        },
        userName: function () {
            return 'Dullahan';
        },
        getData: function () {
            return {};
        },
        isLoggedIn: function () {
            return false;
        },
    };
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUmF0ZXJGYWN0b3J5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiUmF0ZXJGYWN0b3J5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW1CRzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQWFIOztHQUVHO0FBQ0g7SUFBK0IsNkJBQUs7SUFDbEMsbUJB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