"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.yfmTableDndPlugin = void 0;
exports.activateRows = activateRows;
exports.deactivateRow = deactivateRow;
exports.activateColumns = activateColumns;
exports.deactivateColumn = deactivateColumn;
exports.selectDraggedRow = selectDraggedRow;
exports.selectDraggedColumn = selectDraggedColumn;
exports.clearAllSelections = clearAllSelections;
const state_1 = require("../../../../../../pm/state.js");
const view_1 = require("../../../../../../pm/view.js");
const classname_1 = require("../../../../../../classname.js");
const const_1 = require("../const.js");
const b = (0, classname_1.cn)('yfm-table-selected-cell');
const key = new state_1.PluginKey('yfm-table-dnd-decos');
function activateRows(tr, params) {
    const meta = { action: 'row-control-active', ...params };
    tr.setMeta(key, meta);
    return tr;
}
function deactivateRow(tr, uniqKey) {
    const meta = { action: 'row-control-non-active', uniqKey };
    tr.setMeta(key, meta);
    return tr;
}
function activateColumns(tr, params) {
    const meta = { action: 'column-control-active', ...params };
    tr.setMeta(key, meta);
    return tr;
}
function deactivateColumn(tr, uniqKey) {
    const meta = { action: 'column-control-non-active', uniqKey };
    tr.setMeta(key, meta);
    return tr;
}
function selectDraggedRow(tr, cells) {
    const meta = { action: 'drag-rows', cells };
    tr.setMeta(key, meta);
    return tr;
}
function selectDraggedColumn(tr, cells) {
    const meta = { action: 'drag-columns', cells };
    tr.setMeta(key, meta);
    return tr;
}
function clearAllSelections(tr) {
    const meta = { action: 'hide' };
    tr.setMeta(key, meta);
    return tr;
}
const yfmTableDndPlugin = () => {
    return new state_1.Plugin({
        key,
        state: {
            init() {
                return view_1.DecorationSet.empty;
            },
            apply(tr, value) {
                const meta = tr.getMeta(key);
                if (meta?.action === 'hide') {
                    return view_1.DecorationSet.empty;
                }
                if (meta?.action === 'row-control-active') {
                    const { controlCell, cells, uniqKey } = meta;
                    return view_1.DecorationSet.create(tr.doc, [
                        view_1.Decoration.node(controlCell.from, controlCell.to, {}, {
                            [const_1.YfmTableDecorationUniqKey]: uniqKey,
                            [const_1.YfmTableDecorationTypeKey]: const_1.YfmTableDecorationType.OpenRowMenu,
                        }),
                        ...cells.map((cell) => view_1.Decoration.node(cell.from, cell.to, { class: b(cell.mods) }, {
                            [const_1.YfmTableDecorationUniqKey]: uniqKey,
                            [const_1.YfmTableDecorationTypeKey]: const_1.YfmTableDecorationType.ActivateRowCells,
                        })),
                    ]);
                }
                if (meta?.action === 'row-control-non-active') {
                    const { uniqKey } = meta;
                    const decos = value.find(undefined, undefined, (spec) => spec[const_1.YfmTableDecorationUniqKey] === uniqKey);
                    return value.remove(decos).map(tr.mapping, tr.doc);
                }
                if (meta?.action === 'column-control-active') {
                    const { controlCell, cells, uniqKey } = meta;
                    return view_1.DecorationSet.create(tr.doc, [
                        view_1.Decoration.node(controlCell.from, controlCell.to, {}, {
                            [const_1.YfmTableDecorationUniqKey]: uniqKey,
                            [const_1.YfmTableDecorationTypeKey]: const_1.YfmTableDecorationType.OpenColumnMenu,
                        }),
                        ...cells.map((pos) => view_1.Decoration.node(pos.from, pos.to, { class: b(pos.mods) }, {
                            [const_1.YfmTableDecorationUniqKey]: uniqKey,
                            [const_1.YfmTableDecorationTypeKey]: const_1.YfmTableDecorationType.ActivateColumnCells,
                        })),
                    ]);
                }
                if (meta?.action === 'column-control-non-active') {
                    const { uniqKey } = meta;
                    const decos = value.find(undefined, undefined, (spec) => spec[const_1.YfmTableDecorationUniqKey] === uniqKey);
                    return value.remove(decos).map(tr.mapping, tr.doc);
                }
                if (meta?.action === 'drag-rows') {
                    return view_1.DecorationSet.create(tr.doc, meta.cells.map((cell) => view_1.Decoration.node(cell.from, cell.to, {
                        class: b(cell.mods, 'dragged-cell'),
                    })));
                }
                if (meta?.action === 'drag-columns') {
                    return view_1.DecorationSet.create(tr.doc, meta.cells.map((cell) => view_1.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);
            },
        },
    });
};
exports.yfmTableDndPlugin = yfmTableDndPlugin;
//# sourceMappingURL=dnd-plugin.js.map
