/* top-nav-end.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./top-nav-end.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useState } from 'react';
import { cx } from '@compiled/react';
import { useLayoutEffect } from '@atlaskit/ds-lib/use-layout-effect';
import ShowMoreHorizontalIcon from '@atlaskit/icon/core/show-more-horizontal';
import { fg } from '@atlaskit/platform-feature-flags';
import Popup from '@atlaskit/popup';
import { UNSAFE_useMediaQuery as useMediaQuery } from '@atlaskit/primitives/compiled';
import { List } from '../../../components/list';
import { HasCustomThemeContext } from '../../top-nav-items/themed/has-custom-theme-context';
import { IconButton } from '../../top-nav-items/themed/migration';
const containerStyles = {
  root: "_yyhy11wp _1e0c1txw _1bahh9n0 _1ul9zwfg _1d2y1ris _glte1ris _15ri1mjv _1gs5usvi _ndwch9n0",
  jiraProductLogoUpdate: "_15rip2n4",
  fullHeightSidebar: "_lcxv1wug _y4tiutpp"
};
const listStyles = {
  root: "_zulp1b66 _4cvr1h6o _1e0c1txw",
  hideOnSmallViewport: "_1e0cglyw _181n1txw",
  popupContainer: "_1yt4u2gc"
};

/**
 * __TopNavEnd__
 *
 * Wrapper for the top navigation actions on the inline-end (right) side of the top navigation.
 *
 * On small viewports, the children will be placed inside a popup, and a button will be rendered to open the popup.
 * This is to prevent the actions from overflowing the top navigation.
 */
export function TopNavEnd({
  children,
  label = 'Actions',
  showMoreButtonLabel = 'Show more'
}) {
  const [isOpen, setIsOpen] = useState(false);
  // Always setting to `false` for the initial render (will flip in an effect for mobile)
  const [isMobile, setIsMobile] = useState(false);
  // Note: `query` is a stable value between renders
  const query = useMediaQuery('below.sm', function onChange(event) {
    setIsMobile(event.matches);
    // Any time we shift between mobile and desktop
    // We always close the popup
    setIsOpen(false);
  });
  useLayoutEffect(() => {
    var _query$matches;
    // The default is `false`, and desktop will remain as `false` (no re-render).
    // Mobile will flip from `false` to `true`.
    setIsMobile((_query$matches = query === null || query === void 0 ? void 0 : query.matches) !== null && _query$matches !== void 0 ? _query$matches : false);
  }, [query]);
  return /*#__PURE__*/React.createElement("nav", {
    "aria-label": label,
    className: ax([containerStyles.root, fg('navx-full-height-sidebar') && containerStyles.fullHeightSidebar, fg('team25-eu-jira-logo-updates-csm-jsm') && containerStyles.jiraProductLogoUpdate])
  }, isMobile ? /*#__PURE__*/React.createElement(Popup, {
    isOpen: isOpen,
    onClose: () => setIsOpen(false),
    placement: "bottom-start",
    shouldRenderToParent: true,
    content: () => /*#__PURE__*/React.createElement(HasCustomThemeContext.Provider, {
      value: false
    }, /*#__PURE__*/React.createElement(List, {
      xcss: cx(listStyles.root, listStyles.popupContainer)
    }, children)),
    trigger: triggerProps => /*#__PURE__*/React.createElement(IconButton, _extends({}, triggerProps, {
      label: showMoreButtonLabel,
      isSelected: isOpen,
      onClick: () => setIsOpen(!isOpen),
      icon: ShowMoreHorizontalIcon,
      isTooltipDisabled: false
    }))
  }) : /*#__PURE__*/React.createElement(List, {
    xcss: cx(listStyles.root, listStyles.hideOnSmallViewport)
  }, children));
}