import { Plugin, PluginKey } from "../../../../../../pm/state.js";
import { Decoration, DecorationSet } from "../../../../../../pm/view.js";
import { cn } from "../../../../../../classname.js";
import { YfmTableDecorationType, YfmTableDecorationTypeKey, YfmTableDecorationUniqKey, } from "../const.js";
const b = cn('yfm-table-selected-cell');
const key = new PluginKey('yfm-table-dnd-decos');
export function activateRows(tr, params) {
    const meta = { action: 'row-control-active', ...params };
    tr.setMeta(key, meta);
    return tr;
}
export function deactivateRow(tr, uniqKey) {
    const meta = { action: 'row-control-non-active', uniqKey };
    tr.setMeta(key, meta);
    return tr;
}
export function activateColumns(tr, params) {
    const meta = { action: 'column-control-active', ...params };
    tr.setMeta(key, meta);
    return tr;
}
export function deactivateColumn(tr, uniqKey) {
    const meta = { action: 'column-control-non-active', uniqKey };
    tr.setMeta(key, meta);
    return tr;
}
export function selectDraggedRow(tr, cells) {
    const meta = { action: 'drag-rows', cells };
    tr.setMeta(key, meta);
    return tr;
}
export function selectDraggedColumn(tr, cells) {
    const meta = { action: 'drag-columns', cells };
    tr.setMeta(key, meta);
    return tr;
}
export function clearAllSelections(tr) {
    const meta = { action: 'hide' };
    tr.setMeta(key, meta);
    return tr;
}
export const yfmTableDndPlugin = () => {
    return new Plugin({
        key,
        state: {
            init() {
                return DecorationSet.empty;
            },
            apply(tr, value) {
                const meta = tr.getMeta(key);
                if (meta?.action === 'hide') {
                    return DecorationSet.empty;
                }
                if (meta?.action === 'row-control-active') {
                    const { controlCell, cells, uniqKey } = meta;
                    return DecorationSet.create(tr.doc, [
                        Decoration.node(controlCell.from, controlCell.to, {}, {
                            [YfmTableDecorationUniqKey]: uniqKey,
                            [YfmTableDecorationTypeKey]: YfmTableDecorationType.OpenRowMenu,
                        }),
                        ...cells.map((cell) => Decoration.node(cell.from, cell.to, { class: b(cell.mods) }, {
                            [YfmTableDecorationUniqKey]: uniqKey,
                            [YfmTableDecorationTypeKey]: YfmTableDecorationType.ActivateRowCells,
                        })),
                    ]);
                }
                if (meta?.action === 'row-control-non-active') {
                    const { uniqKey } = meta;
                    const decos = value.find(undefined, undefined, (spec) => spec[YfmTableDecorationUniqKey] === uniqKey);
                    return value.remove(decos).map(tr.mapping, tr.doc);
                }
                if (meta?.action === 'column-control-active') {
                    const { controlCell, cells, uniqKey } = meta;
                    return DecorationSet.create(tr.doc, [
                        Decoration.node(controlCell.from, controlCell.to, {}, {
                            [YfmTableDecorationUniqKey]: uniqKey,
                            [YfmTableDecorationTypeKey]: YfmTableDecorationType.OpenColumnMenu,
                        }),
                        ...cells.map((pos) => Decoration.node(pos.from, pos.to, { class: b(pos.mods) }, {
                            [YfmTableDecorationUniqKey]: uniqKey,
                            [YfmTableDecorationTypeKey]: YfmTableDecorationType.ActivateColumnCells,
                        })),
                    ]);
                }
                if (meta?.action === 'column-control-non-active') {
                    const { uniqKey } = meta;
                    const decos = value.find(undefined, undefined, (spec) => spec[YfmTableDecorationUniqKey] === uniqKey);
                    return value.remove(decos).map(tr.mapping, tr.doc);
                }
                if (meta?.action === 'drag-rows') {
                    return DecorationSet.create(tr.doc, meta.cells.map((cell) => Decoration.node(cell.from, cell.to, {
                        class: b(cell.mods, 'dragged-cell'),
                    })));
                }
                if (meta?.action === 'drag-columns') {
                    return DecorationSet.create(tr.doc, meta.cells.map((cell) => Decoration.node(cell.from, cell.to, {
                        class: b(cell.mods, 'dragged-cell'),
                    })));
                }
                return value.map(tr.mapping, tr.doc);
            },
        },
        props: {
            decorations(state) {
                return this.getState(state);
            },
        },
    });
};
//# sourceMappingURL=dnd-plugin.js.map
