/*
 * 1DS JS SDK POST plugin, 4.4.0-nightlybeta3.2601-11
 * Copyright (c) Microsoft and contributors. All rights reserved.
 * (Microsoft Internal Only)
 */
/**
* KillSwitch.ts
* @author Abhilash Panwar (abpanwar)
* @copyright Microsoft 2018
*/
import { arrForEach, dateNow, strTrim } from "@microsoft/1ds-core-js";
import { _DYN_LENGTH, _DYN_PUSH, _DYN_SPLIT } from "./__DynamicConstants";
var SecToMsMultiplier = 1000;
function _normalizeTenants(values) {
    var result = [];
    if (values) {
        arrForEach(values, function (value) {
            result[_DYN_PUSH /* @min:%2epush */](strTrim(value));
        });
    }
    return result;
}
/**
 * Factory function to create a KillSwitch instance.
 * @returns A new IKillSwitch instance.
 * @internal
 */
export function createKillSwitch() {
    var _killedTokenDictionary = {};
    return {
        setKillSwitchTenants: function (killTokens, killDuration) {
            if (killTokens && killDuration) {
                try {
                    var killedTokens = _normalizeTenants(killTokens[_DYN_SPLIT /* @min:%2esplit */](","));
                    if (killDuration === "this-request-only") {
                        return killedTokens;
                    }
                    var durationMs = parseInt(killDuration, 10) * SecToMsMultiplier;
                    for (var i = 0; i < killedTokens[_DYN_LENGTH /* @min:%2elength */]; ++i) {
                        _killedTokenDictionary[killedTokens[i]] = dateNow() + durationMs;
                    }
                }
                catch (ex) {
                    return [];
                }
            }
            return [];
        },
        isTenantKilled: function (tenantToken) {
            var killDictionary = _killedTokenDictionary;
            var name = strTrim(tenantToken);
            if (killDictionary[name] !== undefined && killDictionary[name] > dateNow()) {
                return true;
            }
            delete killDictionary[name];
            return false;
        }
    };
}
//# sourceMappingURL=KillSwitch.js.map