/**
 * DevExtreme (cjs/ui/html_editor/utils/table_helper.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.TABLE_OPERATIONS = void 0;
exports.getAutoSizedElements = getAutoSizedElements;
exports.getColumnElements = getColumnElements;
exports.getLineElements = getLineElements;
exports.getRowElements = getRowElements;
exports.getTableFormats = getTableFormats;
exports.getTableOperationHandler = getTableOperationHandler;
exports.hasEmbedContent = hasEmbedContent;
exports.setLineElementsFormat = setLineElementsFormat;
exports.unfixTableWidth = unfixTableWidth;
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _iterator = require("../../../core/utils/iterator");
var _inflector = require("../../../core/utils/inflector");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const TABLE_FORMATS = ["table", "tableHeaderCell"];
const TABLE_OPERATIONS = ["insertTable", "insertHeaderRow", "insertRowAbove", "insertRowBelow", "insertColumnLeft", "insertColumnRight", "deleteColumn", "deleteRow", "deleteTable", "cellProperties", "tableProperties"];
exports.TABLE_OPERATIONS = TABLE_OPERATIONS;

function getTableFormats(quill) {
    const tableModule = quill.getModule("table");
    return null !== tableModule && void 0 !== tableModule && tableModule.tableFormats ? tableModule.tableFormats() : TABLE_FORMATS
}

function hasEmbedContent(module, selection) {
    return !!selection && module.quill.getText(selection).length < selection.length
}

function unfixTableWidth($table, _ref) {
    let {
        tableBlot: tableBlot,
        quill: quill
    } = _ref;
    const formatBlot = null !== tableBlot && void 0 !== tableBlot ? tableBlot : quill.scroll.find($table.get(0));
    formatBlot.format("tableWidth", "initial")
}

function getColumnElements($table) {
    let index = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0;
    return $table.find("tr").eq(index).find("th, td")
}

function getAutoSizedElements($table) {
    let direction = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "horizontal";
    const result = [];
    const isHorizontal = "horizontal" === direction;
    const $lineElements = isHorizontal ? getColumnElements($table) : getRowElements($table);
    $lineElements.each((index, element) => {
        const $element = (0, _renderer.default)(element);
        if ("" === $element.get(0).style[isHorizontal ? "width" : "height"]) {
            result.push($element)
        }
    });
    return result
}

function setLineElementsFormat(module, _ref2) {
    let {
        elements: elements,
        property: property,
        value: value
    } = _ref2;
    const tableBlotNames = module.quill.getModule("table").tableBlots;
    const fullPropertyName = "cell".concat((0, _inflector.camelize)(property, true));
    (0, _iterator.each)(elements, (i, element) => {
        var _formatBlot;
        let formatBlot = module.quill.scroll.find(element);
        if (!tableBlotNames.includes(formatBlot.statics.blotName)) {
            const descendBlot = formatBlot.descendant(blot => tableBlotNames.includes(blot.statics.blotName));
            formatBlot = descendBlot ? descendBlot[0] : null
        }
        null === (_formatBlot = formatBlot) || void 0 === _formatBlot ? void 0 : _formatBlot.format(fullPropertyName, value + "px")
    })
}

function getLineElements($table, index) {
    let direction = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "horizontal";
    return "horizontal" === direction ? getRowElements($table, index) : getColumnElements($table, index)
}

function getRowElements($table) {
    let index = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0;
    return $table.find("th:nth-child(".concat(1 + index, "), td:nth-child(").concat(1 + index, ")"))
}

function getTableOperationHandler(quill, operationName) {
    for (var _len = arguments.length, rest = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
        rest[_key - 2] = arguments[_key]
    }
    return () => {
        const table = quill.getModule("table");
        if (!table) {
            return
        }
        quill.focus();
        return table[operationName](...rest)
    }
}
