"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.EnumerationTypeDetailViewModelBase = void 0;
var utils_ml_field_helper_1 = require("./../../utils/utils-ml-field-helper");
var aurelia_router_1 = require("aurelia-router");
var enumeration_type_service_1 = require("./../../services/enumeration-type-service");
var aurelia_i18n_1 = require("aurelia-i18n");
var aurelia_framework_1 = require("aurelia-framework");
var breeze_client_1 = require("breeze-client");
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 enumeration_types_helper_1 = require("../../utils/enumeration-types-helper");
var enumeration_types_helper_2 = require("../../utils/enumeration-types-helper");
var detail_view_model_base_1 = require("../detail-view-model-base");
var editing_mode_enum_1 = require("../editing-mode-enum");
var EnumerationTypeDetailViewModelBase = exports.EnumerationTypeDetailViewModelBase = (function (_super) {
    __extends(EnumerationTypeDetailViewModelBase, _super);
    function EnumerationTypeDetailViewModelBase(router, logger, i18n, bindingEngine, skipMandatoryLanguageForDBInfo) {
        if (skipMandatoryLanguageForDBInfo === void 0) { skipMandatoryLanguageForDBInfo = false; }
        var _this = _super.call(this, router, logger) || this;
        _this.bindingEngine = bindingEngine;
        _this.skipMandatoryLanguageForDBInfo = skipMandatoryLanguageForDBInfo;
        _this.ressourceName = enumeration_types_helper_1.EnumerationTypeRessourceName;
        _this.showOrder = false;
        _this.hasClosedEntry = false;
        _super.prototype.initialize.call(_this, new service_base_1.ServiceBase(enumeration_types_helper_1.EnumerationTypeRessourceName));
        _this._enumerationTypeColorService = new service_base_1.ServiceBase(enumeration_types_helper_1.EnumerationTypeColorRessourceName);
        _this.showOrder = _this.config.enumerationTypeConfig.useOrder;
        _this._enumerationTypesEnum = _this.config.enumerationTypeConfig.enumerationTypes;
        return _this;
    }
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "isClosedEntry", {
        get: function () {
            var _a;
            return !((_a = this.entity) === null || _a === void 0 ? void 0 : _a.isEditable);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "enumerationTypeColorService", {
        get: function () {
            return this._enumerationTypeColorService;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "enumerationTypesEnum", {
        get: function () {
            return this._enumerationTypesEnum;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "category", {
        get: function () {
            return this._category;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "enumerationTypeConfig", {
        get: function () {
            var _this = this;
            var _a;
            return (_a = this.config.enumerationTypeConfig.enumerationTypeCategories) === null || _a === void 0 ? void 0 : _a.find(function (t) { return t.category == _this.category; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "ribbonHeaderText", {
        get: function () {
            if (this.enumerationTypesEnum) {
                return "enumerationtype." + this.setFirstLetterToLowerCase(this.enumerationTypesEnum[this.category].toString());
            }
            return "enumerationtype.enumerationtype";
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "documentTitle", {
        get: function () {
            var _a, _b;
            if (this.editingMode === editing_mode_enum_1.EditingModeEnum.Create) {
                if (this.enumerationTypesEnum) {
                    return this.i18n.tr("enumerationtype." + this.setFirstLetterToLowerCase(this.enumerationTypesEnum[this.category].toString()));
                }
                else {
                    return this.i18n.tr("enumerationtype.enumerationtype");
                }
            }
            else if (this.entity) {
                return (_b = (_a = this.entity) === null || _a === void 0 ? void 0 : _a.denomination) === null || _b === void 0 ? void 0 : _b._translation;
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "isUniqueCodeRequired", {
        get: function () {
            return this.config.enumerationTypeConfig.isUniqueCodeRequired;
        },
        enumerable: false,
        configurable: true
    });
    EnumerationTypeDetailViewModelBase.prototype.setFirstLetterToLowerCase = function (a) {
        return (a + '').charAt(0).toLowerCase() + a.substring(1);
    };
    EnumerationTypeDetailViewModelBase.prototype.activate = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var id, _a, _b, _c, applicationParameters;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        id = params.param1;
                        this._category = params.param2;
                        _super.prototype.initialize.call(this, new enumeration_type_service_1.EnumerationTypeService(this.category));
                        _a = this;
                        return [4, this.service.getCount(new breeze_client_1.Predicate('isEditable', breeze_client_1.FilterQueryOp.Equals, false))];
                    case 1:
                        _a.hasClosedEntry = (_d.sent()) > 0;
                        if (!(id == common_model_1.Various.NewId)) return [3, 3];
                        this.editingMode = editing_mode_enum_1.EditingModeEnum.Create;
                        _b = this;
                        return [4, this.service.createEntity({ isEditable: true })];
                    case 2:
                        _b.entity = _d.sent();
                        if (params.callback != null) {
                            params.callback(this.entity);
                        }
                        return [3, 5];
                    case 3:
                        this.editingMode = editing_mode_enum_1.EditingModeEnum.Update;
                        _c = this;
                        return [4, this.service.getEntityById(id)];
                    case 4:
                        _c.entity = _d.sent();
                        this.controller.addObject(this.entity);
                        _d.label = 5;
                    case 5:
                        if (!(this.applicationParametersService != null && false == this.skipMandatoryLanguageForDBInfo)) return [3, 7];
                        return [4, this.applicationParametersService.firstEntity(null, ['mandatoryLanguageForDBInfo'])];
                    case 6:
                        applicationParameters = _d.sent();
                        if (applicationParameters != null && applicationParameters.mandatoryLanguageForDBInfo != null) {
                            this.selectedLanguageCode = applicationParameters.mandatoryLanguageForDBInfo.uniqueCode.toLowerCase();
                        }
                        _d.label = 7;
                    case 7:
                        _super.prototype.activate.call(this, params);
                        (0, enumeration_types_helper_2.validateEnumerationType)(this.entity, this.isUniqueCodeRequired);
                        return [2];
                }
            });
        });
    };
    EnumerationTypeDetailViewModelBase.prototype.cancel = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.enumerationTypeColor = { id: null, text: null };
                        return [4, _super.prototype.cancel.call(this)];
                    case 1:
                        _a.sent();
                        return [2];
                }
            });
        });
    };
    EnumerationTypeDetailViewModelBase.prototype.attached = function () {
        var _this = this;
        this.disposables.push(this.bindingEngine.propertyObserver(this, 'enumerationTypeColor').subscribe(function (newValue) { return __awaiter(_this, void 0, void 0, function () {
            var enumerationTypeColor;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (newValue == null || isNaN(newValue)) {
                            return [2];
                        }
                        return [4, this.enumerationTypeColorService.firstEntity(new breeze_client_1.Predicate("id", breeze_client_1.FilterQueryOp.Equals, newValue))];
                    case 1:
                        enumerationTypeColor = _a.sent();
                        this.entity.textColor = enumerationTypeColor.textColor;
                        this.entity.backgroundColor = enumerationTypeColor.backgroundColor;
                        return [2];
                }
            });
        }); }));
    };
    EnumerationTypeDetailViewModelBase.prototype.activeChanged = function () {
        this.entity.default = this.entity.default && this.entity.active;
    };
    Object.defineProperty(EnumerationTypeDetailViewModelBase.prototype, "tagLabel", {
        get: function () {
            var _this = this;
            var _a;
            if (this.selectedLanguageCode) {
                var language = (_a = this.config.globalConfig.configuredLanguages) === null || _a === void 0 ? void 0 : _a.find(function (l) { return l.code == _this.selectedLanguageCode; });
                if (language) {
                    return this.entity.denomination['lang' + language.columnIndex];
                }
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    EnumerationTypeDetailViewModelBase.prototype.beforeSave = function () {
        return __awaiter(this, void 0, void 0, function () {
            var mlInputBeforeSave, _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _a = false == this.config.enumerationTypeConfig.completeTranslatedFields;
                        if (_a) return [3, 2];
                        return [4, utils_ml_field_helper_1.UtilsMlFieldHelper.BeforeSave(this.entity, [
                                { prop: this.entity.denomination, label: 'enumerationtype.denomination', isRequired: true }
                            ])];
                    case 1:
                        _a = (_c.sent());
                        _c.label = 2;
                    case 2:
                        mlInputBeforeSave = _a;
                        _b = mlInputBeforeSave;
                        if (!_b) return [3, 4];
                        return [4, _super.prototype.beforeSave.call(this)];
                    case 3:
                        _b = (_c.sent());
                        _c.label = 4;
                    case 4: return [2, _b];
                }
            });
        });
    };
    __decorate([
        aurelia_framework_1.observable,
        __metadata("design:type", Object)
    ], EnumerationTypeDetailViewModelBase.prototype, "enumerationTypeColor", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('entity.isEditable'),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "isClosedEntry", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('_enumerationTypeColorService'),
        __metadata("design:type", service_base_1.ServiceBase),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "enumerationTypeColorService", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('_enumerationTypesEnum'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "enumerationTypesEnum", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('_category'),
        __metadata("design:type", Number),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "category", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('_category'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "enumerationTypeConfig", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('category'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "ribbonHeaderText", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('category', 'editingMode', 'entity.denomination._translation'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "documentTitle", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)('config.enumerationTypeConfig.isUniqueCodeRequired'),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], EnumerationTypeDetailViewModelBase.prototype, "isUniqueCodeRequired", null);
    EnumerationTypeDetailViewModelBase = __decorate([
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [aurelia_router_1.Router, custom_logger_1.CustomLogger, aurelia_i18n_1.I18N, aurelia_framework_1.BindingEngine, Boolean])
    ], EnumerationTypeDetailViewModelBase);
    return EnumerationTypeDetailViewModelBase;
}(detail_view_model_base_1.EntityDetailViewModelBase));

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