"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberFilterHelper = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_i18n_1 = require("aurelia-i18n");
var NumberFilterHelper = exports.NumberFilterHelper = (function () {
    function NumberFilterHelper(setModel) {
        this.setModel = setModel;
        this.type = 'equals';
        this.displayEmptyFields = false;
        this.i18n = aurelia_framework_1.Container.instance.get(aurelia_i18n_1.I18N);
    }
    NumberFilterHelper.prototype.isFilterActive = function () {
        return !this.fromNumberIsEmpty() || !this.toNumberIsEmpty() || this.displayEmptyFields;
    };
    NumberFilterHelper.prototype.fromNumberChanged = function (newVal, oldVal) {
        if (newVal !== oldVal) {
            this.initModel();
        }
    };
    NumberFilterHelper.prototype.toNumberChanged = function (newVal, oldVal) {
        if (newVal !== oldVal) {
            this.initModel();
        }
    };
    NumberFilterHelper.prototype.typeCheckedChanged = function (checked) {
        var oldValue = this.type;
        this.type = (checked ? 'equals' : 'inRange');
        if (this.type !== oldValue) {
            this.initModel();
        }
    };
    NumberFilterHelper.prototype.initModel = function () {
        if (this.fromNumberIsEmpty() && this.toNumberIsEmpty() && !this.displayEmptyFields) {
            this.setModel({ 'filterType': 'number', type: this.type, displayEmptyFields: this.displayEmptyFields });
        }
        else {
            this.setModel({ 'filterType': 'number', type: this.type, 'filter': this.fromNumber, 'filterTo': this.toNumber, displayEmptyFields: this.displayEmptyFields });
        }
    };
    NumberFilterHelper.prototype.displayEmptyFieldsChanged = function () {
        var model = { filterType: 'number', type: this.type, displayEmptyFields: this.displayEmptyFields };
        if (!this.fromNumberIsEmpty() || !this.toNumberIsEmpty()) {
            Object.assign(model, { filter: this.fromNumber, filterTo: this.toNumber });
        }
        this.setModel(model);
    };
    NumberFilterHelper.prototype.fromNumberIsEmpty = function () {
        return this.fromNumber == null || this.fromNumber.toString().trim() == '';
    };
    NumberFilterHelper.prototype.toNumberIsEmpty = function () {
        return this.toNumber == null || this.toNumber.toString().trim() == '';
    };
    NumberFilterHelper.prototype.resetFilter = function () {
        var _a;
        (_a = this.type) !== null && _a !== void 0 ? _a : (this.type = 'equals');
        this.fromNumber = this.toNumber = null;
        this.displayEmptyFields = false;
    };
    NumberFilterHelper.prototype.setupContentGui = function () {
        return "<div class=\"floating-filter__body__number\">\n            <div class=\"ui-check__wrapper\">\n                <ui-checkbox checked.to-view=\"filterHelper.type == 'equals'\" on-change.call=\"filterHelper.typeCheckedChanged(checked)\">${ 'grid.equals' | t }</ui-checkbox>\n            </div>\n            <template if.bind=\"filterHelper.type == 'equals'\">\n                <ui-field t=\"[label]common:grid.value\">\n                    <ui-input type=\"number\" value.bind=\"filterHelper.fromNumber & debounce:500\" clear></ui-input>\n                </ui-field>\n            </template>\n            <template else>\n                <ui-field t=\"[label]common:grid.from\">\n                    <ui-input type=\"number\" value.bind=\"filterHelper.fromNumber & debounce:500\" clear></ui-input>\n                </ui-field>\n                <ui-field t=\"[label]common:grid.to\">\n                    <ui-input type=\"number\" value.bind=\"filterHelper.toNumber & debounce:500\" clear></ui-input>\n                </ui-field>\n            </template>\n            <div class=\"ui-check__wrapper\" if.bind=\"showEmptyFieldsCheckbox\">\n                <ui-checkbox checked.bind=\"filterHelper.displayEmptyFields\">${ 'grid.displayEmptyFields' | t }</ui-checkbox>\n            </div>\n        </div>";
    };
    NumberFilterHelper.prototype.setupContentGuiSideFilter = function () {
        return "\n            <ui-field label.bind=\"params.colDef.headerName\"></ui-field>\n            <div class=\"number-filter-input\">\n                <div class=\"col s12 m6 l6\">\n                    <ui-field t=\"[label]common:grid.from\">\n                    <ui-input type=\"number\" value.bind=\"filterHelper.fromNumber & debounce:500\"></ui-input>\n                    </ui-field>\n                </div>\n                <div class=\"col s12 m6 l6\">\n                    <ui-field t=\"[label]common:grid.to\">\n                        <ui-input type=\"number\" value.bind=\"filterHelper.toNumber & debounce:500\"></ui-input>\n                    </ui-field>\n                </div>\n            </div>";
    };
    NumberFilterHelper.prototype.getValue = function () {
        if (this.isFilterActive()) {
            if (this.type == 'equals') {
                return this.fromNumber.toString();
            }
            else if (this.toNumberIsEmpty()) {
                return '≥ ' + this.fromNumber.toString();
            }
            else if (this.fromNumberIsEmpty()) {
                return '≤ ' + this.toNumber.toString();
            }
            else {
                return this.fromNumber.toString() + ' ' + this.i18n.tr('grid.to', { ns: "common" }) + ' ' + this.toNumber.toString();
            }
        }
        return null;
    };
    NumberFilterHelper.prototype.getModelAsString = function () {
        var _a;
        return (_a = this.getValue()) !== null && _a !== void 0 ? _a : this.i18n.tr('grid.noFilter', { ns: "common" });
    };
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", Number)
    ], NumberFilterHelper.prototype, "fromNumber", void 0);
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", Number)
    ], NumberFilterHelper.prototype, "toNumber", void 0);
    return NumberFilterHelper;
}());

//# sourceMappingURL=number-filter-helper.js.map
