/**
 * DevExtreme (cjs/__internal/grids/grid_core/context_menu/m_context_menu.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";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.contextMenuModule = exports.ContextMenuView = exports.ContextMenuController = void 0;
var _element = require("../../../../core/element");
var _renderer = _interopRequireDefault(require("../../../../core/renderer"));
var _iterator = require("../../../../core/utils/iterator");
var _context_menu = _interopRequireDefault(require("../../../../ui/context_menu"));
var _m_modules = _interopRequireDefault(require("../m_modules"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const CONTEXT_MENU = "dx-context-menu";
const viewName = {
    columnHeadersView: "header",
    rowsView: "content",
    footerView: "footer",
    headerPanel: "headerPanel"
};
const VIEW_NAMES = ["columnHeadersView", "rowsView", "footerView", "headerPanel"];
let ContextMenuController = function(_modules$ViewControll) {
    _inheritsLoose(ContextMenuController, _modules$ViewControll);

    function ContextMenuController() {
        return _modules$ViewControll.apply(this, arguments) || this
    }
    var _proto = ContextMenuController.prototype;
    _proto.init = function() {
        this.createAction("onContextMenuPreparing")
    };
    _proto.getContextMenuItems = function(dxEvent) {
        if (!dxEvent) {
            return false
        }
        const that = this;
        const $targetElement = (0, _renderer.default)(dxEvent.target);
        let $element;
        let $targetRowElement;
        let $targetCellElement;
        let menuItems;
        (0, _iterator.each)(VIEW_NAMES, (function() {
            var _a, _b;
            const view = that.getView(this);
            $element = view && view.element();
            if ($element && ($element.is($targetElement) || $element.find($targetElement).length)) {
                $targetCellElement = $targetElement.closest(".dx-row > td, .dx-row > tr");
                $targetRowElement = $targetCellElement.parent();
                const rowIndex = view.getRowIndex($targetRowElement);
                const columnIndex = $targetCellElement[0] && $targetCellElement[0].cellIndex;
                const rowOptions = $targetRowElement.data("options");
                const options = {
                    event: dxEvent,
                    targetElement: (0, _element.getPublicElement)($targetElement),
                    target: viewName[this],
                    rowIndex: rowIndex,
                    row: view._getRows()[rowIndex],
                    columnIndex: columnIndex,
                    column: null === (_b = null === (_a = null === rowOptions || void 0 === rowOptions ? void 0 : rowOptions.cells) || void 0 === _a ? void 0 : _a[columnIndex]) || void 0 === _b ? void 0 : _b.column
                };
                options.items = view.getContextMenuItems && view.getContextMenuItems(options);
                that.executeAction("onContextMenuPreparing", options);
                that._contextMenuPrepared(options);
                menuItems = options.items;
                if (menuItems) {
                    return false
                }
            }
            return
        }));
        return menuItems
    };
    _proto._contextMenuPrepared = function(options) {};
    return ContextMenuController
}(_m_modules.default.ViewController);
exports.ContextMenuController = ContextMenuController;
let ContextMenuView = function(_modules$View) {
    _inheritsLoose(ContextMenuView, _modules$View);

    function ContextMenuView() {
        return _modules$View.apply(this, arguments) || this
    }
    var _proto2 = ContextMenuView.prototype;
    _proto2.init = function() {
        _modules$View.prototype.init.call(this);
        this._contextMenuController = this.getController("contextMenu")
    };
    _proto2._renderCore = function() {
        const $element = this.element().addClass(CONTEXT_MENU);
        this.setAria("role", "presentation", $element);
        this._createComponent($element, _context_menu.default, {
            onPositioning: actionArgs => {
                const {
                    event: event
                } = actionArgs;
                const contextMenuInstance = actionArgs.component;
                const items = this._contextMenuController.getContextMenuItems(event);
                if (items) {
                    contextMenuInstance.option("items", items);
                    event.stopPropagation()
                } else {
                    actionArgs.cancel = true
                }
            },
            onItemClick(params) {
                var _a, _b;
                null === (_b = null === (_a = params.itemData) || void 0 === _a ? void 0 : _a.onItemClick) || void 0 === _b ? void 0 : _b.call(_a, params)
            },
            cssClass: this.getWidgetContainerClass(),
            target: this.component.$element()
        })
    };
    return ContextMenuView
}(_m_modules.default.View);
exports.ContextMenuView = ContextMenuView;
const contextMenuModule = {
    defaultOptions: () => ({
        onContextMenuPreparing: null
    }),
    controllers: {
        contextMenu: ContextMenuController
    },
    views: {
        contextMenuView: ContextMenuView
    }
};
exports.contextMenuModule = contextMenuModule;
