"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.setDataToHTML = exports.regIndexReg = exports.getTargetCell = exports.getCellEventArgsSet = void 0;

const vutils_1 = require("@visactor/vutils");

function getCellEventArgsSet(e) {
    const tableEvent = {
        abstractPos: {
            x: e.viewport.x,
            y: e.viewport.y
        }
    }, targetCell = getTargetCell(e.target);
    return targetCell && (tableEvent.eventArgs = {
        col: targetCell.col,
        row: targetCell.row,
        event: e,
        targetCell: targetCell,
        mergeInfo: getMergeCellInfo(targetCell),
        target: e.target
    }), tableEvent;
}

function getTargetCell(target) {
    for (;target && target.parent; ) {
        if ("cell" === target.role) return target;
        target = target.parent;
    }
    return null;
}

function getMergeCellInfo(cellGroup) {
    if ((0, vutils_1.isValid)(cellGroup.mergeStartCol) && (0, vutils_1.isValid)(cellGroup.mergeStartRow) && (0, 
    vutils_1.isValid)(cellGroup.mergeEndCol) && (0, vutils_1.isValid)(cellGroup.mergeEndRow)) return {
        colStart: cellGroup.mergeStartCol,
        colEnd: cellGroup.mergeEndCol,
        rowStart: cellGroup.mergeStartRow,
        rowEnd: cellGroup.mergeEndRow
    };
}

function setDataToHTML(data) {
    const result = [ "<table>" ], META_HEAD = [ '<meta name="author" content="Visactor"/>', '<style type="text/css">td{white-space:normal}br{mso-data-placement:same-cell}</style>' ].join(""), rows = data.split("\r\n");
    return rows.forEach((function(rowCells, rowIndex) {
        const cells = rowCells.split("\t"), rowValues = [];
        0 === rowIndex && result.push("<tbody>"), cells.forEach((function(cell, cellIndex) {
            const parsedCellData = cell ? cell.toString().replace(/&/g, "&amp;").replace(/'/g, "&#39;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>").replace(/(<br(\s*|\/)>(\r\n|\n)?|\r\n|\n)/g, "<br>\r\n").replace(/\x20{2,}/gi, (substring => `<span style="mso-spacerun: yes">${"&nbsp;".repeat(substring.length - 1)} </span>`)).replace(/\t/gi, "&#9;") : " ";
            rowValues.push(`<td>${parsedCellData}</td>`);
        })), result.push("<tr>", ...rowValues, "</tr>"), rowIndex === rows.length - 1 && result.push("</tbody>");
    })), result.push("</table>"), [ META_HEAD, result.join("") ].join("");
}

exports.getCellEventArgsSet = getCellEventArgsSet, exports.getTargetCell = getTargetCell, 
exports.regIndexReg = /radio-\d+-\d+-(\d+)/, exports.setDataToHTML = setDataToHTML;
//# sourceMappingURL=util.js.map