"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    RTE_TRANSFORMERS: ()=>RTE_TRANSFORMERS
});
const markdown_namespaceObject = require("@lexical/markdown");
const table_namespaceObject = require("@lexical/table");
const external_lexical_namespaceObject = require("lexical");
const UNDERLINE_EXPORTER_AND_TRANSFORMER = {
    dependencies: [
        external_lexical_namespaceObject.TextNode
    ],
    export: (node, _, exportFormat)=>{
        if ((0, external_lexical_namespaceObject.$isTextNode)(node) && node.hasFormat('underline')) return '<u>' + exportFormat(node, node.getTextContent()) + '</u>';
        return null;
    },
    importRegExp: /<u>(.*?)<\/u>/,
    regExp: /<u>(.*?)<\/u>$/,
    replace: (textNode, match)=>{
        textNode.setFormat('underline');
        textNode.setTextContent(match[1] ?? '');
        return textNode;
    },
    trigger: '>',
    type: 'text-match'
};
const UNDERLINE_IMPORTER = {
    dependencies: [
        external_lexical_namespaceObject.TextNode
    ],
    export: ()=>null,
    regExp: /<u>(.*?)<\/u>/,
    replace: (_, children, match)=>{
        for (const child of children)if ((0, external_lexical_namespaceObject.$isTextNode)(child)) {
            child.setFormat('underline');
            child.setTextContent(match[1] ?? '');
        }
    },
    type: 'element'
};
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
const TABLE_ROW_DIVIDER_REG_EXP = /^\|(?:\s*:?-+:?\s*\|)+\s*$/;
const TABLE = {
    dependencies: [
        table_namespaceObject.TableNode,
        table_namespaceObject.TableRowNode,
        table_namespaceObject.TableCellNode
    ],
    export: (node)=>{
        if (!(0, table_namespaceObject.$isTableNode)(node)) return null;
        const output = [];
        for (const row of node.getChildren()){
            const rowOutput = [];
            if (!(0, table_namespaceObject.$isTableRowNode)(row)) continue;
            let isHeaderRow = false;
            for (const cell of row.getChildren())if ((0, table_namespaceObject.$isTableCellNode)(cell)) {
                rowOutput.push((0, markdown_namespaceObject.$convertToMarkdownString)(RTE_TRANSFORMERS, cell).replace(/\n/g, '\\n'));
                if (cell.__headerState === table_namespaceObject.TableCellHeaderStates.ROW) isHeaderRow = true;
            }
            output.push(`| ${rowOutput.join(' | ')} |`);
            if (isHeaderRow) output.push(`| ${rowOutput.map(()=>'---').join(' | ')} |`);
        }
        return output.join('\n');
    },
    regExp: TABLE_ROW_REG_EXP,
    replace: (parentNode, _1, match)=>{
        const matchText = match[0] ?? '';
        if (TABLE_ROW_DIVIDER_REG_EXP.test(matchText)) {
            const table = parentNode.getPreviousSibling();
            if (!table || !(0, table_namespaceObject.$isTableNode)(table)) return;
            const rows = table.getChildren();
            const lastRow = rows[rows.length - 1];
            if (!lastRow || !(0, table_namespaceObject.$isTableRowNode)(lastRow)) return;
            convertToHeaderCells(lastRow.getChildren());
            parentNode.remove();
            return;
        }
        const matchCells = mapToTableCells(matchText);
        if (null == matchCells) return;
        const rows = [
            matchCells
        ];
        let sibling = parentNode.getPreviousSibling();
        let maxCells = matchCells.length;
        let isHeaderRow = false;
        while(sibling){
            if (!(0, external_lexical_namespaceObject.$isParagraphNode)(sibling)) break;
            if (1 !== sibling.getChildrenSize()) break;
            const firstChild = sibling.getFirstChild();
            if (!(0, external_lexical_namespaceObject.$isTextNode)(firstChild)) break;
            if (TABLE_ROW_DIVIDER_REG_EXP.test(firstChild.getTextContent())) isHeaderRow = true;
            else {
                const cells = mapToTableCells(firstChild.getTextContent());
                if (null == cells) break;
                if (isHeaderRow) {
                    convertToHeaderCells(cells);
                    isHeaderRow = false;
                }
                maxCells = Math.max(maxCells, cells.length);
                rows.unshift(cells);
            }
            const previousSibling = sibling.getPreviousSibling();
            sibling.remove();
            sibling = previousSibling;
        }
        const table = (0, table_namespaceObject.$createTableNode)();
        for (const cells of rows){
            const tableRow = (0, table_namespaceObject.$createTableRowNode)();
            table.append(tableRow);
            tableRow.append(...cells);
            const lenDiff = maxCells - cells.length;
            const isInHeaderRow = cells[0]?.__headerState === table_namespaceObject.TableCellHeaderStates.ROW;
            for(let i = 0; i < lenDiff; i++){
                const newCell = $createTableCell('');
                if (isInHeaderRow) newCell.toggleHeaderStyle(table_namespaceObject.TableCellHeaderStates.ROW);
                tableRow.append(newCell);
            }
        }
        const previousSibling = parentNode.getPreviousSibling();
        if ((0, table_namespaceObject.$isTableNode)(previousSibling) && getTableColumnsSize(previousSibling) === maxCells) {
            previousSibling.append(...table.getChildren());
            parentNode.remove();
            previousSibling.selectEnd();
        } else {
            parentNode.replace(table);
            table.selectEnd();
        }
    },
    type: 'element'
};
function getTableColumnsSize(table) {
    const row = table.getFirstChild();
    return (0, table_namespaceObject.$isTableRowNode)(row) ? row.getChildrenSize() : 0;
}
const $createTableCell = (textContent)=>{
    textContent = textContent.replace(/\\r?\\n/g, '\n');
    const cell = (0, table_namespaceObject.$createTableCellNode)(table_namespaceObject.TableCellHeaderStates.NO_STATUS);
    (0, markdown_namespaceObject.$convertFromMarkdownString)(textContent, RTE_TRANSFORMERS, cell);
    return cell;
};
const mapToTableCells = (textContent)=>{
    const match = textContent.match(TABLE_ROW_REG_EXP);
    if (!match?.[1]) return null;
    return match[1].split('|').map((text)=>$createTableCell(text));
};
const convertToHeaderCells = (row)=>{
    row.forEach((cell)=>{
        if (!(0, table_namespaceObject.$isTableCellNode)(cell)) return;
        cell.toggleHeaderStyle(table_namespaceObject.TableCellHeaderStates.ROW);
    });
};
const RTE_TRANSFORMERS = [
    ...markdown_namespaceObject.TRANSFORMERS,
    UNDERLINE_EXPORTER_AND_TRANSFORMER,
    UNDERLINE_IMPORTER,
    TABLE
];
exports.RTE_TRANSFORMERS = __webpack_exports__.RTE_TRANSFORMERS;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "RTE_TRANSFORMERS"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
