import { getBlockModelById } from './block';
import * as constants from '../../common/constant';
export function toDomCol(dataCol, rowNumberEnabled) {
    return rowNumberEnabled ? dataCol + 1 : dataCol;
}
export function toDomRow(dataRow, headerEnabled) {
    return headerEnabled ? dataRow + 1 : dataRow;
}
export function toModelRow(domRow, headerEnabled) {
    return headerEnabled ? domRow - 1 : domRow;
}
export function getDataCell(tableElement, row, col) {
    return tableElement.querySelector("td[data-row=\"" + row + "\"][data-col=\"" + col + "\"]")
        || tableElement.querySelector("th[data-row=\"" + row + "\"][data-col=\"" + col + "\"]");
}
export function getHeaderCell(tableElement, col) {
    var thead = tableElement.tHead;
    if (!thead) {
        return null;
    }
    // Try both dataset and index fallback
    var cell = thead.querySelector("th[data-row=\"0\"][data-col=\"" + col + "\"]");
    if (!cell) {
        cell = thead.querySelectorAll('th')[col];
    }
    return cell || null;
}
export function doesHtmlHasTable(html, text) {
    var htmlHasTable = !!(html && /<table[\s\S]*?>[\s\S]*?<\/table>/i.test(html));
    var textLooksTabular = !!(text && (text.indexOf('\t') !== -1 || /\r?\n.*\t/.test(text)));
    return htmlHasTable || textLooksTabular;
}
export function getTableElements(blockId, rootEditorElement, blocks) {
    var blockEl = rootEditorElement.querySelector("#" + blockId);
    if (!blockEl) {
        return null;
    }
    var table = blockEl.querySelector('table.e-table-element');
    if (!table) {
        return null;
    }
    var block = getBlockModelById(blockId, blocks);
    var props = (block && block.properties) || {};
    return { table: table, props: props };
}
/**
 * Focuses all editable cells in the table.
 * Useful for "Select All" (Ctrl+A) inside a table.
 *
 * @param {HTMLElement} tableEl - The <table> element inside the table block
 * @returns {void}
 *
 * @hidden
 */
export function focusAllCellsInTable(tableEl) {
    var cells = tableEl.querySelectorAll('td[role="gridcell"]');
    cells.forEach(function (cell) {
        if (cell.classList.contains('e-row-number')) {
            return;
        }
        cell.classList.add(constants.TABLE_CELL_FOCUS);
    });
}
/**
 * Removes the focus highlight from all cells in the given table.
 *
 * @param {Element} table - The table element or any element containing the table
 * @returns {void}
 *
 * @hidden
 */
export function removeFocusFromAllCells(table) {
    var focusedCells = table.querySelectorAll('.' + constants.TABLE_CELL_FOCUS);
    if (focusedCells) {
        focusedCells.forEach(function (cell) { return cell.classList.remove(constants.TABLE_CELL_FOCUS); });
    }
}
