import { tTag } from '../logical/data-type.js';
import { isTArray } from '../logical/typesystem.js';
import { DEFAULT_COLUMN_WIDTH } from '../table/consts.js';
import { insertPositionToIndex } from '../utils/insert.js';
import { columnManager } from './columns/manager.js';
import { groupByMatcher } from './group-by/matcher.js';
export const defaultGroupBy = (columnId, type, data) => {
    const name = groupByMatcher.match(columnManager.getColumn(type).dataType(data))?.name;
    return name != null
        ? {
            type: 'groupBy',
            columnId: columnId,
            name: name,
        }
        : undefined;
};
export const ViewOperationMap = {
    kanban: {
        init(model, id, name) {
            const column = model.columns.find(column => {
                const type = columnManager.getColumn(column.type).dataType(column.data);
                return !!(tTag.is(type) || (isTArray(type) && tTag.is(type.ele)));
            });
            if (!column) {
                throw new Error('not implement yet');
            }
            return {
                id,
                name,
                mode: 'kanban',
                columns: model.columns.map(v => ({
                    id: v.id,
                    hide: false,
                })),
                filter: {
                    type: 'group',
                    op: 'and',
                    conditions: [],
                },
                groupBy: defaultGroupBy(column.id, column.type, column.data),
            };
        },
        addColumn(_model, _view, _newColumn) {
            //Nothing to do
        },
        deleteColumn(_model, _view, _id) {
            //Nothing to do
        },
    },
    table: {
        init(_model, id, name) {
            return {
                id,
                name,
                mode: 'table',
                columns: [],
                filter: {
                    type: 'group',
                    op: 'and',
                    conditions: [],
                },
            };
        },
        addColumn(_model, view, newColumn, position) {
            view.columns.splice(insertPositionToIndex(position, view.columns), 0, {
                id: newColumn.id,
                width: DEFAULT_COLUMN_WIDTH,
            });
        },
        deleteColumn(_model, view, id) {
            const index = view.columns.findIndex(c => c.id === id);
            if (index >= 0) {
                view.columns.splice(index, 1);
            }
        },
    },
};
//# sourceMappingURL=view-manager.js.map