"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 };
    }
};
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.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IncompleteMultilingualField = void 0;
var custom_logger_1 = require("./../../modules/custom-logger");
var service_base_1 = require("./../../data/service-base");
var aurelia_router_1 = require("aurelia-router");
var aurelia_framework_1 = require("aurelia-framework");
var toastr = __importStar(require("toastr"));
var aurelia_dialog_1 = require("aurelia-dialog");
var utils_ml_field_1 = require("../utils-ml-field");
var view_model_base_1 = require("../../vm/view-model-base");
var constants_1 = require("../../constants/constants");
var translated_property_helper_1 = require("../translated-property-helper");
var IncompleteMultilingualField = exports.IncompleteMultilingualField = (function (_super) {
    __extends(IncompleteMultilingualField, _super);
    function IncompleteMultilingualField(router, logger, dialogController) {
        var _this = _super.call(this, router, logger) || this;
        _this.dialogController = dialogController;
        _this.ribbonHeaderText = _this.translate("incompletemlfield.missingTranslations");
        _this.ressourceName = "";
        _this.languages = [];
        _this.dialogTitle = _this.translate('incompletemlfield.missingTranslations');
        _this.autoFillBlanksLabel = _this.translate('incompletemlfield.autoFillBlanks');
        _this.languageService = new service_base_1.ServiceBase(constants_1.EntityTypeNames.Language, null, constants_1.EntityTypeNames.ResourceNames.Language);
        return _this;
    }
    IncompleteMultilingualField.prototype.translate = function (key) {
        return this.i18n.i18next.exists(key) ?
            this.i18n.tr(key) : this.i18n.tr(key, { ns: 'common' });
    };
    IncompleteMultilingualField.prototype.activate = function (params) {
        var _a;
        return __awaiter(this, void 0, void 0, function () {
            var _b, _c;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        if (params.callback)
                            params.callback(this);
                        this.mlProps = params.mlProps;
                        _b = this;
                        if (!((_a = params.languages) !== null && _a !== void 0)) return [3, 1];
                        _c = _a;
                        return [3, 3];
                    case 1: return [4, this.languageService.getAllEntities()];
                    case 2:
                        _c = _d.sent();
                        _d.label = 3;
                    case 3:
                        _b.languages = _c;
                        this.warnMsg = "<p>" + this.translate("incompletemlfield.mlInputNotComplete") + "</p>";
                        this.warnMsg = this.warnMsg.replaceAll("{br}", "</p><p>");
                        return [2];
                }
            });
        });
    };
    IncompleteMultilingualField.prototype.autoFillBlanks = function () {
        var e_1, _a;
        var _this = this;
        var noTr = this.mlProps.find(function (x) { return _this.languages.every(function (l) { return !utils_ml_field_1.UtilsMlField.isFilled(l, x); }); });
        if (noTr) {
            return toastr.error(this.translate("incompletemlfield.atLeastOneTrOn") + this.i18n.tr(noTr.label));
        }
        var _loop_1 = function (mlProp) {
            var filledLang = this_1.languages.find(function (l) { return utils_ml_field_1.UtilsMlField.isFilled(l, mlProp); });
            this_1.languages
                .filter(function (l) { return !utils_ml_field_1.UtilsMlField.isFilled(l, mlProp); })
                .forEach(function (l) { return mlProp.prop[utils_ml_field_1.UtilsMlField.getLanguageColumnName(l)] = mlProp.prop[utils_ml_field_1.UtilsMlField.getLanguageColumnName(filledLang)]; });
        };
        var this_1 = this;
        try {
            for (var _b = __values(this.mlProps), _c = _b.next(); !_c.done; _c = _b.next()) {
                var mlProp = _c.value;
                _loop_1(mlProp);
            }
        }
        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; }
        }
    };
    IncompleteMultilingualField.prototype.validate = function () {
        var _this = this;
        this.mlProps.forEach(function (ml) { ml.prop = translated_property_helper_1.TranslatedPropertyHelper.createMissingProperty(ml.prop); });
        var missingTr = this.mlProps.find(function (x) { return _this.languages.some(function (l) { return !utils_ml_field_1.UtilsMlField.isFilled(l, x); }); });
        if (missingTr) {
            return toastr.error(this.translate("incompletemlfield.missingTrOn") + this.i18n.tr(missingTr.label));
        }
        this.dialogController.ok(true);
    };
    IncompleteMultilingualField.prototype.close = function () {
        this.dialogController.cancel();
    };
    IncompleteMultilingualField = __decorate([
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [aurelia_router_1.Router, custom_logger_1.CustomLogger, aurelia_dialog_1.DialogController])
    ], IncompleteMultilingualField);
    return IncompleteMultilingualField;
}(view_model_base_1.ViewModelBase));

//# sourceMappingURL=incomplete-multilingual-field.js.map
