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);
};
import { CUSTOM_PAGING_ATTACHED } from './custom-paging';
import { autoinject, TaskQueue } from 'aurelia-framework';
import { Datacontext } from './../../data/data-context';
import { I18N } from 'aurelia-i18n';
import { bindable, customElement, observable, BindingEngine } from 'aurelia-framework';
import { CustomLogger } from '../../modules/custom-logger';
import { Box } from '../../modules/box';
import { Router } from 'aurelia-router';
import { AuthService } from '../../security/auth-service';
import { computedFrom } from 'aurelia-binding';
import { Config } from '../../config';
import { UIInternal } from '../../utils/ui-internal';
import { debounce } from 'debounce';
import { autoAdjustColumnsSize } from './../../ag-grid/agUtils';
export var CustomAgGrid = (function () {
    function CustomAgGrid(bindingEngine, router, logger, i18n, box, authService, datacontext, config, element, taskQueue) {
        this.bindingEngine = bindingEngine;
        this.router = router;
        this.logger = logger;
        this.i18n = i18n;
        this.box = box;
        this.authService = authService;
        this.datacontext = datacontext;
        this.config = config;
        this.element = element;
        this.taskQueue = taskQueue;
        this.canAdd = true;
        this.canDelete = true;
        this.canBack = false;
        this.canUseView = true;
        this.canUseFilter = true;
        this.canUseParameters = true;
        this.canExport = true;
        this.canShowAllView = false;
        this.disposables = [];
        this.showFilterBox = this.config.agGridConfig.showFilterBox;
        this.showViewBox = this.config.agGridConfig.showViewBox;
        this.showParametersViewBox = this.config.agGridConfig.showParametersViewBox;
    }
    Object.defineProperty(CustomAgGrid.prototype, "isHeightStyleProvided", {
        get: function () {
            return this.heightStyle != null;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(CustomAgGrid.prototype, "showRowsCount", {
        get: function () {
            return this.rowCount != null;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(CustomAgGrid.prototype, "showSelectedElemCount", {
        get: function () {
            return this.selectedElemCount != null;
        },
        enumerable: false,
        configurable: true
    });
    CustomAgGrid.prototype.attached = function () {
        var _this = this;
        window.addEventListener('resize', debounce(function () {
            if (_this.gridOptions.adjustColumns === true && _this.gridOptions.api != null) {
                _this.gridOptions.api.sizeColumnsToFit();
            }
            _this.calculateBodyHeight();
        }));
        UIInternal.subscribe(CUSTOM_PAGING_ATTACHED, function () { return _this.calculateBodyHeight(); });
        this.calculateBodyHeight();
    };
    CustomAgGrid.prototype.detached = function () {
        if (this.disposables) {
            this.disposables.forEach(function (x) {
                x.dispose();
            });
        }
    };
    CustomAgGrid.prototype.calculateBodyHeight = function (tries) {
        var _this = this;
        if (tries === void 0) { tries = 0; }
        if (!this.isHeightStyleProvided) {
            var availableHeight = this.element.parentElement.clientHeight;
            if (availableHeight <= 0) {
                this.calculatedHeight = 'height: 100px';
                return;
            }
            var parentStyle = getComputedStyle(this.element.parentElement);
            availableHeight -= this.getPadding(parentStyle, 'top') + this.getPadding(parentStyle, 'bottom');
            var headerHeight = this.element.getElementsByTagName('ag-grid-aurelia')[0].offsetTop;
            var footerHeight = this.element.getElementsByClassName("grid-footer-bar")[0].offsetHeight;
            if (footerHeight <= 0 && tries < 5) {
                this.taskQueue.queueTask(function () { return _this.calculateBodyHeight(++tries); });
            }
            else {
                this.calculatedHeight = "height: ".concat(availableHeight - headerHeight - footerHeight, "px");
            }
        }
        else {
            this.calculatedHeight = this.heightStyle;
        }
    };
    CustomAgGrid.prototype.getPadding = function (styles, side) {
        return parseFloat(styles.getPropertyValue("padding-".concat(side)).replace(/[^0-9.]/g, ''));
    };
    CustomAgGrid.prototype.gridOptionsChanged = function (newValue, oldValue) {
        var _this = this;
        newValue.onDisplayedColumnsChanged = function (ev) {
            var cols = ev.columnApi.getColumns();
            _this.options.refreshColumns(cols);
        };
        this.options = new BindableGridOptions(newValue);
    };
    CustomAgGrid.prototype.selectAll = function () {
        var show = this.options.columns.every(function (column) { return column.show; });
        this.options.columns.forEach(function (column) {
            column.show = !show;
        });
        autoAdjustColumnsSize(this.gridOptions);
    };
    __decorate([
        bindable,
        __metadata("design:type", Object)
    ], CustomAgGrid.prototype, "gridOptions", void 0);
    __decorate([
        bindable,
        __metadata("design:type", String)
    ], CustomAgGrid.prototype, "heightStyle", void 0);
    __decorate([
        bindable,
        __metadata("design:type", String)
    ], CustomAgGrid.prototype, "title", void 0);
    __decorate([
        bindable,
        __metadata("design:type", BindableGridOptions)
    ], CustomAgGrid.prototype, "options", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Function)
    ], CustomAgGrid.prototype, "addFn", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Function)
    ], CustomAgGrid.prototype, "deleteFn", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canAdd", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canDelete", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canBack", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canUseView", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canUseFilter", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canUseParameters", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canResetFilters", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canExport", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Array)
    ], CustomAgGrid.prototype, "agGridViewList", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Function)
    ], CustomAgGrid.prototype, "afterResetFilters", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Function)
    ], CustomAgGrid.prototype, "afterSetAgGridView", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Number)
    ], CustomAgGrid.prototype, "rowCount", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Number)
    ], CustomAgGrid.prototype, "selectedElemCount", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "canShowAllView", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "showFilterBox", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "showViewBox", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Boolean)
    ], CustomAgGrid.prototype, "showParametersViewBox", void 0);
    __decorate([
        bindable,
        __metadata("design:type", Object)
    ], CustomAgGrid.prototype, "csvExportConfig", void 0);
    __decorate([
        bindable,
        __metadata("design:type", String)
    ], CustomAgGrid.prototype, "agGridViewModuleView", void 0);
    __decorate([
        computedFrom("heightStyle"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], CustomAgGrid.prototype, "isHeightStyleProvided", null);
    __decorate([
        computedFrom("rowCount"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], CustomAgGrid.prototype, "showRowsCount", null);
    __decorate([
        computedFrom("selectedElemCount"),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], CustomAgGrid.prototype, "showSelectedElemCount", null);
    CustomAgGrid = __decorate([
        autoinject,
        customElement('custom-ag-grid'),
        __metadata("design:paramtypes", [BindingEngine, Router, CustomLogger, I18N, Box, AuthService, Datacontext, Config, Element, TaskQueue])
    ], CustomAgGrid);
    return CustomAgGrid;
}());
var GridColumn = (function () {
    function GridColumn(gridOptions) {
        this.gridOptions = gridOptions;
    }
    GridColumn.prototype.showChanged = function (newValue, oldValue) {
        if (newValue != oldValue && oldValue != null) {
            this.gridOptions.columnApi.setColumnVisible(this.field, newValue);
            window.dispatchEvent(new Event('resize'));
        }
    };
    __decorate([
        observable,
        __metadata("design:type", Boolean)
    ], GridColumn.prototype, "show", void 0);
    return GridColumn;
}());
var BindableGridOptions = (function () {
    function BindableGridOptions(gridOptions) {
        this.gridOptions = gridOptions;
        this.columnsSave = [];
        this.columns = [];
    }
    BindableGridOptions.prototype.initializeGridColumn = function (col, gridOptions) {
        var colDef = col.getColDef();
        if (colDef.headerName != null && colDef.headerName.length > 0) {
            var column_1 = new GridColumn(gridOptions);
            column_1.headerName = colDef.headerName;
            column_1.show = col.isVisible();
            column_1.hide = false;
            if (this.columnsSave && this.columnsSave.length > 0) {
                var columnSave = this.columnsSave.find(function (x) { return x.headerName == column_1.headerName; });
                if (columnSave) {
                    column_1.hide = columnSave.hide;
                }
            }
            column_1.field = col.getColId();
            this.columns.push(column_1);
        }
    };
    BindableGridOptions.prototype.refreshColumns = function (updatedColumns) {
        var _this = this;
        this.columnsSave = this.columns;
        this.columns = [];
        this.gridOptions.columnApi.getColumnState().forEach(function (colState) {
            var column = _this.gridOptions.columnApi.getColumn(colState.colId);
            _this.initializeGridColumn(column, _this.gridOptions);
        });
    };
    return BindableGridOptions;
}());
export { BindableGridOptions };

//# sourceMappingURL=custom-ag-grid.js.map
