var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/// <reference path="../../base/BaseClass.ts" />
/// <reference path="../../util/ArrayUtils.ts" />
/// <reference path="./IDefaultProcessorConfigOptions.ts" />
/// <reference path="./ICleanupProcessor.ts" />
/// <reference path="../ITouchpoint.ts" />
var econda;
(function (econda) {
    var tpm;
    (function (tpm) {
        var cleanup;
        (function (cleanup) {
            var ArrayUtils = econda.util.ArrayUtils;
            /**
             * Default cleanup processor. Removes outdated items from touchpoint store.
             * There's no reason to use this class directly.
             *
             * @class econda.tpm.cleanup.DefaultProcessor
             */
            var DefaultProcessor = (function (_super) {
                __extends(DefaultProcessor, _super);
                function DefaultProcessor(cfg) {
                    _super.call(this);
                    /**
                     * Max age of touchpoints in days
                     * @cfg {Number} [maxAge=90]
                     * @accessor
                     */
                    this._maxAge = 90;
                    /**
                     * Max total number of stored touchpoints
                     * @cfg {Number} [maxItems=100]
                     * @accessor
                     */
                    this._maxItems = 100;
                    /**
                     * Filter touchpoints to include in cleanup process
                     * @cfg {Function} filter
                     * @accessor
                     */
                    this._filter = null;
                    if (typeof cfg !== 'undefined') {
                        if (cfg instanceof DefaultProcessor) {
                            return cfg;
                        }
                        this.initConfig(cfg);
                    }
                }
                DefaultProcessor.prototype.getMaxAge = function () {
                    return this._maxAge;
                };
                DefaultProcessor.prototype.setMaxAge = function (days) {
                    this._maxAge = days;
                    return this;
                };
                DefaultProcessor.prototype.getMaxItems = function () {
                    return this._maxItems;
                };
                DefaultProcessor.prototype.setMaxItems = function (numberOfItems) {
                    this._maxItems = numberOfItems;
                    return this;
                };
                DefaultProcessor.prototype.getFilter = function () {
                    return this._filter;
                };
                DefaultProcessor.prototype.setFilter = function (filterFunction) {
                    this._filter = filterFunction;
                    return this;
                };
                /**
                 * Do cleanup on provided array of touchpoints
                 * @method
                 */
                DefaultProcessor.prototype.clean = function (touchpoints) {
                    var filteredItems = ArrayUtils.filter(touchpoints, this._filter);
                    // maxAge
                    var minDate = new Date();
                    minDate.setDate(minDate.getDate() - this._maxAge);
                    ArrayUtils.remove(touchpoints, ArrayUtils.remove(filteredItems, function (tp) { return tp.timestamp < minDate; }) // true = do remove
                    );
                    // maxItems
                    ArrayUtils.remove(touchpoints, filteredItems.slice(0, this._maxItems * -1));
                };
                return DefaultProcessor;
            }(econda.base.BaseClass));
            cleanup.DefaultProcessor = DefaultProcessor;
        })(cleanup = tpm.cleanup || (tpm.cleanup = {}));
    })(tpm = econda.tpm || (econda.tpm = {}));
})(econda || (econda = {}));