/*!
 * Jodit Editor (https://xdsoft.net/jodit/)
 * Released under MIT see LICENSE.txt in the project root for license information.
 * Copyright (c) 2013-2026 Valerii Chupurnov. All rights reserved. https://xdsoft.net
 */
import { IS_INLINE } from "../../../../../core/constants.js";
import { Dom } from "../../../../../core/dom/dom.js";
import { trimInv } from "../../../../../core/helpers/string/trim.js";
/**
 * @private
 */
export function tryRemoveNode(jodit, nodeElm, hadEffect, allowTags, denyTags, currentSelectionNode) {
    if (isRemovableNode(jodit, nodeElm, currentSelectionNode, allowTags, denyTags)) {
        Dom.safeRemove(nodeElm);
        return true;
    }
    return hadEffect;
}
/**
 * @private
 */
function isRemovableNode(jodit, node, current, allow, deny) {
    if (!Dom.isText(node)) {
        if (allow && !allow[node.nodeName]) {
            return true;
        }
        if (!allow && deny && deny[node.nodeName]) {
            return true;
        }
    }
    if (!jodit.o.cleanHTML.removeEmptyElements) {
        return false;
    }
    // Never drop an empty inline element that currently holds the caret — it is
    // a pending-format marker the user is about to type into (#1291). `current`
    // is captured before a click moves the caret, so also check the live caret.
    const liveCaret = jodit.s.isCollapsed()
        ? jodit.s.range.startContainer
        : null;
    return (Dom.isElement(node) &&
        node.nodeName.match(IS_INLINE) != null &&
        !Dom.isTemporary(node) &&
        trimInv(node.innerHTML).length === 0 &&
        (current == null || !Dom.isOrContains(node, current)) &&
        (liveCaret == null || !Dom.isOrContains(node, liveCaret)));
}
