/// <reference path="../../../util/ArrayUtils.ts" />
/// <reference path="../../ITouchpoint.ts" />
/// <reference path="./IConstructorArgument.ts" />
var econda;
(function (econda) {
    var tpm;
    (function (tpm) {
        var provisioning;
        (function (provisioning) {
            var partials;
            (function (partials) {
                /**
                 * Use only first touchpoint. Removes all other touchpoints from collection
                 *
                 * @class econda.tpm.provisioning.partials.OnlyCurrentSession
                 */
                var ProvisionFirst = (function () {
                    function ProvisionFirst(data) {
                        // example
                        // [100] => 100% to first touchpoint
                        // [.8, .2] => 80% for first and 20% for second touchpoint
                        this._rules = [];
                        this._touchpoints = data.touchpoints;
                    }
                    ProvisionFirst.prototype.apply = function (rules, options) {
                        if (rules === void 0) { rules = 1; }
                        if (options === void 0) { options = null; }
                        if (this._touchpoints.length == 0) {
                            return;
                        }
                        this._rules = this._readRulesArgument(rules);
                        var options = this._readOptionsArgument(options);
                        var percentageOfOrderValueToProvision = options.use;
                        var applyableRules = this._getApplyableRules();
                        var currentValue;
                        for (var i = 0; i < applyableRules.length; i++) {
                            currentValue = this._touchpoints[i].share || 0;
                            this._touchpoints[i].share = currentValue + (applyableRules[i] * percentageOfOrderValueToProvision);
                        }
                    };
                    ProvisionFirst.prototype._readOptionsArgument = function (options) {
                        var options = options || {};
                        var ret = {};
                        if (typeof options.use === 'undefined') {
                            ret.use = 1;
                        }
                        else {
                            var userInputOrderValueToProvisionInPercent = this._toNumber(options.use);
                            if (userInputOrderValueToProvisionInPercent > 1) {
                                ret.use = userInputOrderValueToProvisionInPercent / 100;
                            }
                        }
                        return ret;
                    };
                    /**
                     * If number of rules is greater than the number of available touchpoints, we're unable to apply this rule.
                     * This functions recalculates the rules to match the number of available touchpoints.
                     *
                     * @returns {number[]}
                     * @private
                     */
                    ProvisionFirst.prototype._getApplyableRules = function () {
                        if (this._touchpoints.length >= this._rules.length) {
                            return this._rules;
                        }
                        var ret = [];
                        var numberOfTouchpoints = this._touchpoints.length;
                        var unusedValue = 0;
                        for (var i = numberOfTouchpoints; i < this._rules.length; i++) {
                            unusedValue += this._rules[i];
                        }
                        var factorRemainingValuesToOne = 1 / (1 - unusedValue);
                        for (var n = 0; n < numberOfTouchpoints; n++) {
                            ret.push(this._rules[n] * factorRemainingValuesToOne);
                        }
                        return ret;
                    };
                    ProvisionFirst.prototype._readRulesArgument = function (rules) {
                        var ret = [];
                        if (typeof rules === 'number' || typeof rules === 'string') {
                            // one parameter => number of touchpoints to provision. Give all the same.
                            var numberOfTouchpoints = this._toNumber(rules);
                            var itemValue = 1 / numberOfTouchpoints;
                            for (var i = 0; i < rules; i++) {
                                ret.push(itemValue);
                            }
                            return ret;
                        }
                        if (typeof rules === 'object' && typeof rules.length !== 'undefined') {
                            var sum = 0;
                            var tmp = [];
                            for (var i = 0; i < rules.length; i++) {
                                tmp.push(this._toNumber(rules[i]));
                                sum += tmp[i];
                            }
                            var factor = 1 / sum;
                            for (var n = 0; n < tmp.length; n++) {
                                ret.push(tmp[n] * factor);
                            }
                        }
                        return ret;
                    };
                    ProvisionFirst.prototype._toNumber = function (input) {
                        return 1 * (typeof input === 'string' ? input.replace(/[^0-9]/g, '') : input);
                    };
                    ProvisionFirst.FUNCTION_NAME = 'provisionFirst';
                    return ProvisionFirst;
                }());
                partials.ProvisionFirst = ProvisionFirst;
            })(partials = provisioning.partials || (provisioning.partials = {}));
        })(provisioning = tpm.provisioning || (tpm.provisioning = {}));
    })(tpm = econda.tpm || (econda.tpm = {}));
})(econda || (econda = {}));