import { CollectionState, SelectState, } from '@wix/bex-core';
import { action, computed, makeObservable, observable } from 'mobx';
export class CustomColumnsPanelStateBase {
    constructor(params) {
        this.select = new SelectState({
            keyGetter: (column) => column.id,
        });
        this.searchValue = '';
        this.setSearchValue = action((value) => {
            this.searchValue = value;
            this.columnsCollection.refreshAllPages();
        });
        this.onColumnCheckboxToggle = action((column) => {
            const { select, onColumnClick } = this;
            select.toggle(column);
            onColumnClick?.(column, select.isChecked(column.id));
        });
        this.container = params.container;
        this.columnsCollection = new CollectionState({
            ...this.container,
            events: {},
            queryName: params.queryName,
            fetchData: async () => {
                const items = this.filteredColumns.filter((column) => column.hiddenFromCustomColumnsSelection !== true);
                return { items };
            },
            filters: {},
            itemKey: (item) => item.id,
            itemName: (item) => item.name,
        });
    }
    initBaseObservables() {
        makeObservable(this, {
            searchValue: observable,
            setSearchValue: action.bound,
            orderedColumns: computed,
            filteredColumns: computed,
        });
    }
    get filteredColumns() {
        const { orderedColumns, searchValue } = this;
        if (!searchValue.trim()) {
            return orderedColumns;
        }
        const searchLower = searchValue.toLowerCase();
        return orderedColumns.filter((column) => {
            const name = column.name ||
                (typeof column.title === 'string' ? column.title : column.id);
            return name.toLowerCase().includes(searchLower);
        });
    }
}
//# sourceMappingURL=CustomColumnsPanelStateBase.js.map