/// <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 ProvisionLast = (function () { function ProvisionLast(data) { // example // [100] => 100% to first touchpoint // [.8, .2] => 80% for first and 20% for second touchpoint this._rules = []; this._touchpoints = data.touchpoints; } ProvisionLast.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++) { var tpIndex = this._touchpoints.length - 1 - i; var ruleIndex = applyableRules.length - 1 - i; currentValue = this._touchpoints[tpIndex].share || 0; this._touchpoints[tpIndex].share = currentValue + (applyableRules[ruleIndex] * percentageOfOrderValueToProvision); } }; ProvisionLast.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 */ ProvisionLast.prototype._getApplyableRules = function () { if (this._touchpoints.length >= this._rules.length) { return this._rules; } var ret = []; var numberOfTouchpoints = this._touchpoints.length; var numberOfRules = this._rules.length; var unusedValue = 0; // sum of unused shares for (var i = 0; i < (numberOfRules - numberOfTouchpoints); i++) { unusedValue += this._rules[i]; } var factorRemainingValuesToOne = 1 / (1 - unusedValue); for (var n = numberOfRules - numberOfTouchpoints; n < numberOfRules; n++) { ret.push(this._rules[n] * factorRemainingValuesToOne); } return ret; }; ProvisionLast.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; }; ProvisionLast.prototype._toNumber = function (input) { return 1 * (typeof input === 'string' ? input.replace(/[^0-9]/g, '') : input); }; ProvisionLast.FUNCTION_NAME = 'provisionLast'; return ProvisionLast; }()); partials.ProvisionLast = ProvisionLast; })(partials = provisioning.partials || (provisioning.partials = {})); })(provisioning = tpm.provisioning || (tpm.provisioning = {})); })(tpm = econda.tpm || (econda.tpm = {})); })(econda || (econda = {}));