import { __assign, __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop } from 'vue-property-decorator';
import { Enums } from '../../utils/enums/enums';
import { ModulVue } from '../../utils/vue/vue';
import { TABLE_NAME } from '../component-names';
import { MIconButton } from '../icon-button/icon-button';
import { MProgress } from '../progress/progress';
import WithRender from './table.html?style=./table.scss';
export var MTableSkin;
(function (MTableSkin) {
    MTableSkin["Regular"] = "regular";
    MTableSkin["Simple"] = "simple";
})(MTableSkin || (MTableSkin = {}));
export var MColumnSortDirection;
(function (MColumnSortDirection) {
    MColumnSortDirection[MColumnSortDirection["None"] = 0] = "None";
    MColumnSortDirection[MColumnSortDirection["Asc"] = 1] = "Asc";
    MColumnSortDirection[MColumnSortDirection["Dsc"] = -1] = "Dsc";
})(MColumnSortDirection || (MColumnSortDirection = {}));
export var MColumnTextAlign;
(function (MColumnTextAlign) {
    MColumnTextAlign["Center"] = "center";
    MColumnTextAlign["Left"] = "left";
    MColumnTextAlign["Right"] = "right";
})(MColumnTextAlign || (MColumnTextAlign = {}));
var MTable = /** @class */ (function (_super) {
    __extends(MTable, _super);
    function MTable() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.i18nEmptyTable = _this.$i18n.translate('m-table:no-data');
        _this.i18nLoading = _this.$i18n.translate('m-table:loading');
        _this.i18nPleaseWait = _this.$i18n.translate('m-table:please-wait');
        _this.i18nSort = _this.$i18n.translate('m-table:sort');
        return _this;
    }
    MTable.prototype.onAdd = function () {
    };
    MTable.prototype.emitSortApplied = function (columnTable) { };
    Object.defineProperty(MTable.prototype, "isEmpty", {
        get: function () {
            return this.rows.length === 0 && !this.loading;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTable.prototype, "placeholderPositionType", {
        get: function () {
            return this.widthPlaceholder.search(/^\d+(\.[0-9]{1,4})?px$/) ? 'absolute' : 'sticky';
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MTable.prototype, "columnsInternal", {
        get: function () {
            return this.columns.filter(function (c) { return c.visible === undefined || c.visible; }).map(function (c) { return (__assign({}, c)); });
        },
        enumerable: false,
        configurable: true
    });
    MTable.prototype.sort = function (columnTable) {
        if (this.loading || !columnTable.sortable) {
            return;
        }
        if (typeof columnTable.sortDirection === 'undefined') {
            columnTable.sortDirection = MColumnSortDirection.None;
        }
        this.columnsInternal.forEach(function (c) {
            if (c !== columnTable) {
                c.sortDirection = MColumnSortDirection.None;
            }
        });
        switch (columnTable.sortDirection) {
            case MColumnSortDirection.None:
                columnTable.sortDirection = columnTable.defaultSortDirection ? columnTable.defaultSortDirection : MColumnSortDirection.Asc;
                columnTable.isInitialSort = true;
                break;
            case MColumnSortDirection.Asc:
                if (columnTable.enableUnsort) {
                    columnTable.sortDirection = columnTable.defaultSortDirection === MColumnSortDirection.Dsc ? MColumnSortDirection.None : MColumnSortDirection.Dsc;
                }
                else {
                    columnTable.sortDirection = MColumnSortDirection.Dsc;
                }
                columnTable.isInitialSort = false;
                break;
            case MColumnSortDirection.Dsc:
                if (columnTable.enableUnsort) {
                    columnTable.sortDirection = (columnTable.defaultSortDirection === MColumnSortDirection.Asc || columnTable.defaultSortDirection === undefined) ? MColumnSortDirection.None : MColumnSortDirection.Asc;
                }
                else {
                    columnTable.sortDirection = MColumnSortDirection.Asc;
                }
                columnTable.isInitialSort = false;
                break;
        }
        this.emitSortApplied(columnTable);
    };
    MTable.prototype.isColumnSorted = function (columnTable) {
        return columnTable.sortDirection === MColumnSortDirection.Asc || columnTable.sortDirection === MColumnSortDirection.Dsc;
    };
    MTable.prototype.isColumnTextAlignCenter = function (columnTable) {
        return columnTable.textAlign === MColumnTextAlign.Center;
    };
    MTable.prototype.isColumnTextAlignLeft = function (columnTable) {
        return columnTable.textAlign === MColumnTextAlign.Left;
    };
    MTable.prototype.isColumnTextAlignRight = function (columnTable) {
        return columnTable.textAlign === MColumnTextAlign.Right;
    };
    MTable.prototype.getColumnSortDirectionClass = function (columnTable) {
        switch (columnTable.sortDirection) {
            case MColumnSortDirection.Asc:
                return 'm--is-sort-asc';
            case MColumnSortDirection.Dsc:
                return 'm--is-sort-desc';
            default:
                if (columnTable.defaultSortDirection) {
                    return columnTable.defaultSortDirection === MColumnSortDirection.Asc ? 'm--is-sort-asc' : 'm--is-sort-desc';
                }
                else {
                    return undefined;
                }
        }
    };
    MTable.prototype.getColumnSortIcon = function (columnTable) {
        if (columnTable.sortDirection) {
            return 'm-svg__arrow-thin--down'; // CSS rotate transform animation takes care of the arrow position
        }
        else {
            return columnTable.defaultSortDirection === MColumnSortDirection.Dsc ? 'm-svg__arrow-thin--up' : 'm-svg__arrow-thin--down';
        }
    };
    MTable.prototype.columnWidth = function (col) {
        return col.width ? { width: col.width } : '';
    };
    __decorate([
        Prop({
            default: MTableSkin.Regular,
            validator: function (value) { return Enums.toValueArray(MTableSkin).includes(value); }
        })
    ], MTable.prototype, "skin", void 0);
    __decorate([
        Prop({ default: function () { return []; } })
    ], MTable.prototype, "columns", void 0);
    __decorate([
        Prop({ default: function () { return []; } })
    ], MTable.prototype, "rows", void 0);
    __decorate([
        Prop({ default: false })
    ], MTable.prototype, "loading", void 0);
    __decorate([
        Prop({ default: true })
    ], MTable.prototype, "rowHighlightedOnHover", void 0);
    __decorate([
        Prop({
            default: '100%',
            validator: function (value) {
                var pixelOrPercentageNumberRegExp = /^\d+(\.[0-9]{1,4})?(px|%)$/;
                var isPixelOrPercentageNumber = pixelOrPercentageNumberRegExp.test(value);
                if (!isPixelOrPercentageNumber) {
                    ModulVue.prototype.$log.warn("width-placeholder value needs to respect this RegEx: " + pixelOrPercentageNumberRegExp);
                }
                return isPixelOrPercentageNumber;
            }
        })
    ], MTable.prototype, "widthPlaceholder", void 0);
    __decorate([
        Emit('add')
    ], MTable.prototype, "onAdd", null);
    __decorate([
        Emit('sort-applied')
    ], MTable.prototype, "emitSortApplied", null);
    MTable = __decorate([
        WithRender,
        Component({
            components: {
                MProgress: MProgress,
                MIconButton: MIconButton
            }
        })
    ], MTable);
    return MTable;
}(ModulVue));
export { MTable };
var TablePlugin = {
    install: function (v, options) {
        v.component(TABLE_NAME, MTable);
    }
};
export default TablePlugin;
//# sourceMappingURL=table.js.map