"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.yfmTableView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const _core_1 = require("../../../../../../core/index.js");
const ReactRenderer_1 = require("../../../../../behavior/ReactRenderer/index.js");
const lodash_1 = require("../../../../../../lodash.js");
const react_utils_1 = require("../../../../../../react-utils/index.js");
const table_desc_1 = require("../../../../../../table-utils/table-desc.js");
const insert_empty_column_1 = require("../commands/insert-empty-column.js");
const insert_empty_row_1 = require("../commands/insert-empty-row.js");
const FloatingPlusControl_1 = require("../components/FloatingPlusControl/index.js");
const const_1 = require("../const.js");
require("./yfm-table-view.css");
const yfmTableView = (node, view, getPos, decorations) => {
    return new YfmTableNewView(node, view, getPos, decorations);
};
exports.yfmTableView = yfmTableView;
class YfmTableNewView {
    dom;
    contentDOM;
    _node;
    _view;
    _getPos;
    _renderer;
    _logger;
    _isFocused = false;
    constructor(node, view, getPos, decorations) {
        this._node = node;
        this._view = view;
        this._getPos = getPos;
        this._logger = (0, _core_1.getLoggerFromState)(view.state).nested({
            node: 'yfm-table',
        });
        this.dom = this.contentDOM = view.dom.ownerDocument.createElement('table');
        this.dom.classList.add('g-md-yfm-table-view');
        this._updateFocusedState(decorations);
        this._renderer = (0, ReactRenderer_1.getReactRendererFromState)(view.state).createItem('yfm-table-view', () => {
            if (!this._isFocused)
                return null;
            const tableDesc = this._getTableDesc();
            if (!tableDesc)
                return null;
            const tableElem = this._view.domAtPos(tableDesc.pos + 1).node;
            const children = [];
            for (let rowIdx = 0; rowIdx < tableDesc.rows; rowIdx++) {
                const colIdx = tableDesc.base.rowsDesc[rowIdx].cells.findIndex((cell) => cell.type === 'real');
                if (colIdx >= 0) {
                    const cellPos = tableDesc.getPosForCell(rowIdx, colIdx);
                    const cellElem = this._view.domAtPos(cellPos.from + 1).node;
                    children.push((0, jsx_runtime_1.jsx)(FloatingPlusControl_1.FloatingPlusControl, { type: "row", index: rowIdx, cellElem: cellElem, tableElem: tableElem, onClick: this._onRowPlusClick }, `row-${rowIdx}-${colIdx}`));
                }
            }
            for (let colIdx = 0; colIdx < tableDesc.cols; colIdx++) {
                const rowIdx = (0, lodash_1.range)(0, tableDesc.rows).findIndex((rowIdx) => tableDesc.base.rowsDesc[rowIdx].cells[colIdx].type === 'real');
                if (rowIdx >= 0) {
                    const cellPos = tableDesc.getPosForCell(rowIdx, colIdx);
                    const cellElem = this._view.domAtPos(cellPos.from + 1).node;
                    children.push((0, jsx_runtime_1.jsx)(FloatingPlusControl_1.FloatingPlusControl, { type: "column", index: colIdx, cellElem: cellElem, tableElem: tableElem, onClick: this._onColumnPlusClick }, `col-${rowIdx}-${colIdx}`));
                }
            }
            return (0, jsx_runtime_1.jsx)(react_utils_1.ErrorLoggerBoundary, { children: children });
        });
    }
    update(node, decorations) {
        this._node = node;
        this._updateFocusedState(decorations);
        this._renderer.rerender();
        return true;
    }
    destroy() {
        this._renderer.remove();
    }
    _updateFocusedState(decorations) {
        this._isFocused = decorations.some((deco) => deco.spec?.[const_1.YfmTableDecorationTypeKey] === const_1.YfmTableDecorationType.FocusTable);
    }
    _getTableDesc() {
        const pos = this._getPos();
        if (typeof pos !== 'number')
            return null;
        return table_desc_1.TableDesc.create(this._node)?.bind(pos);
    }
    _onRowPlusClick = (rowIdx) => {
        this._logger.event({ event: 'row-insert', source: 'floating-plus-button' });
        const tableDesc = this._getTableDesc();
        if (!tableDesc)
            return;
        (0, insert_empty_row_1.insertEmptyRow)({ tablePos: tableDesc.pos, rowIndex: rowIdx + 1 })(this._view.state, this._view.dispatch);
        this._view.focus();
    };
    _onColumnPlusClick = (colIdx) => {
        this._logger.event({ event: 'column-insert', source: 'floating-plus-button' });
        const tableDesc = this._getTableDesc();
        if (!tableDesc)
            return;
        (0, insert_empty_column_1.insertEmptyColumn)({ tablePos: tableDesc.pos, colIndex: colIdx + 1 })(this._view.state, this._view.dispatch);
        this._view.focus();
    };
}
//# sourceMappingURL=yfm-table-view.js.map
