import _Popover from "@mui/material/Popover";
import _Menu from "@mui/material/Menu";
/*!
 * 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.
 */

import styled from "@emotion/styled";
import { memo, useCallback, useMemo, useState } from "react";
import { Box } from "../Box.js";
import { ChevronDownIcon, MoreIcon } from "../icons.generated/index.js";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
import { useUniqueId } from "../useUniqueId.js";
import { BaseButton } from "./BaseButton.js";
import { MenuContext } from "./MenuContext.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const menuAlignmentValues = ["left", "right"];
export const verticalDividerAlignmentValues = ["start", "end"];
const StyledMenuButtonContainer = styled("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 = useOdysseyDesignTokens();
  const [anchorEl, setAnchorEl] = useState(null);
  const isOpen = Boolean(anchorEl);
  const closeMenu = useCallback(() => {
    setAnchorEl(null);
  }, []);
  const openMenu = useCallback(event => {
    setAnchorEl(event.currentTarget);
  }, []);
  const uniqueId = useUniqueId(idOverride);
  const menuListProps = useMemo(() => ({
    "aria-labelledby": `${uniqueId}-button`
  }), [uniqueId]);
  const providerValue = useMemo(() => ({
    closeMenu,
    openMenu,
    shouldCloseOnSelect
  }), [closeMenu, openMenu, shouldCloseOnSelect]);
  const endIcon = omitEndIcon ? undefined : endIconProp ? endIconProp : isOverflow ? _jsx(MoreIcon, {}) : _jsx(ChevronDownIcon, {});
  const anchorOrigin = useMemo(() => ({
    horizontal: menuAlignment,
    vertical: "bottom"
  }), [menuAlignment]);
  const transformOrigin = useMemo(() => ({
    horizontal: menuAlignment,
    vertical: "top"
  }), [menuAlignment]);
  return _jsxs(StyledMenuButtonContainer, {
    hasVerticalDivider: hasVerticalDivider,
    odysseyDesignTokens: odysseyDesignTokens,
    verticalDividerAlignment: verticalDividerAlignment,
    children: [_jsx(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 && _jsx(_Menu, {
      anchorEl: anchorEl,
      anchorOrigin: anchorOrigin,
      id: `${uniqueId}-menu`,
      MenuListProps: menuListProps,
      onClose: closeMenu,
      open: isOpen,
      transformOrigin: transformOrigin,
      children: _jsx(MenuContext.Provider, {
        value: providerValue,
        children: children
      })
    }), popoverContent && _jsx(_Popover, {
      anchorEl: anchorEl,
      anchorOrigin: anchorOrigin,
      onClose: closeMenu,
      open: isOpen,
      transformOrigin: transformOrigin,
      children: _jsx(Box, {
        sx: {
          padding: odysseyDesignTokens.Spacing4
        },
        children: popoverContent
      })
    })]
  });
};
const MemoizedBaseMenuButton = memo(BaseMenuButton);
MemoizedBaseMenuButton.displayName = "BaseMenuButton";
export { MemoizedBaseMenuButton as BaseMenuButton };
//# sourceMappingURL=BaseMenuButton.js.map