"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnumerationTypeListViewModelBase = void 0;
var aurelia_router_1 = require("aurelia-router");
var breeze_client_1 = require("breeze-client");
var aurelia_i18n_1 = require("aurelia-i18n");
var list_view_is_selected_coldef_1 = require("../list-view-is-selected-coldef");
var aurelia_framework_1 = require("aurelia-framework");
var service_base_1 = require("../../data/service-base");
var common_model_1 = require("../../model/common-model");
var custom_logger_1 = require("../../modules/custom-logger");
var list_view_model_base_1 = require("../list-view-model-base");
var enumeration_types_helper_1 = require("../../utils/enumeration-types-helper");
var enumeration_category_filter_1 = require("../../resources/filters/enumeration-category-filter");
var enumeration_category_floating_filter_1 = require("../../resources/filters/enumeration-category-floating-filter");
var EnumerationTypeListViewModelBase = exports.EnumerationTypeListViewModelBase = (function (_super) {
    __extends(EnumerationTypeListViewModelBase, _super);
    function EnumerationTypeListViewModelBase(router, logger, i18n) {
        var _this = this;
        var _a, _b;
        _this = _super.call(this, router, logger, new service_base_1.ServiceBase(enumeration_types_helper_1.EnumerationTypeRessourceName)) || this;
        _this.ressourceName = enumeration_types_helper_1.EnumerationTypeRessourceName;
        _this.enumerationTypes = (_b = (_a = _this.config.enumerationTypeConfig.enumerationTypeCategories) === null || _a === void 0 ? void 0 : _a.sort(function (a, b) { return _this.i18n.tr(a.name).localeCompare(_this.i18n.tr(b.name)); })) !== null && _b !== void 0 ? _b : [];
        _this.showOrder = _this.config.enumerationTypeConfig.useOrder;
        _this.listByCategory = _this.config.enumerationTypeConfig.listByCategory;
        _this.showTag = _this.config.enumerationTypeConfig.showTag;
        return _this;
    }
    EnumerationTypeListViewModelBase.prototype.setEnumerationTypes = function (enumerationTypesEnum, types) {
        var _this = this;
        this.enumerationTypes = types.map(function (t) {
            return Object.assign({}, t, { name: _this.i18n.tr("enumerationtype." + _this.setFirstLetterToLowerCase(enumerationTypesEnum[t.category])) });
        });
        this.enumerationTypes = this.enumerationTypes.sort(function (a, b) { return a.name.localeCompare(b.name); });
        this.initializeGridOptions();
    };
    EnumerationTypeListViewModelBase.prototype.activate = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var category_1;
            var _this = this;
            return __generator(this, function (_a) {
                if (this.listByCategory) {
                    category_1 = params.param2;
                    if (category_1 != null && !isNaN(category_1)) {
                        this.currentCategory = category_1;
                        this.service.gridDataSource.getPredicatesFunction = function () { return new breeze_client_1.Predicate('category', breeze_client_1.FilterQueryOp.Equals, category_1); };
                    }
                }
                else {
                    this.service.gridDataSource.getPredicatesFunction = function () {
                        var _a;
                        var predicates = (_a = _this.enumerationTypes) === null || _a === void 0 ? void 0 : _a.map(function (enumerationType) { return new breeze_client_1.Predicate('category', breeze_client_1.FilterQueryOp.Equals, enumerationType.category); });
                        return (predicates === null || predicates === void 0 ? void 0 : predicates.length) > 0 ? breeze_client_1.Predicate.or(predicates) : null;
                    };
                }
                if (this.gridOptions && this.onlyClosedEnums) {
                    this.gridOptions.columnDefs = this.getDataGridColumns();
                }
                this.initializeGridOptions();
                return [2];
            });
        });
    };
    Object.defineProperty(EnumerationTypeListViewModelBase.prototype, "menuItems", {
        get: function () {
            var _this = this;
            var _a;
            if (this.listByCategory || ((_a = this.enumerationTypes) === null || _a === void 0 ? void 0 : _a.length) == 1) {
                return [];
            }
            return this.enumerationTypes
                .filter(function (x) { return false == x.isClosed; })
                .map(function (x) {
                return {
                    label: _this.i18n.tr(x.name),
                    handler: function () { return _this.addEnumerationType(x); },
                };
            });
        },
        enumerable: false,
        configurable: true
    });
    EnumerationTypeListViewModelBase.prototype.setFirstLetterToLowerCase = function (a) {
        return (a + '').charAt(0).toLowerCase() + a.substring(1);
    };
    EnumerationTypeListViewModelBase.prototype.getEnumerationTypeConfig = function (category) {
        var _a;
        return (_a = this.enumerationTypes) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.category == category; });
    };
    EnumerationTypeListViewModelBase.prototype.currentCategoryIsClosed = function () {
        var _a;
        return (_a = this.getEnumerationTypeConfig(this.currentCategory)) === null || _a === void 0 ? void 0 : _a.isClosed;
    };
    EnumerationTypeListViewModelBase.prototype.isClosedEnum = function (entity) {
        var _a;
        return (_a = this.getEnumerationTypeConfig(entity === null || entity === void 0 ? void 0 : entity.category)) === null || _a === void 0 ? void 0 : _a.isClosed;
    };
    Object.defineProperty(EnumerationTypeListViewModelBase.prototype, "onlyClosedEnums", {
        get: function () {
            var _a;
            return ((_a = this.enumerationTypes) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.isClosed == false; })) == null;
        },
        enumerable: false,
        configurable: true
    });
    EnumerationTypeListViewModelBase.prototype.isSelectable = function (entity) {
        return false == this.isClosedEnum(entity) && (entity === null || entity === void 0 ? void 0 : entity.isEditable) == true;
    };
    EnumerationTypeListViewModelBase.prototype.getRouteDetail = function (category, id) {
        var _a;
        var route = (_a = this.getEnumerationTypeConfig(category).route) !== null && _a !== void 0 ? _a : "enumeration-types/";
        return route + "".concat(category, "/").concat(id);
    };
    EnumerationTypeListViewModelBase.prototype.getDetailsUrl = function (self, entity) {
        if (entity && entity.id) {
            return this.getRouteDetail(entity.category, entity.id);
        }
        return null;
    };
    EnumerationTypeListViewModelBase.prototype.addEnumerationType = function (enumerationTypeCategory) {
        if (this.listByCategory) {
            this.navigateTo(this.getRouteDetail(this.currentCategory, -100));
        }
        else if (enumerationTypeCategory != null) {
            this.navigateTo(this.getRouteDetail(enumerationTypeCategory.category, -100));
        }
        else {
            this.navigateTo(this.getRouteDetail(this.enumerationTypes[0].category, -100));
        }
    };
    Object.defineProperty(EnumerationTypeListViewModelBase.prototype, "canAdd", {
        get: function () {
            var _a, _b;
            if (this.listByCategory) {
                return (_b = !((_a = this.getEnumerationTypeConfig(this.currentCategory)) === null || _a === void 0 ? void 0 : _a.isClosed)) !== null && _b !== void 0 ? _b : false;
            }
            return this.enumerationTypes && this.enumerationTypes.length == 1 && !this.onlyClosedEnums;
        },
        enumerable: false,
        configurable: true
    });
    EnumerationTypeListViewModelBase.prototype.getDataGridColumns = function () {
        var _this = this;
        var isSelectedCol = (0, list_view_is_selected_coldef_1.getSelectedColDef)(this);
        if (this.onlyClosedEnums) {
            isSelectedCol.headerComponent = null;
        }
        var defs = [
            isSelectedCol
        ];
        if (!this.listByCategory) {
            defs.push({
                headerName: this.i18n.tr("general.category"),
                field: 'category',
                type: common_model_1.FieldType.String,
                valueGetter: function (params) { var _a, _b; return _this.i18n.tr((_b = _this.getEnumerationTypeConfig((_a = params.data) === null || _a === void 0 ? void 0 : _a.category)) === null || _b === void 0 ? void 0 : _b.name); },
                sortable: true,
                filter: enumeration_category_filter_1.EnumerationCategoryFilter,
                filterParams: {
                    categories: this.enumerationTypes
                },
                floatingFilterComponent: enumeration_category_floating_filter_1.EnumerationCategoryFloatingFilter,
                floatingFilterComponentParams: {
                    categories: this.enumerationTypes
                }
            });
        }
        if (this.showTag) {
            defs.push({
                headerName: '',
                field: 'denomination._translation',
                floatingFilterComponentParams: {
                    suppressFilterButton: true,
                },
                filterParams: {
                    textColor: 'textColor',
                    backgroundColor: 'backgroundColor'
                },
                cellRenderer: "tagRenderer"
            });
        }
        defs.push.apply(defs, [
            {
                headerName: this.i18n.tr("enumerationtype.uniqueCode"),
                field: "uniqueCode",
                type: common_model_1.FieldType.String
            },
            {
                headerName: this.i18n.tr("enumerationtype.isEditable"),
                field: "isEditable",
                type: common_model_1.FieldType.Boolean
            },
            {
                headerName: this.i18n.tr("enumerationtype.denomination") + " " + this.i18n.tr("general.inBracketsFR"),
                field: "denomination.lang1",
                type: common_model_1.FieldType.String
            },
            {
                headerName: this.i18n.tr("enumerationtype.denomination") + " " + this.i18n.tr("general.inBracketsNL"),
                field: "denomination.lang2",
                type: common_model_1.FieldType.String
            },
            {
                headerName: this.i18n.tr("enumerationtype.denomination") + " " + this.i18n.tr("general.inBracketsEN"),
                field: "denomination.lang3",
                type: common_model_1.FieldType.String
            }
        ]);
        if (this.showOrder) {
            defs.push({
                headerName: this.i18n.tr("enumerationtype.order"),
                field: 'order',
                type: common_model_1.FieldType.Number,
            });
        }
        defs.push.apply(defs, [
            {
                headerName: this.i18n.tr("enumerationtype.active"),
                field: "active",
                type: common_model_1.FieldType.Boolean
            },
            {
                headerName: this.i18n.tr("enumerationtype.default"),
                field: "default",
                type: common_model_1.FieldType.Boolean
            },
            {
                headerName: this.i18n.tr("general.createdTime"),
                field: "createdTime",
                hide: true,
                type: common_model_1.FieldType.Date
            },
            {
                headerName: this.i18n.tr("general.updatedTime"),
                field: "updatedTime",
                hide: true,
                type: common_model_1.FieldType.Date
            },
            {
                headerName: this.i18n.tr("general.createdByFullName"),
                field: "createdByFullName",
                hide: true,
                type: common_model_1.FieldType.String
            },
            {
                headerName: this.i18n.tr("general.updatedByFullName"),
                field: "updatedByFullName",
                hide: true,
                type: common_model_1.FieldType.String
            }
        ]);
        return defs;
    };
    __decorate([
        (0, aurelia_framework_1.computedFrom)('enumerationTypes.length'),
        __metadata("design:type", Array),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeListViewModelBase.prototype, "menuItems", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('enumerationTypes.length', 'currentCategory', 'listByCategory'),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeListViewModelBase.prototype, "canAdd", null);
    EnumerationTypeListViewModelBase = __decorate([
        aurelia_framework_1.autoinject,
        (0, aurelia_framework_1.useView)("./../../vm/enumeration-types/enumeration-type-list-view-model-base.html"),
        __metadata("design:paramtypes", [aurelia_router_1.Router, custom_logger_1.CustomLogger, aurelia_i18n_1.I18N])
    ], EnumerationTypeListViewModelBase);
    return EnumerationTypeListViewModelBase;
}(list_view_model_base_1.ListViewModelBase));

//# sourceMappingURL=enumeration-type-list-view-model-base.js.map
