var _a, _b, _c;
import { __decorate, __extends, __spreadArrays } from "tslib";
import Component from 'vue-class-component';
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { SORTABLE_NAME } from '../../../directives/directive-names';
import { SortableDirective } from '../../../directives/sortable/sortable';
import { I18N_NAME as I18N_FILTER_NAME } from '../../../filters/filter-names';
import { i18nFilter } from '../../../filters/i18n/i18n';
import { ModulVue } from '../../../utils/vue/vue';
import { MAdd } from '../../add/add';
import { MButton } from '../../button/button';
import { ADD_NAME, BUTTON_NAME, ICON_BUTTON_NAME, ICON_NAME, LIST_ITEM_NAME, ORGANIZE_TABLE_COLUMNS_NAME } from '../../component-names';
import { MIconButton } from '../../icon-button/icon-button';
import { MIcon } from '../../icon/icon';
import { MListItem } from '../../list-item/list-item';
import WithRender from './organize-table-columns.html?style=./organize-table-columns.scss';
var MOrganizeTableColumns = /** @class */ (function (_super) {
    __extends(MOrganizeTableColumns, _super);
    function MOrganizeTableColumns() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.displayedColumns = [];
        _this.hiddenColumns = [];
        return _this;
    }
    MOrganizeTableColumns.prototype.setInternalsColumns = function () {
        var internalColumns = JSON.parse(JSON.stringify(this.columns.filter(function (c) { return c.ignored === undefined || !c.ignored; })));
        var canBeFixed = true;
        for (var i = 0; i < internalColumns.length; i++) {
            if (internalColumns[i].fixed && !(internalColumns[i].visible === undefined || internalColumns[i].visible)) {
                // Une colonne ne peut pas être fixed et non visible
                throw new Error(this.$i18n.translate('m-organize-table-columns:error.fixed1'));
            }
            if (!canBeFixed && internalColumns[i].fixed) {
                // Une colonne ne peut pas être fixed et après une colonne non fixed
                throw new Error(this.$i18n.translate('m-organize-table-columns:error.fixed2'));
            }
            if (internalColumns[i].fixed === undefined || !internalColumns[i].fixed) {
                canBeFixed = false;
            }
        }
        this.displayedColumns = internalColumns.filter(function (c) { return c.visible === undefined || c.visible; });
        this.hiddenColumns = internalColumns.filter(function (c) { return (c.fixed === undefined || c.fixed === false) && c.visible === false; });
    };
    Object.defineProperty(MOrganizeTableColumns.prototype, "numberDisplayedColumns", {
        get: function () {
            return this.displayedColumns.length;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MOrganizeTableColumns.prototype, "numberHiddenColumns", {
        get: function () {
            return this.hiddenColumns.length;
        },
        enumerable: false,
        configurable: true
    });
    MOrganizeTableColumns.prototype.removeColumn = function (column) {
        this.displayedColumns.splice(this.displayedColumns.findIndex(function (c) { return c.id === column.id; }), 1);
        column.visible = false;
        this.hiddenColumns.push(column);
        this.emitReorganize(__spreadArrays(this.displayedColumns, this.hiddenColumns));
    };
    MOrganizeTableColumns.prototype.addColumn = function (column) {
        this.hiddenColumns.splice(this.hiddenColumns.findIndex(function (c) { return c.id === column.id; }), 1);
        column.visible = true;
        this.displayedColumns.push(column);
        this.emitReorganize(__spreadArrays(this.displayedColumns, this.hiddenColumns));
    };
    MOrganizeTableColumns.prototype.moveColumn = function (event) {
        this.moveItemInList(this.displayedColumns, event.sortInfo.oldPosition, event.sortInfo.newPosition);
        this.emitReorganize(__spreadArrays(this.displayedColumns, this.hiddenColumns));
    };
    MOrganizeTableColumns.prototype.isDraggable = function (column) {
        return column.fixed !== undefined && column.fixed ? 'false' : 'true';
    };
    MOrganizeTableColumns.prototype.isDroppable = function (column) {
        return column.fixed !== undefined && column.fixed ? 'false' : 'true';
    };
    MOrganizeTableColumns.prototype.emitReorganize = function (columns) {
    };
    MOrganizeTableColumns.prototype.moveItemInList = function (arr, oldIndex, newIndex) {
        var element = arr[oldIndex];
        arr.splice(oldIndex, 1);
        arr.splice(newIndex, 0, element);
    };
    __decorate([
        Prop({
            required: true
        })
    ], MOrganizeTableColumns.prototype, "columns", void 0);
    __decorate([
        Watch('columns', { immediate: true })
    ], MOrganizeTableColumns.prototype, "setInternalsColumns", null);
    __decorate([
        Emit('reorganize')
    ], MOrganizeTableColumns.prototype, "emitReorganize", null);
    MOrganizeTableColumns = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[ADD_NAME] = MAdd,
                _a[BUTTON_NAME] = MButton,
                _a[LIST_ITEM_NAME] = MListItem,
                _a[ICON_BUTTON_NAME] = MIconButton,
                _a[ICON_NAME] = MIcon,
                _a),
            filters: (_b = {},
                _b[I18N_FILTER_NAME] = i18nFilter,
                _b),
            directives: (_c = {},
                _c[SORTABLE_NAME] = SortableDirective,
                _c)
        })
    ], MOrganizeTableColumns);
    return MOrganizeTableColumns;
}(ModulVue));
export { MOrganizeTableColumns };
var OrganizeTableColumnsPlugin = {
    install: function (v, options) {
        v.component(ORGANIZE_TABLE_COLUMNS_NAME, MOrganizeTableColumns);
    }
};
export default OrganizeTableColumnsPlugin;
//# sourceMappingURL=organize-table-columns.js.map