/* top-nav-end.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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';
var containerStyles = {
  root: "_yyhy11wp _1e0c1txw _1bahh9n0 _1ul9zwfg _1d2y1ris _glte1ris _15ri1mjv _1gs5usvi _ndwch9n0",
  jiraProductLogoUpdate: "_15rip2n4",
  fullHeightSidebar: "_lcxv1wug"
};
var 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(_ref) {
  var children = _ref.children,
    _ref$label = _ref.label,
    label = _ref$label === void 0 ? 'Actions' : _ref$label,
    _ref$showMoreButtonLa = _ref.showMoreButtonLabel,
    showMoreButtonLabel = _ref$showMoreButtonLa === void 0 ? 'Show more' : _ref$showMoreButtonLa;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isOpen = _useState2[0],
    setIsOpen = _useState2[1];
  // Always setting to `false` for the initial render (will flip in an effect for mobile)
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isMobile = _useState4[0],
    setIsMobile = _useState4[1];
  // Note: `query` is a stable value between renders
  var 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(function () {
    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: function onClose() {
      return setIsOpen(false);
    },
    placement: "bottom-start",
    shouldRenderToParent: true,
    content: function content() {
      return /*#__PURE__*/React.createElement(HasCustomThemeContext.Provider, {
        value: false
      }, /*#__PURE__*/React.createElement(List, {
        xcss: cx(listStyles.root, listStyles.popupContainer)
      }, children));
    },
    trigger: function trigger(triggerProps) {
      return /*#__PURE__*/React.createElement(IconButton, _extends({}, triggerProps, {
        label: showMoreButtonLabel,
        isSelected: isOpen,
        onClick: function onClick() {
          return setIsOpen(!isOpen);
        },
        icon: ShowMoreHorizontalIcon,
        isTooltipDisabled: false
      }));
    }
  }) : /*#__PURE__*/React.createElement(List, {
    xcss: cx(listStyles.root, listStyles.hideOnSmallViewport)
  }, children));
}