var _excluded = ["title", "children", "variant"],
  _excluded2 = ["children", "qa"],
  _excluded3 = ["children", "qa"],
  _excluded4 = ["children", "onClickBack", "onClose"],
  _excluded5 = ["children"],
  _excluded6 = ["children"],
  _excluded7 = ["children"],
  _excluded8 = ["children"],
  _excluded9 = ["aria-labelledby", "children"];
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import { ChevronLeft, Clear, EllipsisVertical } from '@procore/core-icons/dist';
import { useId } from '@react-aria/utils';
import React from 'react';
import { Button } from '../Button';
import { Menu } from '../Menu';
import { OverlayTrigger } from '../OverlayTrigger';
import { LevelContext, Section, SectionProvider } from '../Section';
import { Tooltip } from '../Tooltip';
import { useI18nContext } from '../_hooks/I18n';
import { useOverflowObserver } from '../_hooks/OverflowObserver';
import { addSubcomponents } from '../_utils/addSubcomponents';
import { isReactElement } from '../_utils/isReactElement';
import { mergeRefs } from '../_utils/mergeRefs';
import { partition } from '../_utils/partition';
import { StyledBody, StyledCloseButton, StyledFooter, StyledFooterActions, StyledFooterNotation, StyledHeader, StyledHeaderActions, StyledHeaderBackAction, StyledMoreMenu, StyledPanel, StyledTitle } from './Panel.styles';
var PanelContext = /*#__PURE__*/React.createContext({
  ariaLabelledBy: undefined,
  isScrolling: false,
  setIsScrolling: function setIsScrolling() {}
});
var usePanelContext = function usePanelContext() {
  return React.useContext(PanelContext);
};
var maxVisibleIcons = 3;
var HeaderAction = /*#__PURE__*/React.forwardRef(function HeaderAction(_ref, ref) {
  var title = _ref.title,
    children = _ref.children,
    _ref$variant = _ref.variant,
    variant = _ref$variant === void 0 ? 'tertiary' : _ref$variant,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/React.createElement(Tooltip, {
    overlay: title,
    trigger: ['hover', 'focus'],
    ref: ref
  }, /*#__PURE__*/React.createElement(Button, _extends({
    "aria-label": title
  }, props, {
    variant: variant
  }), children));
});
var MoreMenu = /*#__PURE__*/React.forwardRef(function MoreMenu(_ref2, ref) {
  var children = _ref2.children;
  return /*#__PURE__*/React.createElement(StyledMoreMenu, {
    ref: ref
  }, /*#__PURE__*/React.createElement(Menu, {
    ref: function ref(node) {
      return node === null || node === void 0 ? void 0 : node.focus();
    }
  }, /*#__PURE__*/React.createElement(Menu.Options, null, React.Children.map(children, function (child) {
    if (isReactElement(child)) {
      return /*#__PURE__*/React.createElement(Menu.Item, {
        item: null,
        key: child.props.title,
        onClick: child.props.onClick
      }, child.props.title);
    }
  }))));
});
export var MoreIcon = function MoreIcon(_ref3) {
  var children = _ref3.children,
    qa = _ref3.qa;
  var i18n = useI18nContext();
  return /*#__PURE__*/React.createElement(OverlayTrigger, {
    overlay: /*#__PURE__*/React.createElement(MoreMenu, null, children),
    placement: "bottom-right",
    trackAriaExpanded: true,
    trigger: "click"
  }, /*#__PURE__*/React.createElement(Button, {
    "aria-haspopup": "menu",
    "aria-label": i18n.t('core.dropdown.moreOptions'),
    "data-qa": qa === null || qa === void 0 ? void 0 : qa.moreIcon,
    icon: /*#__PURE__*/React.createElement(EllipsisVertical, null),
    variant: "tertiary"
  }));
};
var HeaderActions = /*#__PURE__*/React.forwardRef(function HeaderActions(_ref4, ref) {
  var children = _ref4.children,
    qa = _ref4.qa,
    props = _objectWithoutProperties(_ref4, _excluded2);
  var iconsAmount = React.Children.count(children);
  var _ref5 = iconsAmount > maxVisibleIcons ? partition(function (_, index) {
      return index > iconsAmount - maxVisibleIcons;
    }, React.Children.toArray(children)) : [children, []],
    _ref6 = _slicedToArray(_ref5, 2),
    visibleIcons = _ref6[0],
    hiddenIcons = _ref6[1];
  return /*#__PURE__*/React.createElement(StyledHeaderActions, _extends({}, props, {
    ref: ref
  }), visibleIcons, hiddenIcons.length > 0 && /*#__PURE__*/React.createElement(MoreIcon, {
    qa: qa
  }, hiddenIcons));
});
export var TitleTooltip = function TitleTooltip(_ref7) {
  var children = _ref7.children,
    overlay = _ref7.overlay,
    isVisible = _ref7.isVisible;
  if (!isVisible) {
    return children;
  }
  return /*#__PURE__*/React.createElement(Tooltip, {
    trigger: ['hover', 'focus'],
    overlay: /*#__PURE__*/React.createElement(Tooltip.Content, null, overlay),
    placement: "bottom",
    role: "tooltip"
  }, children);
};
var Title = /*#__PURE__*/React.forwardRef(function Title(_ref8, ref) {
  var children = _ref8.children,
    qa = _ref8.qa,
    props = _objectWithoutProperties(_ref8, _excluded3);
  var _useOverflowObserver = useOverflowObserver(),
    isOverflowingX = _useOverflowObserver.isOverflowingX,
    overflowRef = _useOverflowObserver.ref;
  var _usePanelContext = usePanelContext(),
    ariaLabelledBy = _usePanelContext.ariaLabelledBy;
  var headingId = props.id || ariaLabelledBy;
  return /*#__PURE__*/React.createElement(TitleTooltip, {
    isVisible: isOverflowingX,
    overlay: children
  }, /*#__PURE__*/React.createElement(StyledTitle, _extends({
    id: headingId
  }, props, {
    ref: mergeRefs(overflowRef, ref),
    "data-qa": qa === null || qa === void 0 ? void 0 : qa.title
  }), children));
});
var Header = /*#__PURE__*/React.forwardRef(function Header(_ref9, ref) {
  var children = _ref9.children,
    onClickBack = _ref9.onClickBack,
    onClose = _ref9.onClose,
    props = _objectWithoutProperties(_ref9, _excluded4);
  var I18n = useI18nContext();
  return /*#__PURE__*/React.createElement(StyledHeader, _extends({}, props, {
    ref: ref
  }), /*#__PURE__*/React.createElement(StyledHeaderBackAction, {
    $active: onClickBack !== undefined
  }, /*#__PURE__*/React.createElement(Button, {
    "aria-label": I18n.t('core.panel.goBack'),
    variant: "tertiary",
    onClick: onClickBack,
    icon: /*#__PURE__*/React.createElement(ChevronLeft, null)
  })), children, typeof onClose === 'function' && /*#__PURE__*/React.createElement(StyledCloseButton, null, /*#__PURE__*/React.createElement(HeaderAction, {
    title: I18n.t('core.panel.close'),
    icon: /*#__PURE__*/React.createElement(Clear, null),
    onClick: onClose
  })));
});
var Body = /*#__PURE__*/React.forwardRef(function Body(_ref0, ref) {
  var children = _ref0.children,
    props = _objectWithoutProperties(_ref0, _excluded5);
  var _usePanelContext2 = usePanelContext(),
    ariaLabelledBy = _usePanelContext2.ariaLabelledBy,
    setIsScrolling = _usePanelContext2.setIsScrolling;
  var timer = React.useRef(null);
  var handleScroll = React.useCallback(function () {
    setIsScrolling(true);
    if (timer.current) {
      clearTimeout(timer.current);
    }
    timer.current = setTimeout(function () {
      setIsScrolling(false);
    }, 100);
  }, [setIsScrolling]);
  return /*#__PURE__*/React.createElement(SectionProvider, null, /*#__PURE__*/React.createElement(LevelContext.Provider, {
    value: 3
  }, /*#__PURE__*/React.createElement(StyledBody, _extends({}, props, {
    ref: ref,
    tabIndex: 0,
    role: "region",
    "aria-labelledby": ariaLabelledBy,
    onScroll: handleScroll
  }), children)));
});
var FooterNotation = /*#__PURE__*/React.forwardRef(function FooterNotation(_ref1, ref) {
  var children = _ref1.children,
    props = _objectWithoutProperties(_ref1, _excluded6);
  return /*#__PURE__*/React.createElement(StyledFooterNotation, _extends({}, props, {
    ref: ref
  }), children);
});
var FooterActions = /*#__PURE__*/React.forwardRef(function FooterActions(_ref10, ref) {
  var children = _ref10.children,
    props = _objectWithoutProperties(_ref10, _excluded7);
  return /*#__PURE__*/React.createElement(StyledFooterActions, _extends({}, props, {
    ref: ref
  }), children);
});
var Footer = /*#__PURE__*/React.forwardRef(function Footer(_ref11, ref) {
  var children = _ref11.children,
    props = _objectWithoutProperties(_ref11, _excluded8);
  var _usePanelContext3 = usePanelContext(),
    isScrolling = _usePanelContext3.isScrolling;
  return /*#__PURE__*/React.createElement(StyledFooter, _extends({}, props, {
    ref: ref,
    $hasShadow: isScrolling
  }), children);
});
var Panel_ = /*#__PURE__*/React.forwardRef(function Panel(_ref12, ref) {
  var ariaLabelledByProp = _ref12['aria-labelledby'],
    children = _ref12.children,
    props = _objectWithoutProperties(_ref12, _excluded9);
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isScrolling = _React$useState2[0],
    setIsScrolling = _React$useState2[1];
  var defaultLabelId = useId();
  var ariaLabelledBy = ariaLabelledByProp !== null && ariaLabelledByProp !== void 0 ? ariaLabelledByProp : defaultLabelId;
  return /*#__PURE__*/React.createElement(PanelContext.Provider, {
    value: {
      ariaLabelledBy: ariaLabelledBy,
      isScrolling: isScrolling,
      setIsScrolling: setIsScrolling
    }
  }, /*#__PURE__*/React.createElement(StyledPanel, _extends({}, props, {
    ref: ref
  }), children));
});
Panel_.displayName = 'Panel';
Body.displayName = 'Panel.Body';
Footer.displayName = 'Panel.Footer';
FooterActions.displayName = 'Panel.FooterActions';
FooterNotation.displayName = 'Panel.FooterNotation';
Header.displayName = 'Panel.Header';
HeaderAction.displayName = 'Panel.HeaderAction';
HeaderActions.displayName = 'Panel.HeaderActions';
Title.displayName = 'Panel.Title';

/**

 Panels help users complete tasks or view additional information alongside the
 main content of a page.

 Panels are often initiated by clicking on an item somewhere on the main page,
 and they often contain additional information or options specific to that item.

 @since 10.19.0

 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-panel--demo)

 @see [Design Guidelines](https://design.procore.com/panel)

 */
export var Panel = addSubcomponents({
  Body: Body,
  Footer: Footer,
  FooterActions: FooterActions,
  FooterNotation: FooterNotation,
  Header: Header,
  HeaderAction: HeaderAction,
  HeaderActions: HeaderActions,
  Section: Section,
  Title: Title
}, Panel_);
//# sourceMappingURL=Panel.js.map