import { __awaiter, __decorate, __generator, __read, __spread, __values } from "tslib";
import { Component, OnInit, AfterViewInit, ViewChild, Output, EventEmitter, Input, ChangeDetectorRef } from '@angular/core';
import { TranslateService } from '../../../../services/translate/translate.service';
import { NotifyService } from '../../../../services/notify/notify.service';
import { ExcelService } from '../../../../services/excel/excel.service';
import { LoadingService } from '../../../../services/loading/loading.service';
import * as hash from 'object-hash';
var GridPopupExportExcelComponent = /** @class */ (function () {
    function GridPopupExportExcelComponent(ref, loadingService, excelService, notifyService, translateService) {
        var _this = this;
        this.ref = ref;
        this.loadingService = loadingService;
        this.excelService = excelService;
        this.notifyService = notifyService;
        this.translateService = translateService;
        this.prefix = 'Popup Export Excel';
        this.key = '';
        this.columns = [];
        this.extendColumns = [];
        this.excelConfigs = [];
        this.exportByTemplate = new EventEmitter();
        this.initDefaultTemplate = function () { return __awaiter(_this, void 0, void 0, function () {
            var excelColumns, extendColumns, columns, columns_1, columns_1_1, column, _a, _b, childColumn;
            var e_1, _c, e_2, _d;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        excelColumns = [];
                        extendColumns = this.extendColumns || [];
                        columns = __spread(this.columns, extendColumns);
                        try {
                            for (columns_1 = __values(columns), columns_1_1 = columns_1.next(); !columns_1_1.done; columns_1_1 = columns_1.next()) {
                                column = columns_1_1.value;
                                if (column.type === 'children') {
                                    try {
                                        for (_a = (e_2 = void 0, __values(column.children)), _b = _a.next(); !_b.done; _b = _a.next()) {
                                            childColumn = _b.value;
                                            if (childColumn.type !== 'image') {
                                                excelColumns.push({
                                                    field: childColumn.field,
                                                    title: childColumn.title,
                                                    width: childColumn.width
                                                });
                                            }
                                        }
                                    }
                                    catch (e_2_1) { e_2 = { error: e_2_1 }; }
                                    finally {
                                        try {
                                            if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
                                        }
                                        finally { if (e_2) throw e_2.error; }
                                    }
                                }
                                else if (column.type !== 'image') {
                                    excelColumns.push({
                                        field: column.field,
                                        title: column.title,
                                        width: column.width
                                    });
                                }
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (columns_1_1 && !columns_1_1.done && (_c = columns_1.return)) _c.call(columns_1);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                        return [4 /*yield*/, this.excelService.generateExcelTemplate({
                                columns: excelColumns
                            })];
                    case 1: return [2 /*return*/, _e.sent()];
                }
            });
        }); };
        this.generateTemplate = function () { return __awaiter(_this, void 0, void 0, function () {
            var result;
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        this.loadingService.start();
                        return [4 /*yield*/, this.initDefaultTemplate().finally(this.loadingService.stop)];
                    case 1:
                        result = _b.sent();
                        if ((_a = result) === null || _a === void 0 ? void 0 : _a.filePath) {
                            this.excelService.downloadTemplate(result.filePath, this.fullKey + ".xlsx");
                        }
                        return [2 /*return*/];
                }
            });
        }); };
        this.onExportByTemplate = function (excelConfig) {
            var e_3, _a, e_4, _b;
            var excelColumns = [];
            var extendColumns = _this.extendColumns || [];
            var columns = __spread(_this.columns, extendColumns);
            try {
                for (var columns_2 = __values(columns), columns_2_1 = columns_2.next(); !columns_2_1.done; columns_2_1 = columns_2.next()) {
                    var column = columns_2_1.value;
                    if (column.type === 'children') {
                        try {
                            for (var _c = (e_4 = void 0, __values(column.children)), _d = _c.next(); !_d.done; _d = _c.next()) {
                                var childColumn = _d.value;
                                if (childColumn.type !== 'image') {
                                    excelColumns.push({
                                        field: childColumn.field,
                                        title: childColumn.title,
                                        width: childColumn.width
                                    });
                                }
                            }
                        }
                        catch (e_4_1) { e_4 = { error: e_4_1 }; }
                        finally {
                            try {
                                if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
                            }
                            finally { if (e_4) throw e_4.error; }
                        }
                    }
                    else if (column.type !== 'image') {
                        excelColumns.push({
                            field: column.field,
                            title: column.title,
                            width: column.width
                        });
                    }
                }
            }
            catch (e_3_1) { e_3 = { error: e_3_1 }; }
            finally {
                try {
                    if (columns_2_1 && !columns_2_1.done && (_a = columns_2.return)) _a.call(columns_2);
                }
                finally { if (e_3) throw e_3.error; }
            }
            _this.exportByTemplate.emit({
                excelConfig: excelConfig,
                excelColumns: excelColumns
            });
            _this.ref.detectChanges();
        };
        this.uploadTemplate = function () { return __awaiter(_this, void 0, void 0, function () {
            var excelConfig;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.excelService.uploadTemplate(this.fullKey, this.validator)];
                    case 1:
                        excelConfig = _a.sent();
                        this.excelConfigs.push({
                            name: excelConfig.name,
                            fullPath: excelConfig.fullPath,
                            removable: true
                        });
                        this.ref.detectChanges();
                        return [2 /*return*/];
                }
            });
        }); };
        this.downloadTemplate = function (excelConfig) {
            _this.excelService.downloadTemplate(excelConfig.fullPath, excelConfig.name);
        };
        this.removeTemplate = function (excelConfig) {
            _this.notifyService.confirm(_this.translateService.translate('Remove template')).then(function () { return __awaiter(_this, void 0, void 0, function () {
                var idx;
                return __generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            this.loadingService.start();
                            return [4 /*yield*/, this.excelService.remove(this.fullKey, excelConfig.name).finally(this.loadingService.stop)];
                        case 1:
                            _a.sent();
                            idx = this.excelConfigs.indexOf(excelConfig);
                            this.excelConfigs.splice(idx, 1);
                            this.ref.detectChanges();
                            return [2 /*return*/];
                    }
                });
            }); });
        };
        this.exportDefault = function () { return __awaiter(_this, void 0, void 0, function () {
            var result, excelColumns, extendColumns, columns, columns_3, columns_3_1, column, _a, _b, childColumn;
            var e_5, _c, e_6, _d;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        this.loadingService.start();
                        return [4 /*yield*/, this.initDefaultTemplate().finally(this.loadingService.stop)];
                    case 1:
                        result = _e.sent();
                        excelColumns = [];
                        extendColumns = this.extendColumns || [];
                        columns = __spread(this.columns, extendColumns);
                        try {
                            for (columns_3 = __values(columns), columns_3_1 = columns_3.next(); !columns_3_1.done; columns_3_1 = columns_3.next()) {
                                column = columns_3_1.value;
                                if (column.type === 'children') {
                                    try {
                                        for (_a = (e_6 = void 0, __values(column.children)), _b = _a.next(); !_b.done; _b = _a.next()) {
                                            childColumn = _b.value;
                                            if (childColumn.type !== 'image') {
                                                excelColumns.push({
                                                    field: childColumn.field,
                                                    title: childColumn.title,
                                                    width: childColumn.width
                                                });
                                            }
                                        }
                                    }
                                    catch (e_6_1) { e_6 = { error: e_6_1 }; }
                                    finally {
                                        try {
                                            if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
                                        }
                                        finally { if (e_6) throw e_6.error; }
                                    }
                                }
                                else if (column.type !== 'image') {
                                    excelColumns.push({
                                        field: column.field,
                                        title: column.title,
                                        width: column.width
                                    });
                                }
                            }
                        }
                        catch (e_5_1) { e_5 = { error: e_5_1 }; }
                        finally {
                            try {
                                if (columns_3_1 && !columns_3_1.done && (_c = columns_3.return)) _c.call(columns_3);
                            }
                            finally { if (e_5) throw e_5.error; }
                        }
                        this.exportByTemplate.emit({
                            excelConfig: {
                                fullPath: result.filePath,
                                name: "[" + this.fullKey + "] Default.xlsx"
                            },
                            excelColumns: excelColumns
                        });
                        this.ref.detectChanges();
                        return [2 /*return*/];
                }
            });
        }); };
    }
    Object.defineProperty(GridPopupExportExcelComponent.prototype, "fullKey", {
        get: function () {
            if (!this.key) {
                return '';
            }
            return hash({
                prefix: this.prefix,
                key: this.key
            });
        },
        enumerable: true,
        configurable: true
    });
    GridPopupExportExcelComponent.prototype.ngOnInit = function () {
    };
    GridPopupExportExcelComponent.prototype.ngAfterViewInit = function () {
    };
    GridPopupExportExcelComponent.prototype.open = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!!this.fullKey) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.exportDefault()];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                    case 2: return [4 /*yield*/, this.loadConfiguration()];
                    case 3:
                        _a.sent();
                        this.modal.open();
                        this.ref.detectChanges();
                        return [2 /*return*/];
                }
            });
        });
    };
    GridPopupExportExcelComponent.prototype.loadConfiguration = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        this.excelConfigs = [];
                        _a = this;
                        return [4 /*yield*/, this.excelService.getFiles(this.fullKey)];
                    case 1:
                        _a.excelConfigs = _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    GridPopupExportExcelComponent.ctorParameters = function () { return [
        { type: ChangeDetectorRef },
        { type: LoadingService },
        { type: ExcelService },
        { type: NotifyService },
        { type: TranslateService }
    ]; };
    __decorate([
        ViewChild('modal')
    ], GridPopupExportExcelComponent.prototype, "modal", void 0);
    __decorate([
        Input()
    ], GridPopupExportExcelComponent.prototype, "key", void 0);
    __decorate([
        Input()
    ], GridPopupExportExcelComponent.prototype, "columns", void 0);
    __decorate([
        Input()
    ], GridPopupExportExcelComponent.prototype, "extendColumns", void 0);
    __decorate([
        Input()
    ], GridPopupExportExcelComponent.prototype, "validator", void 0);
    __decorate([
        Output()
    ], GridPopupExportExcelComponent.prototype, "exportByTemplate", void 0);
    GridPopupExportExcelComponent = __decorate([
        Component({
            selector: 'sd-grid-popup-export-excel',
            template: "<sd-modal-control width=\"sm\" [title]=\"'Export excel' | sdTranslate\" #modal>\r\n  <sd-modal-body>\r\n    <!-- <div class=\"mt-2\">\r\n      <button class=\"btn btn-success btn-sm mr-2\" (click)=\"exportDefault()\">\r\n        <i class=\"fa fa-download fa-fw mr-1\"></i>\r\n        {{'Export' | sdTranslate}}\r\n      </button>\r\n      <span>{{'Default Template' | sdTranslate}}</span>\r\n    </div> -->\r\n    <div *ngFor=\"let excelConfig of excelConfigs\" class=\"mt-2\">\r\n      <button class=\"btn btn-success btn-sm mr-2\" (click)=\"onExportByTemplate(excelConfig)\">\r\n        <i class=\"fa fa-download fa-fw mr-1\"></i>\r\n        {{'Export' | sdTranslate}}\r\n      </button>\r\n      <button *ngIf=\"excelConfig.removable\" class=\"btn btn-danger btn-sm mr-2\" (click)=\"removeTemplate(excelConfig)\">\r\n        <i class=\"fa fa-trash fa-fw mr-1\"></i>\r\n        {{'Delete template' | sdTranslate}}\r\n      </button>\r\n      <span>{{excelConfig.name}}</span>\r\n    </div>\r\n  </sd-modal-body>\r\n  <sd-modal-footer>\r\n    <div class=\"mr-auto\">\r\n      <button type=\"button\" class=\"btn btn-success btn-sm mr-2\" (click)=\"generateTemplate()\">\r\n        <i class=\"fa fa-download fa-fw mr-1\"></i>{{'Generate template' | sdTranslate}}</button>\r\n      <button type=\"button\" class=\"btn btn-warning btn-sm mr-2\" (click)=\"uploadTemplate()\">\r\n        <i class=\"fa fa-plus fa-fw mr-1\"></i>{{'Add template' | sdTranslate}}</button>\r\n    </div>\r\n  </sd-modal-footer>\r\n</sd-modal-control>",
            styles: [".btn{color:#fff;background-color:#919191;border-color:#919191;box-shadow:0 2px 2px 0 rgba(153,153,153,.14),0 3px 1px -2px rgba(153,153,153,.2),0 1px 5px 0 rgba(153,153,153,.12)}.btn:active,.btn:hover{box-shadow:0 14px 26px -12px rgba(153,153,153,.14),0 3px 1px -2px rgba(153,153,153,.2),0 1px 5px 0 rgba(153,153,153,.12)}.btn.btn-sm{padding:.2rem .5rem;font-size:11px}.btn.btn-xs{padding:.1rem .3rem;font-size:9px}.btn.btn-round{border-radius:30px}.btn.btn-primary{color:#fff;background-color:#c82c37;border-color:#c82c37;box-shadow:0 2px 2px 0 rgba(156,39,176,.14),0 3px 1px -2px rgba(156,39,176,.2),0 1px 5px 0 rgba(156,39,176,.12)}.btn.btn-primary:active,.btn.btn-primary:hover{box-shadow:0 14px 26px -12px rgba(156,39,176,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(156,39,176,.2)}.btn.btn-info{color:#fff;background-color:#00bcd4;border-color:#00bcd4;box-shadow:0 2px 2px 0 rgba(0,188,212,.14),0 3px 1px -2px rgba(0,0,0,.2),0 1px 5px 0 rgba(0,188,212,.12)}.btn.btn-info:active,.btn.btn-info:hover{box-shadow:0 14px 26px -12px rgba(0,188,212,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(0,188,212,.2)}.btn.btn-success{color:#fff;background-color:#4caf50;border-color:#4caf50;box-shadow:0 2px 2px 0 rgba(76,175,80,.14),0 3px 1px -2px rgba(76,175,80,.2),0 1px 5px 0 rgba(76,175,80,.12)}.btn.btn-success:active,.btn.btn-success:hover{box-shadow:0 14px 26px -12px rgba(76,175,80,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(76,175,80,.2)}.btn.btn-warning{color:#fff;background-color:#ff9800;border-color:#ff9800;box-shadow:0 2px 2px 0 rgba(255,152,0,.14),0 3px 1px -2px rgba(255,152,0,.2),0 1px 5px 0 rgba(255,152,0,.12)}.btn.btn-warning:active,.btn.btn-warning:hover{box-shadow:0 14px 26px -12px rgba(255,152,0,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(255,152,0,.2)}.btn.btn-danger{color:#fff;background-color:#f44336;border-color:#f44336;box-shadow:0 2px 2px 0 rgba(244,67,54,.14),0 3px 1px -2px rgba(244,67,54,.2),0 1px 5px 0 rgba(244,67,54,.12)}.btn.btn-danger:active,.btn.btn-danger:hover{box-shadow:0 14px 26px -12px rgba(244,67,54,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(244,67,54,.2)}", ".sd-label{font-weight:500;color:#919191!important}.sd-label.sd-label-primary{color:#c82c37!important}.sd-label.sd-label-success{color:#4caf50!important}.sd-label.sd-label-info{color:#00bcd4!important}.sd-label.sd-label-warning{color:#ff9800!important}.sd-label.sd-label-danger{color:#f44336!important}", "::-webkit-scrollbar{width:7px;height:7px}::-webkit-scrollbar-track{background:#f1f1f1}::-webkit-scrollbar-thumb{background:#888;border-radius:50px}::-webkit-scrollbar-thumb:hover{background:#555}", ""]
        })
    ], GridPopupExportExcelComponent);
    return GridPopupExportExcelComponent;
}());
export { GridPopupExportExcelComponent };
//# sourceMappingURL=data:application/json;base64, 