import { Slot } from '@blockeditor/global/utils';
import { columnManager } from './columns/manager.js';
import { columnRenderer } from './columns/renderer.js';
export class BaseDataViewManager {
    get rows() {
        return this.filteredRows(this.searchString);
    }
    constructor(dataSource) {
        this.dataSource = dataSource;
        this.searchString = '';
        this.slots = {
            update: new Slot(),
        };
        this.dataSource.slots.update.pipe(this.slots.update);
    }
    setSearch(str) {
        this.searchString = str;
        this.slots.update.emit();
    }
    filteredRows(searchString) {
        return this.dataSource.rows.filter(id => {
            if (searchString) {
                const containsSearchString = this.columns.some(columnId => {
                    return this.cellGetStringValue(id, columnId)
                        ?.toLowerCase()
                        .includes(searchString?.toLowerCase());
                });
                if (!containsSearchString) {
                    return false;
                }
            }
            return this.isShow(id);
        });
    }
    get columnManagerList() {
        return this.columns.map(id => this.columnGet(id));
    }
    get readonly() {
        return false;
    }
    onCellUpdate(rowId, columnId, callback) {
        return this.dataSource.onCellUpdate(rowId, columnId, callback);
    }
    cellGetValue(rowId, columnId) {
        return columnManager
            .getColumn(this.columnGetType(columnId))
            .formatValue(this.dataSource.cellGetValue(rowId, columnId), this.columnGetData(columnId));
    }
    cellGetJsonValue(rowId, columnId) {
        return columnManager
            .getColumn(this.columnGetType(columnId))
            .toJson(this.dataSource.cellGetValue(rowId, columnId), this.columnGetData(columnId));
    }
    cellGetRenderValue(rowId, columnId) {
        return this.dataSource.cellGetRenderValue(rowId, columnId);
    }
    cellGetStringValue(rowId, columnId) {
        return (columnManager
            .getColumn(this.columnGetType(columnId))
            .toString(this.dataSource.cellGetValue(rowId, columnId), this.columnGetData(columnId)) ?? '');
    }
    cellGetExtra(rowId, columnId) {
        return this.dataSource.cellGetExtra(rowId, columnId);
    }
    cellSetValueFromString(columnId, cellData) {
        return (columnManager
            .getColumn(this.columnGetType(columnId))
            .fromString(cellData, this.columnGetData(columnId)) ?? '');
    }
    cellUpdateRenderValue(rowId, columnId, value) {
        this.dataSource.cellChangeValue(rowId, columnId, value);
    }
    cellUpdateValue(rowId, columnId, value) {
        this.dataSource.cellChangeValue(rowId, columnId, value);
    }
    columnAdd(position, type) {
        const id = this.dataSource.propertyAdd(position, type);
        this.columnMove(id, position);
        return id;
    }
    columnDelete(columnId) {
        this.dataSource.propertyDelete(columnId);
    }
    columnDuplicate(columnId) {
        const id = this.dataSource.propertyDuplicate(columnId);
        this.columnMove(id, {
            before: false,
            id: columnId,
        });
    }
    columnGetData(columnId) {
        return this.dataSource.propertyGetData(columnId);
    }
    columnGetDataType(columnId) {
        return columnManager
            .getColumn(this.columnGetType(columnId))
            .dataType(this.columnGetData(columnId));
    }
    columnGetIdByIndex(index) {
        return this.columns[index];
    }
    columnGetReadonly(columnId) {
        return this.dataSource.propertyGetReadonly(columnId);
    }
    columnGetIndex(columnId) {
        return this.columns.indexOf(columnId);
    }
    columnGetName(columnId) {
        return this.dataSource.propertyGetName(columnId);
    }
    columnGetNextColumn(columnId) {
        return this.columnGet(this.columnGetIdByIndex(this.columnGetIndex(columnId) + 1));
    }
    columnGetPreColumn(columnId) {
        return this.columnGet(this.columnGetIdByIndex(this.columnGetIndex(columnId) - 1));
    }
    columnGetType(columnId) {
        return this.dataSource.propertyGetType(columnId);
    }
    columnUpdateData(columnId, data) {
        this.dataSource.propertyChangeData(columnId, data);
    }
    columnUpdateName(columnId, name) {
        this.dataSource.propertyChangeName(columnId, name);
    }
    columnUpdateType(columnId, type) {
        this.dataSource.propertyChangeType(columnId, type);
    }
    rowAdd(insertPosition) {
        return this.dataSource.rowAdd(insertPosition);
    }
    rowDelete(ids) {
        this.dataSource.rowDelete(ids);
    }
    captureSync() {
        this.dataSource.captureSync();
    }
    get allColumnConfig() {
        return this.dataSource.allPropertyConfig;
    }
    get columnConfigManager() {
        return this.dataSource.columnConfigManager;
    }
    getIcon(type) {
        return columnRenderer.get(type).icon;
    }
    get detailSlots() {
        return this.dataSource.detailSlots;
    }
    get vars() {
        return this.columnsWithoutFilter.map(id => {
            const v = this.columnGet(id);
            return {
                id: v.id,
                name: v.name,
                type: columnManager.getColumn(v.type).dataType(v.data),
                icon: v.icon,
            };
        });
    }
    filterSetVisible(visible) {
        this._filterVisible = visible;
        this.slots.update.emit();
    }
    get filterVisible() {
        return this._filterVisible ?? this.filter.conditions.length > 0;
    }
    rowMove(rowId, position) {
        this.dataSource.rowMove(rowId, position);
    }
}
export class BaseDataViewColumnManager {
    constructor(propertyId, dataViewManager) {
        this.propertyId = propertyId;
        this.dataViewManager = dataViewManager;
    }
    get index() {
        return this.dataViewManager.columnGetIndex(this.id);
    }
    get data() {
        return this.dataViewManager.columnGetData(this.id);
    }
    get hide() {
        return this.dataViewManager.columnGetHide(this.id);
    }
    get id() {
        return this.propertyId;
    }
    get isFirst() {
        return this.dataViewManager.columnGetIndex(this.id) === 0;
    }
    get isLast() {
        return (this.dataViewManager.columnGetIndex(this.id) ===
            this.dataViewManager.columnManagerList.length - 1);
    }
    get name() {
        return this.dataViewManager.columnGetName(this.id);
    }
    get renderer() {
        return columnRenderer.get(this.type).cellRenderer;
    }
    get detailRenderer() {
        return columnRenderer.get(this.type).detailCellRenderer ?? this.renderer;
    }
    get type() {
        return this.dataViewManager.columnGetType(this.id);
    }
    get dataType() {
        return columnManager.getColumn(this.type).dataType(this.data);
    }
    getValue(rowId) {
        return this.dataViewManager.cellGetRenderValue(rowId, this.id);
    }
    getExtra(rowId) {
        return this.dataViewManager.cellGetExtra(rowId, this.id);
    }
    setValue(rowId, value) {
        this.dataViewManager.cellUpdateRenderValue(rowId, this.id, value);
    }
    updateData(updater) {
        const data = this.dataViewManager.columnGetData(this.id);
        this.dataViewManager.columnUpdateData(this.id, {
            ...data,
            ...updater(data),
        });
    }
    updateHide(hide) {
        this.dataViewManager.columnUpdateHide(this.id, hide);
    }
    updateName(name) {
        this.dataViewManager.columnUpdateName(this.id, name);
    }
    get delete() {
        return () => this.dataViewManager.columnDelete(this.id);
    }
    get duplicate() {
        return () => this.dataViewManager.columnDuplicate(this.id);
    }
    get updateType() {
        return type => this.dataViewManager.columnUpdateType(this.id, type);
    }
    get readonly() {
        return (this.dataViewManager.readonly ||
            this.dataViewManager.columnGetReadonly(this.id));
    }
    captureSync() {
        this.dataViewManager.captureSync();
    }
    getJsonValue(rowId) {
        return this.dataViewManager.cellGetJsonValue(rowId, this.id);
    }
    getStringValue(rowId) {
        return this.dataViewManager.cellGetStringValue(rowId, this.id);
    }
    setValueFromString(value) {
        const result = this.dataViewManager.cellSetValueFromString(this.id, value);
        if (result.data) {
            this.dataViewManager.columnUpdateData(this.id, result.data);
        }
        return result.value;
    }
    get icon() {
        if (!this.type)
            return undefined;
        return this.dataViewManager.getIcon(this.type);
    }
    onCellUpdate(rowId, callback) {
        return this.dataViewManager.onCellUpdate(rowId, this.id, callback);
    }
}
//# sourceMappingURL=data-view-manager.js.map