/**
 * DevExtreme (esm/__internal/grids/grid_core/editor_factory/m_editor_factory.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/
 */
import positionUtils from "../../../../animation/position";
import domAdapter from "../../../../core/dom_adapter";
import $ from "../../../../core/renderer";
import browser from "../../../../core/utils/browser";
import {
    extend
} from "../../../../core/utils/extend";
import {
    getBoundingRect
} from "../../../../core/utils/position";
import {
    getOuterHeight,
    getOuterWidth,
    setOuterHeight,
    setOuterWidth
} from "../../../../core/utils/size";
import {
    name as clickEventName
} from "../../../../events/click";
import eventsEngine from "../../../../events/core/events_engine";
import pointerEvents from "../../../../events/pointer";
import {
    addNamespace,
    normalizeKeyName
} from "../../../../events/utils/index";
import EditorFactoryMixin from "../../../../ui/shared/ui.editor_factory_mixin";
import modules from "../m_modules";
import gridCoreUtils from "../m_utils";
var EDITOR_INLINE_BLOCK = "dx-editor-inline-block";
var CELL_FOCUS_DISABLED_CLASS = "dx-cell-focus-disabled";
var CELL_MODIFIED_CLASS = "dx-cell-modified";
var CELL_INVALID_CLASS = "invalid";
var FOCUSED_CELL_MODIFIED_CLASS = "dx-focused-cell-modified";
var FOCUSED_CELL_INVALID_CLASS = "dx-focused-cell-invalid";
var FOCUS_OVERLAY_CLASS = "focus-overlay";
var CONTENT_CLASS = "content";
var FOCUSED_ELEMENT_CLASS = "dx-focused";
var ROW_CLASS = "dx-row";
var MODULE_NAMESPACE = "dxDataGridEditorFactory";
var UPDATE_FOCUS_EVENTS = addNamespace([pointerEvents.down, "focusin", clickEventName].join(" "), MODULE_NAMESPACE);
var DX_HIDDEN = "dx-hidden";
var ViewControllerWithMixin = EditorFactoryMixin(modules.ViewController);
export class EditorFactory extends ViewControllerWithMixin {
    init() {
        this.createAction("onEditorPreparing", {
            excludeValidators: ["disabled", "readOnly"],
            category: "rendering"
        });
        this.createAction("onEditorPrepared", {
            excludeValidators: ["disabled", "readOnly"],
            category: "rendering"
        });
        this._columnsResizerController = this.getController("columnsResizer");
        this._editingController = this.getController("editing");
        this._keyboardNavigationController = this.getController("keyboardNavigation");
        this._columnsController = this.getController("columns");
        this._validatingController = this.getController("validating");
        this._rowsView = this.getView("rowsView");
        this._updateFocusHandler = this._updateFocusHandler || this.createAction(this._updateFocus.bind(this));
        this._subscribedContainerRoot = this._getContainerRoot();
        eventsEngine.on(this._subscribedContainerRoot, UPDATE_FOCUS_EVENTS, this._updateFocusHandler);
        this._attachContainerEventHandlers()
    }
    dispose() {
        clearTimeout(this._focusTimeoutID);
        clearTimeout(this._updateFocusTimeoutID);
        eventsEngine.off(this._subscribedContainerRoot, UPDATE_FOCUS_EVENTS, this._updateFocusHandler)
    }
    _getFocusedElement($dataGridElement) {
        var rowSelector = this.option("focusedRowEnabled") ? "tr[tabindex]:focus" : "tr[tabindex]:not(.dx-data-row):focus";
        var focusedElementSelector = ["td[tabindex]:focus", "".concat(rowSelector), "input:focus", "button:focus", "textarea:focus", "div[tabindex]:focus", ".dx-lookup-field:focus", ".dx-checkbox:focus", ".dx-switch:focus", ".dx-dropdownbutton .dx-buttongroup:focus", ".dx-adaptive-item-text:focus"].join(",");
        var $focusedElement = $dataGridElement.find(focusedElementSelector);
        return this.elementIsInsideGrid($focusedElement) && $focusedElement
    }
    _getFocusCellSelector() {
        return ".dx-row > td"
    }
    _updateFocusCore() {
        var $dataGridElement = this.component && this.component.$element();
        if ($dataGridElement) {
            var $focus = this._getFocusedElement($dataGridElement);
            if ($focus && $focus.length) {
                var isHideBorder;
                if (!$focus.hasClass(CELL_FOCUS_DISABLED_CLASS) && !$focus.hasClass(ROW_CLASS)) {
                    var $focusCell = $focus.closest("".concat(this._getFocusCellSelector(), ", .").concat(CELL_FOCUS_DISABLED_CLASS));
                    if ($focusCell.get(0) !== $focus.get(0)) {
                        isHideBorder = this._needHideBorder($focusCell);
                        $focus = $focusCell
                    }
                }
                if ($focus.length && !$focus.hasClass(CELL_FOCUS_DISABLED_CLASS)) {
                    this.focus($focus, isHideBorder);
                    return
                }
            }
        }
        this.loseFocus()
    }
    _needHideBorder($element) {
        var rowsViewElement = this._rowsView.element();
        var isRowsView = $element.closest(rowsViewElement).length > 0;
        var isEditing = this._editingController.isEditing();
        return $element.hasClass(EDITOR_INLINE_BLOCK) || isRowsView && !isEditing
    }
    _updateFocus(e) {
        var that = this;
        var isFocusOverlay = e && e.event && $(e.event.target).hasClass(that.addWidgetPrefix(FOCUS_OVERLAY_CLASS));
        that._isFocusOverlay = that._isFocusOverlay || isFocusOverlay;
        clearTimeout(that._updateFocusTimeoutID);
        that._updateFocusTimeoutID = setTimeout(() => {
            delete that._updateFocusTimeoutID;
            if (!that._isFocusOverlay) {
                that._updateFocusCore()
            }
            that._isFocusOverlay = false
        })
    }
    _updateFocusOverlaySize($element, position) {
        $element.hide();
        var location = positionUtils.calculate($element, extend({
            collision: "fit"
        }, position));
        if (location.h.oversize > 0) {
            setOuterWidth($element, getOuterWidth($element) - location.h.oversize)
        }
        if (location.v.oversize > 0) {
            setOuterHeight($element, getOuterHeight($element) - location.v.oversize)
        }
        $element.show()
    }
    callbackNames() {
        return ["focused"]
    }
    focus($element, isHideBorder) {
        var that = this;
        if (void 0 === $element) {
            return that._$focusedElement
        }
        if ($element) {
            if (!$element.is(that._$focusedElement)) {
                that._$focusedElement && that._$focusedElement.removeClass(FOCUSED_ELEMENT_CLASS)
            }
            that._$focusedElement = $element;
            clearTimeout(that._focusTimeoutID);
            that._focusTimeoutID = setTimeout(() => {
                delete that._focusTimeoutID;
                that.renderFocusOverlay($element, isHideBorder);
                $element.addClass(FOCUSED_ELEMENT_CLASS);
                that.focused.fire($element)
            })
        }
    }
    refocus() {
        var $focus = this.focus();
        this.focus($focus)
    }
    renderFocusOverlay($element, isHideBorder) {
        if (!gridCoreUtils.isElementInCurrentGrid(this, $element)) {
            return
        }
        if (!this._$focusOverlay) {
            this._$focusOverlay = $("<div>").addClass(this.addWidgetPrefix(FOCUS_OVERLAY_CLASS))
        }
        if (isHideBorder) {
            this._$focusOverlay.addClass(DX_HIDDEN)
        } else if ($element.length) {
            var align = browser.mozilla ? "right bottom" : "left top";
            var $content = $element.closest(".".concat(this.addWidgetPrefix(CONTENT_CLASS)));
            var elemCoord = getBoundingRect($element.get(0));
            var isFocusedCellInvalid = $element.hasClass(this.addWidgetPrefix(CELL_INVALID_CLASS));
            var isFocusedCellModified = $element.hasClass(CELL_MODIFIED_CLASS) && !isFocusedCellInvalid;
            this._$focusOverlay.removeClass(DX_HIDDEN).toggleClass(FOCUSED_CELL_INVALID_CLASS, isFocusedCellInvalid).toggleClass(FOCUSED_CELL_MODIFIED_CLASS, isFocusedCellModified).appendTo($content);
            setOuterHeight(this._$focusOverlay, elemCoord.bottom - elemCoord.top + 1);
            setOuterWidth(this._$focusOverlay, elemCoord.right - elemCoord.left + 1);
            var focusOverlayPosition = {
                precise: true,
                my: align,
                at: align,
                of: $element,
                boundary: $content.length && $content
            };
            this._updateFocusOverlaySize(this._$focusOverlay, focusOverlayPosition);
            positionUtils.setup(this._$focusOverlay, focusOverlayPosition);
            this._$focusOverlay.css("visibility", "visible")
        }
    }
    resize() {
        var $focusedElement = this._$focusedElement;
        if ($focusedElement) {
            this.focus($focusedElement)
        }
    }
    loseFocus(skipValidator) {
        this._$focusedElement && this._$focusedElement.removeClass(FOCUSED_ELEMENT_CLASS);
        this._$focusedElement = null;
        this._$focusOverlay && this._$focusOverlay.addClass(DX_HIDDEN)
    }
    _getContainerRoot() {
        var _a;
        var $container = null === (_a = this.component) || void 0 === _a ? void 0 : _a.$element();
        var root = domAdapter.getRootNode(null === $container || void 0 === $container ? void 0 : $container.get(0));
        if (root.nodeType === Node.DOCUMENT_FRAGMENT_NODE && !root.host) {
            return domAdapter.getDocument()
        }
        return root
    }
    _attachContainerEventHandlers() {
        var that = this;
        var $container = that.component && that.component.$element();
        if ($container) {
            eventsEngine.on($container, addNamespace("keydown", MODULE_NAMESPACE), e => {
                if ("tab" === normalizeKeyName(e)) {
                    that._updateFocusHandler(e)
                }
            })
        }
    }
}
export var editorFactoryModule = {
    defaultOptions: () => ({}),
    controllers: {
        editorFactory: EditorFactory
    }
};
