"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.verticalDividerAlignmentValues = exports.menuAlignmentValues = exports.BaseMenuButton = void 0;
var _Popover2 = _interopRequireDefault(require("@mui/material/Popover"));
var _Menu2 = _interopRequireDefault(require("@mui/material/Menu"));
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _react = require("react");
var _Box = require("../Box.cjs");
var _index = require("../icons.generated/index.cjs");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
var _useUniqueId = require("../useUniqueId.cjs");
var _BaseButton = require("./BaseButton.cjs");
var _MenuContext = require("./MenuContext.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const menuAlignmentValues = exports.menuAlignmentValues = ["left", "right"];
const verticalDividerAlignmentValues = exports.verticalDividerAlignmentValues = ["start", "end"];
const StyledMenuButtonContainer = (0, _styled.default)("div", {
  shouldForwardProp: prop => prop !== "hasVerticalDivider" && prop !== "odysseyDesignTokens" && prop !== "verticalDividerAlignment"
})(({
  hasVerticalDivider,
  odysseyDesignTokens,
  verticalDividerAlignment
}) => ({
  position: "relative",
  ...(hasVerticalDivider && {
    display: "inline-block",
    "&:before": {
      position: "absolute",
      top: "50%",
      left: 0,
      width: 1,
      height: odysseyDesignTokens.Spacing6,
      backgroundColor: odysseyDesignTokens.HueNeutral300,
      content: '""',
      transform: "translate3d(0, -50%, 0)",
      transition: `opacity 200ms`,
      ...(verticalDividerAlignment === "end" && {
        left: "unset",
        right: 0
      })
    },
    ":has(> button:hover)": {
      "&:before": {
        opacity: 0
      }
    },
    ":dir(rtl)": {
      "&:before": {
        left: "unset",
        right: 0,
        ...(verticalDividerAlignment === "end" && {
          right: "unset",
          left: 0
        })
      }
    }
  })
}));
const BaseMenuButton = ({
  ariaLabel,
  ariaLabelledBy,
  ariaDescribedBy,
  buttonChildren,
  buttonLabel = "",
  buttonVariant = "secondary",
  children,
  popoverContent,
  endIcon: endIconProp,
  hasVerticalDivider = false,
  id: idOverride,
  isDisabled,
  isOverflow,
  menuAlignment = "left",
  omitEndIcon = false,
  shouldCloseOnSelect = true,
  size,
  testId,
  tooltipText,
  translate,
  verticalDividerAlignment = "start"
}) => {
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const [anchorEl, setAnchorEl] = (0, _react.useState)(null);
  const isOpen = Boolean(anchorEl);
  const closeMenu = (0, _react.useCallback)(() => {
    setAnchorEl(null);
  }, []);
  const openMenu = (0, _react.useCallback)(event => {
    setAnchorEl(event.currentTarget);
  }, []);
  const uniqueId = (0, _useUniqueId.useUniqueId)(idOverride);
  const menuListProps = (0, _react.useMemo)(() => ({
    "aria-labelledby": `${uniqueId}-button`
  }), [uniqueId]);
  const providerValue = (0, _react.useMemo)(() => ({
    closeMenu,
    openMenu,
    shouldCloseOnSelect
  }), [closeMenu, openMenu, shouldCloseOnSelect]);
  const endIcon = omitEndIcon ? undefined : endIconProp ? endIconProp : isOverflow ? (0, _jsxRuntime.jsx)(_index.MoreIcon, {}) : (0, _jsxRuntime.jsx)(_index.ChevronDownIcon, {});
  const anchorOrigin = (0, _react.useMemo)(() => ({
    horizontal: menuAlignment,
    vertical: "bottom"
  }), [menuAlignment]);
  const transformOrigin = (0, _react.useMemo)(() => ({
    horizontal: menuAlignment,
    vertical: "top"
  }), [menuAlignment]);
  return (0, _jsxRuntime.jsxs)(StyledMenuButtonContainer, {
    hasVerticalDivider: hasVerticalDivider,
    odysseyDesignTokens: odysseyDesignTokens,
    verticalDividerAlignment: verticalDividerAlignment,
    children: [(0, _jsxRuntime.jsx)(_BaseButton.BaseButton, {
      ariaControls: isOpen ? `${uniqueId}-menu` : undefined,
      ariaDescribedBy: ariaDescribedBy,
      ariaExpanded: isOpen ? "true" : undefined,
      ariaHasPopup: "true",
      ariaLabel: ariaLabel,
      ariaLabelledBy: ariaLabelledBy,
      children: buttonChildren,
      endIcon: endIcon,
      id: `${uniqueId}-button`,
      isDisabled: isDisabled,
      label: buttonLabel,
      onClick: openMenu,
      size: size,
      testId: testId,
      tooltipText: tooltipText,
      translate: translate,
      variant: buttonVariant
    }), children && (0, _jsxRuntime.jsx)(_Menu2.default, {
      anchorEl: anchorEl,
      anchorOrigin: anchorOrigin,
      id: `${uniqueId}-menu`,
      MenuListProps: menuListProps,
      onClose: closeMenu,
      open: isOpen,
      transformOrigin: transformOrigin,
      children: (0, _jsxRuntime.jsx)(_MenuContext.MenuContext.Provider, {
        value: providerValue,
        children: children
      })
    }), popoverContent && (0, _jsxRuntime.jsx)(_Popover2.default, {
      anchorEl: anchorEl,
      anchorOrigin: anchorOrigin,
      onClose: closeMenu,
      open: isOpen,
      transformOrigin: transformOrigin,
      children: (0, _jsxRuntime.jsx)(_Box.Box, {
        sx: {
          padding: odysseyDesignTokens.Spacing4
        },
        children: popoverContent
      })
    })]
  });
};
const MemoizedBaseMenuButton = exports.BaseMenuButton = (0, _react.memo)(BaseMenuButton);
MemoizedBaseMenuButton.displayName = "BaseMenuButton";
//# sourceMappingURL=BaseMenuButton.cjs.map