"use strict";
/**
 * Split Feature Flag
 *
 *  Copyright (C) 2010-2019 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 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/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SplitFeatureFlag = void 0;
/**
 * Global feature flag for Split
 */
var SplitFeatureFlag = /** @class */ (function () {
    /**
     * Initialize FeatureFlag
     */
    function SplitFeatureFlag(_default, _base_conditions, _client) {
        if (_base_conditions === void 0) { _base_conditions = {}; }
        this._default = _default;
        this._base_conditions = _base_conditions;
        this._client = _client;
    }
    /**
     * Close the Split connection
     */
    SplitFeatureFlag.prototype.close = function () {
        this._client.destroy();
    };
    /**
     * Look up a feature flag by key
     *
     * @param key        - the key to lookup
     * @param conditions - optional conditions to specify
     *
     * @return whether or not this feature flag is enabled
     */
    SplitFeatureFlag.prototype.isEnabled = function (key, conditions) {
        var _this = this;
        if (conditions === void 0) { conditions = {}; }
        return new Promise(function (resolve) {
            // Add base conditions but don't override passed in values
            var atts = _this._base_conditions;
            Object.keys(conditions).forEach(function (cond_key) {
                atts[cond_key] = conditions[cond_key];
            });
            _this._client
                .getTreatment('' + atts.user || '', key, atts)
                .then(function (treatment) {
                if (treatment === 'on') {
                    resolve(true);
                    return;
                }
                else if (treatment === 'off') {
                    resolve(false);
                    return;
                }
                resolve(_this._default.isEnabled(key));
            });
        });
    };
    return SplitFeatureFlag;
}());
exports.SplitFeatureFlag = SplitFeatureFlag;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaW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