import { isValid } from "@visactor/vutils";

export 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;
}

export function getCellEventArgsSetWithTable(e, table) {
    var _a;
    const eventArgsSet = getCellEventArgsSet(e);
    if (!eventArgsSet.eventArgs) return eventArgsSet;
    if (!table.options.scrollFrozenCols || 0 === (null === (_a = table.getFrozenColsOffset) || void 0 === _a ? void 0 : _a.call(table))) return eventArgsSet;
    const xInTable = e.x - table.tableX, yInTable = e.y - table.tableY;
    if (xInTable < 0 || yInTable < 0 || xInTable > table.tableNoFrameWidth || yInTable > table.tableNoFrameHeight) return eventArgsSet;
    const frozenViewportWidth = table.getFrozenColsWidth(), pickedCol = eventArgsSet.eventArgs.col, pickedRow = eventArgsSet.eventArgs.row;
    if (pickedCol >= 0 && pickedCol < table.frozenColCount && !table.isRightFrozenColumn(pickedCol, pickedRow) && xInTable >= frozenViewportWidth) {
        const cell = table.getCellAtRelativePosition(e.x, e.y);
        if (-1 === cell.col || -1 === cell.row) return eventArgsSet;
        const targetCell = table.scenegraph.getCell(cell.col, cell.row);
        eventArgsSet.eventArgs = {
            col: cell.col,
            row: cell.row,
            event: e,
            targetCell: targetCell,
            mergeInfo: getMergeCellInfo(targetCell),
            target: targetCell
        };
    }
    return eventArgsSet;
}

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

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

export const regIndexReg = /radio-\d+-\d+-(\d+)/;

export 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("");
}
//# sourceMappingURL=util.js.map