import { __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_HEAD_NAME } from '../../component-names';
import { MIconButton, MIconButtonSkin } from '../../icon-button/icon-button';
import { getCellAlignmentClass, getCellWidthStyle, MTableHeadStyle, MTableSortDirection } from '../responsive-table-commons';
import WithRender from './table-head.html?style=./table-head.scss';
var MTableHead = /** @class */ (function (_super) {
    __extends(MTableHead, _super);
    function MTableHead() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MTableHead.prototype.emitSort = function (column) { };
    MTableHead.prototype.emitUpdateHeadRows = function (headRows) { };
    Object.defineProperty(MTableHead.prototype, "hasMultipleHeadRow", {
        get: function () {
            return Object.keys(this.headRows).length > 1;
        },
        enumerable: false,
        configurable: true
    });
    MTableHead.prototype.getSkinSortIcon = function (headRowKey) {
        var rowIndex = Object.keys(this.headRows).findIndex(function (key) { return key === headRowKey; });
        if (this.headStyle === MTableHeadStyle.Dark && rowIndex <= 1) {
            return MIconButtonSkin.Dark;
        }
        return MIconButtonSkin.Light;
    };
    MTableHead.prototype.sort = function (currentColumn, columns) {
        if (this.disabled || !currentColumn.sortable) {
            return;
        }
        var sortDirection = this.getCurrentColumnSortDirection(currentColumn);
        columns.forEach(function (c) {
            if (c !== currentColumn) {
                c.sortDirection = MTableSortDirection.None;
            }
        });
        switch (sortDirection) {
            case MTableSortDirection.Asc:
                currentColumn.sortDirection = this.getNextSortDirectionWhenDirectionAsc(currentColumn);
                currentColumn.isInitialSort = false;
                break;
            case MTableSortDirection.Dsc:
                currentColumn.sortDirection = this.getNextSortDirectionWhenDirectionDsc(currentColumn);
                currentColumn.isInitialSort = false;
                break;
            case MTableSortDirection.None:
            default:
                currentColumn.sortDirection = this.getNextSortDirectionWhenDirectionNone(currentColumn);
                currentColumn.isInitialSort = true;
                break;
        }
        this.emitSort(currentColumn);
        this.emitUpdateHeadRows(this.headRows);
    };
    MTableHead.prototype.isColumnSorted = function (currentColumn) {
        return Boolean(this.getColumnSortDirectionClass(currentColumn));
    };
    MTableHead.prototype.getColumnAlignmentClass = function (column) {
        return getCellAlignmentClass(column.textAlign);
    };
    MTableHead.prototype.getColumnSortIcon = function (currentColumn) {
        if (currentColumn.sortDirection) {
            return 'm-svg__arrow-thin--up'; // CSS rotate transform animation takes care of the arrow position
        }
        else {
            return currentColumn.defaultSortDirection === MTableSortDirection.Dsc
                ? 'm-svg__arrow-thin--down'
                : 'm-svg__arrow-thin--up';
        }
    };
    MTableHead.prototype.getColumnSortDirectionClass = function (currentColumn) {
        switch (this.getCurrentColumnSortDirection(currentColumn)) {
            case MTableSortDirection.Asc:
                return 'm--is-sort-asc';
            case MTableSortDirection.Dsc:
                return 'm--is-sort-desc';
            case MTableSortDirection.None:
            default:
                return undefined;
        }
    };
    MTableHead.prototype.getColumnWidthStyle = function (column) {
        return getCellWidthStyle(column);
    };
    MTableHead.prototype.getNextSortDirectionWhenDirectionAsc = function (column) {
        if (column.enableUnsort) {
            return column.defaultSortDirection === MTableSortDirection.Dsc
                ? MTableSortDirection.None
                : MTableSortDirection.Dsc;
        }
        else {
            return MTableSortDirection.Dsc;
        }
    };
    MTableHead.prototype.getNextSortDirectionWhenDirectionDsc = function (column) {
        if (column.enableUnsort) {
            return column.defaultSortDirection === MTableSortDirection.Asc ||
                column.defaultSortDirection === undefined
                ? MTableSortDirection.None
                : MTableSortDirection.Asc;
        }
        else {
            return MTableSortDirection.Asc;
        }
    };
    MTableHead.prototype.getNextSortDirectionWhenDirectionNone = function (column) {
        return column.defaultSortDirection
            ? column.defaultSortDirection
            : MTableSortDirection.Asc;
    };
    MTableHead.prototype.getCurrentColumnSortDirection = function (currentColumn) {
        if (typeof currentColumn.sortDirection === 'undefined') {
            return MTableSortDirection.None;
        }
        return currentColumn.sortDirection || MTableSortDirection.None;
    };
    __decorate([
        Prop({
            required: true
        })
    ], MTableHead.prototype, "id", void 0);
    __decorate([
        Prop()
    ], MTableHead.prototype, "headRows", void 0);
    __decorate([
        Prop({
            default: MTableHeadStyle.Light,
            validator: function (value) {
                return Enums.toValueArray(MTableHeadStyle).includes(value);
            }
        })
    ], MTableHead.prototype, "headStyle", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MTableHead.prototype, "firstColumnFixed", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MTableHead.prototype, "disabled", void 0);
    __decorate([
        Emit('sort')
    ], MTableHead.prototype, "emitSort", null);
    __decorate([
        Emit('update:headRows')
    ], MTableHead.prototype, "emitUpdateHeadRows", null);
    MTableHead = __decorate([
        WithRender,
        Component({
            components: {
                MIconButton: MIconButton
            }
        })
    ], MTableHead);
    return MTableHead;
}(ModulVue));
export { MTableHead };
var TableHeadPlugin = {
    install: function (v) {
        v.prototype.$log.debug(TABLE_HEAD_NAME, 'plugin.install');
        v.component(TABLE_HEAD_NAME, MTableHead);
    }
};
export default TableHeadPlugin;
//# sourceMappingURL=table-head.js.map