"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIDataSource = void 0;
var ui_internal_1 = require("../../../utils/ui-internal");
var UIDataSource = (function () {
    function UIDataSource(data, options, dateUtils) {
        if (options === void 0) { options = {}; }
        var _this = this;
        this.dateUtils = dateUtils;
        this.isPaginated = false;
        this.sortByOrder = "asc";
        this.data = [];
        this.original = [];
        this.options = {
            rootProperty: "",
            dataProperty: "data",
            pageProperty: "pageno",
            countProperty: "total",
            sortProperty: "sortBy",
            orderProperty: "sortOrder",
            perPageProperty: "pageCount"
        };
        this.deboucePerformFilter = ui_internal_1.UIInternal.debounce(function () {
            _this.performFilter();
        }, 200);
        this.options = __assign(__assign({}, this.options), options);
        this.sortByProperty = options.defaultSortProperty;
        this.sortByOrder = options.defaultSortOrder || "asc";
        this.disposables = [];
        this.filter = options.filter || null;
        if (isArray(data)) {
            this.original = data;
            this.listenForDataReordering();
            this.performFilter();
        }
    }
    UIDataSource.prototype.listenForDataReordering = function () {
        var _this = this;
        this.dispose();
        this.original.forEach(function (d) { return _this.disposables.push(ui_internal_1.UIInternal.observe(d, _this.sortByProperty, function () {
            _this.deboucePerformFilter();
        })); });
    };
    UIDataSource.prototype.dispose = function () {
        var _a;
        (_a = this.disposables) === null || _a === void 0 ? void 0 : _a.forEach(function (d) { return d.dispose(); });
        this.disposables.splice(0);
    };
    UIDataSource.prototype.sortBy = function (property, order) {
        if (this.sortByProperty === property) {
            this.sortByOrder = order !== null && order !== void 0 ? order : (this.sortByOrder === "asc" ? "desc" : "asc");
        }
        else {
            this.sortByProperty = property;
            this.sortByOrder = order !== null && order !== void 0 ? order : "asc";
            this.listenForDataReordering();
        }
        this.performFilter();
    };
    UIDataSource.prototype.performFilter = function () {
        var _a;
        var data = __spreadArray([], __read(this.original), false);
        if (this.filter) {
            data = data.filter(this.filter);
        }
        if (this.sortByProperty) {
            data = this.sortData(data);
        }
        this.data.splice(0);
        (_a = this.data).push.apply(_a, __spreadArray([], __read(data), false));
    };
    UIDataSource.prototype.sortData = function (data) {
        var _this = this;
        data = data.sort(function (a, b) {
            var aValue = ui_internal_1.UIInternal.getValue(a, _this.sortByProperty);
            var bValue = ui_internal_1.UIInternal.getValue(b, _this.sortByProperty);
            var comparison;
            if (_this.isDateValue(aValue) || _this.isDateValue(bValue)) {
                comparison = _this.dateUtils.isBefore(aValue, bValue) ? -1 : 1;
            }
            else if (_this.isNumberValue(aValue) && _this.isNumberValue(bValue)) {
                comparison = parseFloat(aValue) < parseFloat(bValue) ? -1 : 1;
            }
            else {
                comparison = ((aValue === null || aValue === void 0 ? void 0 : aValue.toString().toLowerCase()) || '').localeCompare((bValue === null || bValue === void 0 ? void 0 : bValue.toString().toLowerCase()) || '');
            }
            return _this.sortByOrder === 'desc' ? comparison * -1 : comparison;
        });
        return data;
    };
    UIDataSource.prototype.isDateValue = function (value) {
        return value != null && isDate(value);
    };
    UIDataSource.prototype.isNumberValue = function (value) {
        return value != null && !isNaN(value) && isDecimal(value);
    };
    return UIDataSource;
}());
exports.UIDataSource = UIDataSource;

//# sourceMappingURL=ui-data-source.js.map
