"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecondaryActionsButton = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const ClickAwayListener_1 = __importDefault(require("@mui/material/ClickAwayListener"));
const react_1 = require("react");
const DotsVOutlineMd_1 = require("../../../icons/DotsVOutlineMd");
const useMenu_1 = require("../../useMenu");
const ActionCellContext_1 = require("../ActionCellContext");
const DataGridSecondaryActions_1 = require("../DataGridSecondaryActions");
const SecondaryActionsMenuContext_1 = require("../SecondaryActionsMenuContext");
const styles_1 = require("./styles");
const SecondaryActionsButton = ({ actions, renderActions, }) => {
    const buttonRef = (0, react_1.useRef)(null);
    const { open, anchorRef, handleOpenMenu, handleCloseMenu } = (0, useMenu_1.useMenu)(buttonRef);
    const { isDisabled } = (0, react_1.useContext)(ActionCellContext_1.ActionCellContext);
    // Если контент единожды отрендерился, то будем рендерить его дальше,
    // чтобы были актуальные статусы loading для блокировки действий
    const [isOnceOpen, setIsOnceOpen] = (0, react_1.useState)(false);
    const handleOpen = () => {
        setIsOnceOpen(true);
        handleOpenMenu();
    };
    const renderContent = () => {
        if (renderActions) {
            return renderActions({ isLoading: false });
        }
        return (0, jsx_runtime_1.jsx)(DataGridSecondaryActions_1.DataGridSecondaryActions, { actions: actions });
    };
    return ((0, jsx_runtime_1.jsxs)(SecondaryActionsMenuContext_1.SecondaryActionsMenuProvider, { value: { open, anchorEl: anchorRef.current }, children: [(0, jsx_runtime_1.jsx)(ClickAwayListener_1.default, { onClickAway: handleCloseMenu, mouseEvent: "onMouseUp", children: (0, jsx_runtime_1.jsx)(styles_1.StyledIconButton, { ref: anchorRef, onClick: handleOpen, selected: open, variant: "text", disabled: isDisabled, children: (0, jsx_runtime_1.jsx)(DotsVOutlineMd_1.DotsVOutlineMd, {}) }) }), isOnceOpen && renderContent()] }));
};
exports.SecondaryActionsButton = SecondaryActionsButton;
