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);
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { FieldType, UserPreFilter } from './../../../model/common-model';
import { autoinject, bindable, bindingMode, customElement } from "aurelia-framework";
export var SummaryFilter = (function () {
    function SummaryFilter() {
        this.FieldType = FieldType;
        this.maxShowedFilter = 3;
    }
    SummaryFilter.prototype.gridOptionsChanged = function (newValue, oldValue) {
        var _this = this;
        this.gridOptions.onFilterChanged = function (event) {
            _this.filterCurrent();
            if (_this.afterFilterChanged != null) {
                _this.afterFilterChanged(event);
            }
        };
    };
    SummaryFilter.prototype.resetAllFilter = function () {
        var e_1, _a;
        if (this.currentSelectedFilters.length > 0) {
            try {
                for (var _b = __values(this.currentSelectedFilters), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var currentSelectedFilter = _c.value;
                    if (currentSelectedFilter.instance.resetFilter != null) {
                        currentSelectedFilter.instance.resetFilter();
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                }
                finally { if (e_1) throw e_1.error; }
            }
        }
    };
    SummaryFilter.prototype.showAll = function (filter) {
        filter.show = true;
    };
    SummaryFilter.prototype.filterCurrent = function () {
        var _a, _b, _c;
        (_a = this.currentSelectedFilters) !== null && _a !== void 0 ? _a : (this.currentSelectedFilters = new Array());
        this.currentSelectedFilters.splice(0);
        var filters = (_c = (_b = this.gridOptions) === null || _b === void 0 ? void 0 : _b.api) === null || _c === void 0 ? void 0 : _c.getFilterModel();
        if (filters != null) {
            var _loop_1 = function (filter) {
                var colDef = this_1.gridOptions.columnDefs.find(function (y) { return y.field == filter; });
                this_1.createCurrentSelectedFilter(colDef, filters[filter]);
            };
            var this_1 = this;
            for (var filter in filters) {
                _loop_1(filter);
            }
        }
    };
    SummaryFilter.prototype.createCurrentSelectedFilter = function (colDef, value) {
        var _a, _b;
        if (colDef.filter !== false && colDef.type != null && colDef.type != FieldType.Base) {
            var currentSelectedFilter = new CurrentSelectedFilter();
            currentSelectedFilter.columnName = colDef.headerName;
            currentSelectedFilter.type = colDef.type;
            currentSelectedFilter.instance = (_b = (_a = this.gridOptions) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.getFilterInstance(colDef.field);
            var value_1 = currentSelectedFilter.instance.getValue();
            switch (currentSelectedFilter.type) {
                case FieldType.String:
                case FieldType.Html:
                case FieldType.Number:
                case FieldType.Currency:
                case FieldType.Date:
                    currentSelectedFilter.value = value_1;
                    break;
                case FieldType.Boolean:
                    currentSelectedFilter.value = value_1 === null || value_1 === void 0 ? void 0 : value_1.toString();
                    break;
                case FieldType.Enum:
                    var translationFirstPart_1 = colDef.floatingFilterComponentParams.category + '.';
                    currentSelectedFilter.enumValues = value_1;
                    currentSelectedFilter.enumValues.forEach(function (x) {
                        x.name = translationFirstPart_1 + x.key;
                    });
                    break;
                case FieldType.User:
                    if (Array.isArray(value_1)) {
                        currentSelectedFilter.valueSelected = value_1;
                    }
                    else {
                        switch (value_1) {
                            case UserPreFilter.Me:
                                currentSelectedFilter.value = "common:grid.me";
                                break;
                            case UserPreFilter.NotMe:
                                currentSelectedFilter.value = "common:grid.notMe";
                                break;
                        }
                    }
                    break;
                case FieldType.Enumeration:
                case FieldType.OneToMany:
                case FieldType.ManyToMany:
                    currentSelectedFilter.valueSelected = value_1;
                    break;
                default:
                    break;
            }
            this.currentSelectedFilters.push(currentSelectedFilter);
        }
    };
    SummaryFilter.prototype.beforeCloseFn = function (value, filter) {
        var _a, _b;
        if (filter.type != null && filter.type != FieldType.Base) {
            var instance = filter.instance;
            instance.removeFilterValue(value);
            instance.setModel(instance.getModel());
            (_b = (_a = this.gridOptions) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.onFilterChanged();
        }
    };
    SummaryFilter.prototype.unselectEmptyFields = function (filter) {
        var _a, _b;
        var instance = filter.instance;
        if (filter.type == "Date") {
            instance.filterHelper.isDateNullable = false;
        }
        else if (instance.filterHelper) {
            instance.filterHelper.displayEmptyFields = false;
        }
        else {
            instance.displayEmptyFields = false;
        }
        instance.setModel(instance.getModel());
        (_b = (_a = this.gridOptions) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.onFilterChanged();
    };
    __decorate([
        bindable({ bindingMode: bindingMode.fromView }),
        __metadata("design:type", Object)
    ], SummaryFilter.prototype, "gridOptions", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.fromView }),
        __metadata("design:type", Number)
    ], SummaryFilter.prototype, "entitiesCount", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.fromView }),
        __metadata("design:type", Object)
    ], SummaryFilter.prototype, "usedView", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.fromView }),
        __metadata("design:type", Number)
    ], SummaryFilter.prototype, "maxShowedFilter", void 0);
    __decorate([
        bindable({ bindingMode: bindingMode.fromView }),
        __metadata("design:type", Function)
    ], SummaryFilter.prototype, "afterFilterChanged", void 0);
    SummaryFilter = __decorate([
        autoinject,
        customElement("summary-filter")
    ], SummaryFilter);
    return SummaryFilter;
}());
var CurrentSelectedFilter = (function () {
    function CurrentSelectedFilter() {
        this.show = false;
    }
    return CurrentSelectedFilter;
}());

//# sourceMappingURL=summary-filter.js.map
