"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);
};
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 __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIEntityHistory = exports.EVT_REFRESH_HISTORY = void 0;
var aurelia_binding_1 = require("aurelia-binding");
var picture_helper_1 = require("./../files/picture-helper");
var aurelia_router_1 = require("aurelia-router");
var constants_1 = require("./../../constants/constants");
var aurelia_i18n_1 = require("aurelia-i18n");
var aurelia_fetch_client_1 = require("aurelia-fetch-client");
var aurelia_framework_1 = require("aurelia-framework");
var moment_1 = __importDefault(require("moment"));
var service_base_1 = require("../../data/service-base");
var data_context_1 = require("../../data/data-context");
var ui_internal_1 = require("../../utils/ui-internal");
var came_case_1 = require("../../utils/came-case");
var config_1 = require("../../config");
var utils_1 = require("./../../utils/utils");
exports.EVT_REFRESH_HISTORY = "EVT_REFRESH_HISTORY";
var UIEntityHistory = exports.UIEntityHistory = (function () {
    function UIEntityHistory(element, router, httpClient, config, i18n, context, pictureHelper, bindingEngine, taskQueue) {
        var _this = this;
        this.element = element;
        this.router = router;
        this.httpClient = httpClient;
        this.config = config;
        this.i18n = i18n;
        this.context = context;
        this.pictureHelper = pictureHelper;
        this.bindingEngine = bindingEngine;
        this.taskQueue = taskQueue;
        this.disposables = [];
        this.auditLogsToDisplay = [];
        this.auditLogLastDate = (0, moment_1.default)().add(1, "days").toDate();
        this.queryInProcess = false;
        this.stopQueryLog = false;
        this.viewHistory = true;
        this.disabledSaveFilter = true;
        this.filterConfig = {
            size: 'nm',
            offBackgroundColor: 'white',
            offTextColor: 'ui-theme--primary',
            onBackgroundColor: 'ui-theme--primary',
            onTextColor: 'white'
        };
        this.tagsSelected = [];
        this.userTagsSelected = [];
        this.file = HistorySpecificType.File;
        this.picture = HistorySpecificType.Picture;
        this.comment = HistorySpecificType.Comment;
        this.getOperationTranslation = function (entity, type) {
            var entry = _this.getTranslation(type);
            var trans = _this.i18n.tr(entry);
            var result = "";
            if (trans === entry)
                result = entity.ClrEntityType;
            result = trans;
            return result;
        };
        this.auditLogService = new service_base_1.ServiceBase(constants_1.EntityTypeNames.AuditLog);
        this.auditLogs = [];
        this.isLoading = true;
    }
    UIEntityHistory.prototype.bind = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.eventSub = ui_internal_1.UIInternal.subscribe(exports.EVT_REFRESH_HISTORY, function () { return __awaiter(_this, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                this.refreshHistory(true);
                                return [2];
                            });
                        }); });
                        return [4, this.refreshHistory(true)];
                    case 1:
                        _a.sent();
                        return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.detached = function () {
        this.eventSub.dispose();
    };
    UIEntityHistory.prototype.entityIdChanged = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.refreshHistory(true)];
                    case 1:
                        _a.sent();
                        return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.getFilterEntityNames = function () {
        return __awaiter(this, void 0, void 0, function () {
            var results, entityNames, userInfos, json;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.httpClient.fetch(this.config.globalConfig.remoteServiceName + "/AuditLog/EntityNamesAndUserNames?entityId=" + this.entityId + "&entityType=" + this.entityType)];
                    case 1:
                        results = _a.sent();
                        if (!results.ok) return [3, 3];
                        return [4, results.json()];
                    case 2:
                        json = _a.sent();
                        console.log(json.EntityInfos);
                        this.entitiesInfo = json.EntityInfos;
                        entityNames = json.EntityNames;
                        userInfos = json.UserInfos;
                        _a.label = 3;
                    case 3:
                        this.filterModelEntityName = {
                            label: null,
                            tags: []
                        };
                        this.entitiesInfo.forEach(function (x) {
                            _this.filterModelEntityName.tags.push({
                                id: x.EntityName,
                                label: _this.i18n.tr(_this.getTranslation(x.EntityName)),
                                icon: x.Icon
                            });
                        });
                        this.filterModelEntityName.tags.forEach(function (x) {
                            var selectedTag = _this.tagsSelected.find(function (t) { return t.id == x.id; });
                            if (selectedTag != null) {
                                x.selected = true;
                                _this.tagsSelected = _this.tagsSelected.filter(function (t) { return t.id != x.id; });
                                _this.tagsSelected.push(x);
                                _this.updateDisabledSaveFilter();
                            }
                            _this.disposables.push(_this.bindingEngine.propertyObserver(x, "selected").subscribe(function (newValue, oldValue) { return __awaiter(_this, void 0, void 0, function () {
                                var tagFind;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0:
                                            if (!(newValue != oldValue)) return [3, 3];
                                            if (!newValue) return [3, 1];
                                            this.disabledSaveFilter = false;
                                            this.tagsSelected.push(x);
                                            return [3, 3];
                                        case 1:
                                            tagFind = this.tagsSelected.find(function (tag) { return tag == x; });
                                            if (tagFind != null) {
                                                this.tagsSelected = this.tagsSelected.filter(function (tag) { return tag != x; });
                                            }
                                            return [4, this.refreshHistory(false)];
                                        case 2:
                                            _a.sent();
                                            this.updateDisabledSaveFilter();
                                            _a.label = 3;
                                        case 3: return [2];
                                    }
                                });
                            }); }));
                        });
                        this.filterModelUser = {
                            label: null,
                            tags: userInfos.map(function (x) {
                                return {
                                    id: x.UserName,
                                    label: x.UserName,
                                    icon: x.AvatarUri
                                };
                            })
                        };
                        this.filterModelUser.tags.forEach(function (x) {
                            var selectedTag = _this.userTagsSelected.find(function (t) { return t.id == x.id; });
                            if (selectedTag != null) {
                                x.selected = true;
                                _this.userTagsSelected = _this.userTagsSelected.filter(function (t) { return t.id != x.id; });
                                _this.userTagsSelected.push(x);
                                _this.updateDisabledSaveFilter();
                            }
                            _this.disposables.push(_this.bindingEngine.propertyObserver(x, "selected").subscribe(function (newValue, oldValue) { return __awaiter(_this, void 0, void 0, function () {
                                var tagFind;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0:
                                            if (!(newValue != oldValue)) return [3, 3];
                                            if (!newValue) return [3, 1];
                                            this.disabledSaveFilter = false;
                                            this.userTagsSelected.push(x);
                                            return [3, 3];
                                        case 1:
                                            tagFind = this.userTagsSelected.find(function (tag) { return tag == x; });
                                            if (tagFind != null) {
                                                this.userTagsSelected = this.userTagsSelected.filter(function (tag) { return tag != x; });
                                            }
                                            return [4, this.refreshHistory(false)];
                                        case 2:
                                            _a.sent();
                                            this.updateDisabledSaveFilter();
                                            _a.label = 3;
                                        case 3: return [2];
                                    }
                                });
                            }); }));
                        });
                        return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.updateDisabledSaveFilter = function () {
        if (this.filterModelUser.tags.some(function (x) { return x.selected == true; }) || this.filterModelEntityName.tags.some(function (x) { return x.selected == true; })) {
            this.disabledSaveFilter = false;
        }
        else {
            this.disabledSaveFilter = true;
        }
    };
    UIEntityHistory.prototype.refreshHistory = function (refreshFilter) {
        var _a;
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (!refreshFilter) return [3, 2];
                        return [4, this.getFilterEntityNames()];
                    case 1:
                        _b.sent();
                        _b.label = 2;
                    case 2:
                        this.auditLogs.splice(0);
                        (_a = this.auditLogsToDisplay) === null || _a === void 0 ? void 0 : _a.splice(0);
                        this.stopQueryLog = false;
                        this.auditLogLastDate = (0, moment_1.default)().add(1, "day").toDate();
                        if (!(this.entityId != null && this.entityType != null)) return [3, 4];
                        this.isLoading = true;
                        return [4, this.getAuditLogs()];
                    case 3:
                        _b.sent();
                        this.isLoading = false;
                        _b.label = 4;
                    case 4: return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.isEllipsisActive = function (e) {
        var tolerance = 2;
        return e.offsetHeight + tolerance < e.scrollHeight;
    };
    UIEntityHistory.prototype.getAuditLogs = function () {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function () {
            var filterEntityName, filterUser, results, newHistories;
            var _c;
            var _this = this;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        if (!(!this.queryInProcess && !this.stopQueryLog)) return [3, 4];
                        this.queryInProcess = true;
                        filterEntityName = (_a = this.filterModelEntityName.tags) === null || _a === void 0 ? void 0 : _a.filter(function (x) { return x.selected == true; }).map(function (x) { return x.id; }).toString();
                        filterUser = (_b = this.filterModelUser.tags) === null || _b === void 0 ? void 0 : _b.filter(function (x) { return x.selected == true; }).map(function (x) { return x.id; }).toString();
                        return [4, this.httpClient.fetch(this.config.globalConfig.remoteServiceName + "/AuditLog/" + constants_1.EntityTypeNames.ResourceNames.AuditLog + "?entityId=" + this.entityId + "&entityType=" + this.entityType + (filterEntityName.length > 0 ? "&filterEntityName=" + filterEntityName : "") + (filterUser.length > 0 ? "&filterUser=" + filterUser : ""))];
                    case 1:
                        results = _d.sent();
                        if (!results.ok) return [3, 3];
                        return [4, results.json()];
                    case 2:
                        newHistories = _d.sent();
                        (_c = this.auditLogs).push.apply(_c, __spreadArray([], __read(newHistories), false));
                        this.sortAuditLogs(newHistories);
                        this.taskQueue.queueTask(function () {
                            var commentDetail = _this.element.getElementsByClassName('comment_detail');
                            for (var i = 0; i < commentDetail.length; i++) {
                                var e = commentDetail[i];
                                if (_this.isEllipsisActive(e)) {
                                    e.classList.add("value_overflow_hidden");
                                }
                            }
                            var oldValue = _this.element.getElementsByClassName('history_container__old');
                            for (var i = 0; i < oldValue.length; i++) {
                                var e = oldValue[i];
                                if (_this.isEllipsisActive(e)) {
                                    e.classList.add("value_overflow_hidden");
                                }
                            }
                            var newValue = _this.element.getElementsByClassName('history_container__new');
                            for (var i = 0; i < newValue.length; i++) {
                                var e = newValue[i];
                                if (_this.isEllipsisActive(e)) {
                                    e.classList.add("value_overflow_hidden");
                                }
                            }
                            var containerPicture = _this.element.getElementsByClassName('history_container_picture');
                            for (var i = 0; i < containerPicture.length; i++) {
                                var e = containerPicture[i];
                                if (_this.isEllipsisActive(e)) {
                                    e.classList.add("value_overflow_hidden");
                                }
                            }
                        });
                        _d.label = 3;
                    case 3:
                        this.queryInProcess = false;
                        _d.label = 4;
                    case 4: return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.sortAuditLogs = function (auditLogs) {
        var _this = this;
        auditLogs.forEach(function (audit) {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j;
            if (_this.router.hasOwnRoute(audit.LinkEntityType)) {
                try {
                    var newRoute = _this.router.generate(audit.LinkEntityType, { param1: audit.LinkEntityPK.toString() });
                    audit.Link = newRoute;
                }
                catch (error) { }
            }
            if (audit.Changes != null && audit.Changes.length > 0) {
                _this.enrichChanges(audit);
            }
            var date = moment_1.default.utc(audit.DateTime).local().hour(0).minute(0).second(0).millisecond(0).toDate();
            var display = _this.auditLogsToDisplay.find(function (x) { return (0, moment_1.default)(date).diff(x.date, "days") == 0; });
            if (display == null) {
                display = {
                    date: date,
                    groupBlocs: []
                };
                _this.auditLogsToDisplay.push(display);
            }
            var startDate = moment_1.default.utc(audit.DateTime).local().minute(0).second(0).millisecond(0).toDate();
            var endDate = (0, moment_1.default)(startDate).clone().add(1, "hours").toDate();
            var groupBloc = display.groupBlocs.find(function (x) { return x.blocs.some(function (b) { return (0, moment_1.default)(startDate).diff(b.startDate, 'hours') == 0; }); });
            if (groupBloc == null) {
                groupBloc = display.groupBlocs.find(function (x) { return x.blocs.some(function (b) { return (0, moment_1.default)(startDate).diff(b.endDate, 'hours') == 0; }); });
                if (groupBloc == null) {
                    groupBloc = {
                        startDate: startDate,
                        endDate: endDate,
                        blocs: []
                    };
                    display.groupBlocs.push(groupBloc);
                    display.groupBlocs = _this.sortArray(display.groupBlocs, "endDate", "desc");
                }
            }
            var bloc = groupBloc.blocs.find(function (x) { return (0, moment_1.default)(endDate).diff(x.endDate, 'hours') == 0; });
            if (bloc == null) {
                bloc = {
                    startDate: startDate,
                    endDate: endDate,
                    lastOfBloc: false,
                    entities: []
                };
                groupBloc.blocs.push(bloc);
                groupBloc.blocs = _this.sortArray(groupBloc.blocs, "endDate", "desc");
                groupBloc.startDate = bloc.startDate;
            }
            var name = _this.getEntityName(audit);
            var type = _this.getSpecificTypeOfEntity(name, audit.ActionType);
            var operation = _this.getOperation(audit, type);
            var entity = bloc.entities.find(function (x) { return x.name == name && x.operation == operation; });
            if (entity == null) {
                entity = {
                    name: name,
                    operation: operation,
                    viewDetail: true,
                    icon: _this.entitiesInfo.find(function (x) { return x.EntityName == name; }).Icon,
                    link: audit.Link,
                    minutes: [],
                    type: type
                };
                bloc.entities.push(entity);
            }
            var minute = entity.minutes.find(function (x) { return moment_1.default.utc(audit.DateTime).local().diff(x.date, 'minutes') == 0 && x.user == audit.AuditUser; });
            if (minute == null) {
                minute = {
                    date: moment_1.default.utc(audit.DateTime).local().toDate(),
                    user: audit.AuditUser,
                    data: []
                };
                entity.minutes.push(minute);
                entity.minutes = _this.sortArray(entity.minutes, "date", "desc");
            }
            if (type == _this.picture || type == _this.file) {
                var url = (_a = audit.Changes) === null || _a === void 0 ? void 0 : _a.find(function (x) { return x.PropertyName == "Url"; });
                if (url != null) {
                    var urlData = {
                        url: url.NewValue,
                        name: audit.Display
                    };
                    minute.data.push(urlData);
                }
            }
            else if (type == _this.comment) {
                var description = (_c = (_b = audit.Changes) === null || _b === void 0 ? void 0 : _b.find(function (x) { return x.PropertyName == "Description"; })) === null || _c === void 0 ? void 0 : _c.NewValue;
                description = _this.formatHtml(description);
                var commentData = {
                    type: (_f = (_e = (_d = audit.Changes) === null || _d === void 0 ? void 0 : _d.find(function (x) { return x.PropertyName == "CommentTypeId"; })) === null || _e === void 0 ? void 0 : _e.NewValue) === null || _f === void 0 ? void 0 : _f.Auto,
                    description: description,
                    createdBy: (!audit.ActionType.startsWith("Create") ? (_h = (_g = audit.Changes) === null || _g === void 0 ? void 0 : _g.find(function (x) { return x.PropertyName == "CreatedByFullName"; })) === null || _h === void 0 ? void 0 : _h.NewValue : null),
                    Changes: _this.getCommentChanges(audit),
                };
                minute.data.push(commentData);
            }
            else {
                audit.Changes == ((_j = audit.Changes) === null || _j === void 0 ? void 0 : _j.filter(function (x) { return x.PropertyName != "CreatedByFullName"; }));
                minute.data.push(audit);
            }
        });
        this.auditLogsToDisplay.forEach(function (byDate) { return byDate.groupBlocs.forEach(function (x) { return x.blocs.last().lastOfBloc = true; }); });
    };
    UIEntityHistory.prototype.getCommentChanges = function (audit) {
        var _a;
        if (audit.ActionType == ActionType.Update || audit.ActionType == ActionType.UpdateFK) {
            return (_a = audit.Changes) === null || _a === void 0 ? void 0 : _a.filter(function (x) { return x.PropertyName != "CreatedByFullName" && x.PropertyName != "Description" && x.PropertyName != "CommentTypeId"; });
        }
        return [];
    };
    UIEntityHistory.prototype.formatHtml = function (html) {
        return utils_1.Utils.innerTextFromHTML(html);
    };
    UIEntityHistory.prototype.getSpecificTypeOfEntity = function (entityName, actionType) {
        var entityInfo = this.entitiesInfo.find(function (x) { return x.EntityName == entityName; });
        if (entityInfo.SpecificType == HistorySpecificType.File && actionType != ActionType.Update && actionType != ActionType.UpdateFK) {
            return HistorySpecificType.File;
        }
        if (entityInfo.SpecificType == HistorySpecificType.Picture && actionType != ActionType.Update && actionType != ActionType.UpdateFK) {
            return HistorySpecificType.Picture;
        }
        if (entityInfo.SpecificType == HistorySpecificType.Comment) {
            return HistorySpecificType.Comment;
        }
        return null;
    };
    UIEntityHistory.prototype.getOperation = function (entity, type) {
        if (entity.ActionType != null) {
            switch (entity.ActionType) {
                case "Create":
                    return "".concat(this.getOperationTranslation(entity, entity.ClrEntityType), "  <span class=\"body__historique--title--creation\">").concat(this.i18n.tr("history.created", { ns: "common" }), "</span>");
                case "Update":
                    return "".concat(this.getOperationTranslation(entity, entity.ClrEntityType), " <span class=\"body__historique--title--update\">").concat(this.i18n.tr("history.updated", { ns: "common" }), "</span>");
                case "CreateFK":
                    return "".concat(this.getOperationTranslation(entity, entity.LinkEntityType)).concat(this.displayNameCreateFk(entity, type), " <span class=\"body__historique--title--creation\">").concat(this.i18n.tr("history.created", { ns: "common" }), "</span>");
                case "UpdateFK":
                    return "".concat(this.getOperationTranslation(entity, entity.LinkEntityType)).concat(this.getDisplayName(entity), " <span class=\"body__historique--title--update\">").concat(this.i18n.tr("history.updated", { ns: "common" }), "</span>");
                case "DeleteFK":
                    return "".concat(this.getOperationTranslation(entity, entity.LinkEntityType)).concat(this.getDisplayName(entity), " <span class=\"body__historique--title--delete\">").concat(this.i18n.tr("history.deleted", { ns: "common" }), "</span>");
            }
        }
    };
    UIEntityHistory.prototype.displayNameCreateFk = function (entity, type) {
        if (type == null || (type != HistorySpecificType.File && type != HistorySpecificType.Picture && type != HistorySpecificType.Comment)) {
            return this.getDisplayName(entity);
        }
        return '';
    };
    UIEntityHistory.prototype.getEntityName = function (entity) {
        if (entity.ActionType != null) {
            switch (entity.ActionType) {
                case "Create":
                case "Update":
                    return entity.ClrEntityType;
                case "CreateFK":
                case "UpdateFK":
                case "DeleteFK":
                    return entity.LinkEntityType;
            }
        }
    };
    UIEntityHistory.prototype.enrichChanges = function (entity) {
        var _this = this;
        entity.Changes.forEach(function (change) {
            var _a, _b;
            change.TransPropertyName = (entity.ActionType != null && change.PropertyName != null && change.PropertyName.length >= 1) ?
                (entity.ActionType == "Update" && entity.ClrEntityType != null) ?
                    _this.i18n.tr(entity.ClrEntityType.toLowerCase() + "." + came_case_1.CamelCase.toCamelCase(change.PropertyName))
                    : ((entity.ActionType == "UpdateFK" && entity.LinkEntityType != null) ?
                        _this.i18n.tr(entity.LinkEntityType.toLowerCase() + "." + came_case_1.CamelCase.toCamelCase(change.PropertyName))
                        : change.PropertyName)
                :
                    change.PropertyName;
            change.TransOldValue = _this.valueFormating(change.OldValue);
            change.TransOldValue = _this.formatHtml(change.TransOldValue);
            change.TransNewValue = _this.valueFormating(change.NewValue);
            change.TransNewValue = _this.formatHtml(change.TransNewValue);
            change.showChangesContainer = ((_a = change.TransOldValue) === null || _a === void 0 ? void 0 : _a.length) > 0 || ((_b = change.TransNewValue) === null || _b === void 0 ? void 0 : _b.length) > 0;
        });
    };
    UIEntityHistory.prototype.valueFormating = function (value) {
        if (value != null) {
            if (Object.keys(value).indexOf("Lang1") > -1
                || Object.keys(value).indexOf("Lang2") > -1
                || Object.keys(value).indexOf("Lang3") > -1
                || Object.keys(value).indexOf("Lang4") > -1
                || Object.keys(value).indexOf("Lang5") > -1) {
                return value[this.translatePropertyName];
            }
            else if (typeof value == "boolean") {
                return value ? this.i18n.tr("general.yes", { ns: "common" }) : this.i18n.tr("general.no", { ns: "common" });
            }
            else if (typeof value == "number") {
                return value.toString();
            }
            else if (jQuery.isEmptyObject(value)) {
                return "";
            }
            else if ((0, moment_1.default)(value, "DD-MM-YYYY HH:mm:ss", true).isValid()) {
                return moment_1.default.utc(value, "DD-MM-YYYY HH:mm:ss", true).local().format("DD/MM/YYYY HH:mm");
            }
            else if ((0, moment_1.default)(value, "DD/MM/YYYY", true).isValid()) {
                return moment_1.default.utc(value, "DD/MM/YYYY", true).local().format("DD/MM/YYYY");
            }
            else if ((0, moment_1.default)(value, "HH:mm:ss", true).isValid()) {
                return moment_1.default.utc(value, "HH:mm:ss", true).local().format("HH:mm:ss");
            }
            else if ((0, moment_1.default)(value, "HH:mm", true).isValid()) {
                return value;
            }
            else {
                var strValue = value;
                if ((strValue === null || strValue === void 0 ? void 0 : strValue.length) > 10000)
                    return '';
                return value;
            }
        }
        else {
            return "";
        }
    };
    UIEntityHistory.prototype.getTranslation = function (type) {
        return "".concat(type.toLowerCase(), ".").concat(type.toLowerCase());
    };
    UIEntityHistory.prototype.getDisplayName = function (entity) {
        var result = "";
        if (entity.Display != null) {
            result += " ";
            switch (entity.DisplayType) {
                case "TranslatedProperty":
                    try {
                        var parsedDisplay = JSON.parse(entity.Display);
                        result += parsedDisplay[this.translatePropertyName];
                    }
                    catch (error) {
                    }
                    break;
                default:
                    result += entity.Display;
                    break;
            }
        }
        return result;
    };
    Object.defineProperty(UIEntityHistory.prototype, "translatePropertyName", {
        get: function () {
            return "Lang".concat(this.context.currentLanguage.columnIndex);
        },
        enumerable: false,
        configurable: true
    });
    UIEntityHistory.prototype.sortArray = function (array, propertyName, direction) {
        if (direction === void 0) { direction = 'asc'; }
        var factor = direction === 'asc' ? 1 : -1;
        return array.slice(0).sort(function (a, b) {
            return (a[propertyName] - b[propertyName]) * factor;
        });
    };
    UIEntityHistory.prototype.viewMoreChange = function ($event) {
        var list = $event.target.parentElement.getElementsByClassName("comment_detail");
        var parent;
        if (list.length == 0) {
            list = $event.target.parentElement.getElementsByClassName("history_container_picture");
            if (list.length == 0) {
                var previous = $event.target.previousElementSibling;
                if (!previous.className.includes("history_container__old")) {
                    var previous_1 = $event.target.previousElementSibling;
                    if (previous_1.className.includes("history_container__new")) {
                        parent = previous_1;
                    }
                }
                else {
                    parent = previous;
                }
            }
            else {
                parent = list[0];
            }
        }
        else {
            parent = list[0];
        }
        parent.classList.remove("value_overflow_hidden");
        parent.classList.add("value_overflow_visible");
    };
    UIEntityHistory.prototype.viewLessChange = function ($event) {
        var list = $event.target.parentElement.getElementsByClassName("comment_detail");
        var parent;
        if (list.length == 0) {
            list = $event.target.parentElement.getElementsByClassName("history_container_picture");
            if (list.length == 0) {
                var previous = $event.target.previousElementSibling.previousElementSibling;
                if (!previous.className.includes("history_container__old")) {
                    if (previous.className.includes("history_container__new")) {
                        parent = previous;
                    }
                }
                else {
                    parent = previous;
                }
            }
            else {
                parent = list[0];
            }
        }
        else {
            parent = list[0];
        }
        parent.classList.remove("value_overflow_visible");
        parent.classList.add("value_overflow_hidden");
    };
    UIEntityHistory.prototype.filter = function (open) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!open) return [3, 1];
                        this.viewHistory = false;
                        return [3, 4];
                    case 1:
                        if (!this.disabledSaveFilter) return [3, 3];
                        return [4, this.refreshHistory(false)];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        this.viewHistory = true;
                        _a.label = 4;
                    case 4: return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.saveFilter = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.viewHistory = true;
                        return [4, this.refreshHistory(false)];
                    case 1:
                        _a.sent();
                        return [2];
                }
            });
        });
    };
    UIEntityHistory.prototype.removeUserTagsSelected = function (tag) {
        this.userTagsSelected = this.userTagsSelected.filter(function (x) { return x != tag; });
        this.filterModelUser.tags.find(function (x) { return x.id == tag.id; }).selected = false;
    };
    UIEntityHistory.prototype.selectedUserTagsSelected = function (tag) {
        var findTag = this.filterModelUser.tags.find(function (x) { return x.id == tag.id; });
        findTag.selected = !findTag.selected;
    };
    UIEntityHistory.prototype.beforeCloseFn = function (tag) {
        tag.selected = false;
    };
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", String)
    ], UIEntityHistory.prototype, "entityType", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Number)
    ], UIEntityHistory.prototype, "entityId", void 0);
    __decorate([
        (0, aurelia_binding_1.computedFrom)("context"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UIEntityHistory.prototype, "translatePropertyName", null);
    UIEntityHistory = __decorate([
        (0, aurelia_framework_1.customElement)('ui-entity-history'),
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [Element, aurelia_router_1.Router, aurelia_fetch_client_1.HttpClient, config_1.Config, aurelia_i18n_1.I18N, data_context_1.Datacontext, picture_helper_1.PictureHelper, aurelia_framework_1.BindingEngine, aurelia_framework_1.TaskQueue])
    ], UIEntityHistory);
    return UIEntityHistory;
}());
var EntityInfo = (function () {
    function EntityInfo() {
    }
    return EntityInfo;
}());
var UserInfo = (function () {
    function UserInfo() {
    }
    return UserInfo;
}());
var ActionType = (function () {
    function ActionType() {
    }
    ActionType.Create = "Create";
    ActionType.Update = "Update";
    ActionType.CreateFK = "CreatedFK";
    ActionType.UpdateFK = "UpdateFK";
    ActionType.DeleteFK = "DeleteFK";
    return ActionType;
}());
var HistorySpecificType = (function () {
    function HistorySpecificType() {
    }
    HistorySpecificType.File = "File";
    HistorySpecificType.Picture = "Picture";
    HistorySpecificType.Comment = "Comment";
    return HistorySpecificType;
}());
var AuditLogToDisplay = (function () {
    function AuditLogToDisplay() {
    }
    return AuditLogToDisplay;
}());
var AuditLogByDate = (function () {
    function AuditLogByDate() {
    }
    return AuditLogByDate;
}());
var AuditLogGroupBloc = (function () {
    function AuditLogGroupBloc() {
    }
    return AuditLogGroupBloc;
}());
var AuditLogBlocHour = (function () {
    function AuditLogBlocHour() {
        this.lastOfBloc = false;
    }
    return AuditLogBlocHour;
}());
var AuditLogEntity = (function () {
    function AuditLogEntity() {
    }
    return AuditLogEntity;
}());
var AuditLogBlocMinute = (function () {
    function AuditLogBlocMinute() {
    }
    return AuditLogBlocMinute;
}());
var UrlData = (function () {
    function UrlData() {
    }
    return UrlData;
}());
var CommentData = (function () {
    function CommentData() {
    }
    return CommentData;
}());

//# sourceMappingURL=ui-entity-history.js.map
