import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useState, useRef, useEffect } from 'react';
import classnames from 'classnames';
import Close from '@bigbinary/neeto-icons/Close';
import { u as useOverlayManager, a as useOverlay, P as Portal, C as CSSTransition, B as Backdrop } from './Portal-qx5uRFGk.js';
import Button from './Button.js';
import 'react-router-dom';
import 'qs';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import '@bigbinary/neeto-hotkeys';
import './overlayManager.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import './index-QWVp_Xt6.js';
import '@bigbinary/neeto-cist';
import 'ramda';
import './en-Dmu3B4Vs.js';
import './index-DyUNP5G9.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'dayjs';
import 'dayjs/plugin/localeData';
import 'dayjs/plugin/utc';
import 'dayjs/plugin/weekday';
import 'dayjs/plugin/weekOfYear';
import 'i18next';
import '@babel/runtime/helpers/esm/extends';
import '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
import '@babel/runtime/helpers/esm/inheritsLoose';
import 'react-dom';
import './Spinner.js';
import './Tooltip.js';
import '@tippyjs/react';
import 'tippy.js';
import './usePrefersReducedMotion-n4ZJtExQ.js';

var Body = function Body(_ref) {
  var children = _ref.children,
    className = _ref.className,
    _ref$hasFooter = _ref.hasFooter,
    hasFooter = _ref$hasFooter === void 0 ? true : _ref$hasFooter;
  return /*#__PURE__*/jsx("div", {
    "data-testid": "pane-body",
    className: classnames("neeto-ui-pane__body neeto-ui-flex neeto-ui-flex-col neeto-ui-items-start neeto-ui-justify-start", _defineProperty({
      "neeto-ui-pane__body--has-footer": hasFooter
    }, className, className)),
    children: children
  });
};

var Footer = function Footer(_ref) {
  var children = _ref.children,
    className = _ref.className;
  return /*#__PURE__*/jsx("div", {
    className: classnames("neeto-ui-pane__footer neeto-ui-flex neeto-ui-items-center", className),
    children: children
  });
};

var Header = function Header(_ref) {
  var children = _ref.children,
    className = _ref.className;
  return /*#__PURE__*/jsx("div", {
    className: classnames("neeto-ui-pane__header", className),
    "data-testid": "pane-header",
    children: children
  });
};

var DEFAULT_PANE_HEADER_HEIGHT = 78;

var getHeader = function getHeader(paneWrapperRef) {
  var _paneWrapperRef$curre;
  return (_paneWrapperRef$curre = paneWrapperRef.current) === null || _paneWrapperRef$curre === void 0 ? void 0 : _paneWrapperRef$curre.querySelector(".neeto-ui-pane__header");
};
var updateHeaderHeight = function updateHeaderHeight(header, paneWrapperRef) {
  var headerHeight = header === null || header === void 0 ? void 0 : header.offsetHeight;
  if (headerHeight > DEFAULT_PANE_HEADER_HEIGHT) {
    var _paneWrapperRef$curre2;
    (_paneWrapperRef$curre2 = paneWrapperRef.current) === null || _paneWrapperRef$curre2 === void 0 ? void 0 : _paneWrapperRef$curre2.style.setProperty("--neeto-ui-pane-header-height", "".concat(headerHeight, "px"));
  } else {
    var _paneWrapperRef$curre3;
    (_paneWrapperRef$curre3 = paneWrapperRef.current) === null || _paneWrapperRef$curre3 === void 0 ? void 0 : _paneWrapperRef$curre3.style.removeProperty("--neeto-ui-pane-header-height");
  }
};

var _excluded = ["size", "isOpen", "onClose", "children", "className", "closeOnEsc", "closeButton", "backdropClassName", "closeOnOutsideClick", "initialFocusRef", "finalFocusRef"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SIZES = {
  small: "small",
  large: "large",
  extraLarge: "extraLarge"
};
var Pane = function Pane(_ref) {
  var _ref$size = _ref.size,
    size = _ref$size === void 0 ? SIZES.small : _ref$size,
    _ref$isOpen = _ref.isOpen,
    isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen,
    _ref$onClose = _ref.onClose,
    onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
    children = _ref.children,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? "" : _ref$className,
    _ref$closeOnEsc = _ref.closeOnEsc,
    closeOnEsc = _ref$closeOnEsc === void 0 ? true : _ref$closeOnEsc,
    _ref$closeButton = _ref.closeButton,
    closeButton = _ref$closeButton === void 0 ? true : _ref$closeButton,
    _ref$backdropClassNam = _ref.backdropClassName,
    backdropClassName = _ref$backdropClassNam === void 0 ? "" : _ref$backdropClassNam,
    _ref$closeOnOutsideCl = _ref.closeOnOutsideClick,
    closeOnOutsideClick = _ref$closeOnOutsideCl === void 0 ? true : _ref$closeOnOutsideCl,
    initialFocusRef = _ref.initialFocusRef,
    finalFocusRef = _ref.finalFocusRef,
    otherProps = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    hasTransitionCompleted = _useState2[0],
    setHasTransitionCompleted = _useState2[1];
  var paneWrapperRef = useRef(null);
  var backdropRef = useRef(null);
  var observerRef = useRef(new ResizeObserver(function (_ref2) {
    var _ref3 = _slicedToArray(_ref2, 1),
      entry = _ref3[0];
    return updateHeaderHeight(entry.target, paneWrapperRef);
  }));
  useOverlayManager(paneWrapperRef, isOpen);
  var _useOverlay = useOverlay({
      overlayWrapper: paneWrapperRef,
      backdropRef: backdropRef,
      closeOnOutsideClick: closeOnOutsideClick,
      closeOnEsc: closeOnEsc,
      onClose: onClose,
      isOpen: isOpen,
      initialFocusRef: initialFocusRef,
      finalFocusRef: finalFocusRef,
      hasTransitionCompleted: hasTransitionCompleted
    }),
    handleOverlayClose = _useOverlay.handleOverlayClose,
    setFocusField = _useOverlay.setFocusField,
    isTopOverlay = _useOverlay.isTopOverlay;
  useEffect(function () {
    if (!hasTransitionCompleted || !paneWrapperRef.current) return undefined;
    var observer = observerRef.current;
    var header = getHeader(paneWrapperRef);
    if (header) {
      observer.observe(header);
      return function () {
        return observer.disconnect();
      };
    }
    var mutationObserver = new MutationObserver(function () {
      var header = getHeader(paneWrapperRef);
      if (!header) return;
      observer.observe(header);
      mutationObserver.disconnect();
    });
    mutationObserver.observe(paneWrapperRef.current, {
      childList: true,
      subtree: true
    });
    return function () {
      mutationObserver.disconnect();
      observer.disconnect();
    };
  }, [hasTransitionCompleted, isTopOverlay]);
  return /*#__PURE__*/jsx(Portal, {
    bringToFront: isOpen,
    rootId: "neeto-ui-portal",
    children: /*#__PURE__*/jsx(CSSTransition, {
      unmountOnExit: true,
      appear: isOpen,
      classNames: "neeto-ui-pane",
      "in": isOpen,
      timeout: 230,
      onEntered: function onEntered() {
        return setHasTransitionCompleted(true);
      },
      onExited: function onExited() {
        return setHasTransitionCompleted(false);
      },
      children: /*#__PURE__*/jsx(Backdrop, {
        ref: backdropRef,
        className: classnames("neeto-ui-pane__backdrop neeto-ui-flex neeto-ui-justify-end", backdropClassName),
        children: /*#__PURE__*/jsxs("div", _objectSpread(_objectSpread({
          "data-testid": "pane-wrapper",
          ref: paneWrapperRef,
          className: classnames("neeto-ui-pane__wrapper", _defineProperty({
            "neeto-ui-pane__wrapper--small": size === SIZES.small,
            "neeto-ui-pane__wrapper--large": size === SIZES.large,
            "neeto-ui-pane__wrapper--extralarge": size === SIZES.extraLarge
          }, className, className))
        }, otherProps), {}, {
          children: [closeButton && /*#__PURE__*/jsx(Button, {
            "aria-label": "Close",
            className: "neeto-ui-pane__close",
            "data-testid": "pane-close-button",
            icon: Close,
            size: "small",
            style: "text",
            onClick: handleOverlayClose
          }), hasTransitionCompleted && /*#__PURE__*/jsx(Fragment, {
            children: typeof children === "function" ? children({
              setFocusField: setFocusField
            }) : children
          })]
        }), "pane-wrapper")
      }, "pane-backdrop")
    })
  });
};
Pane.Header = Header;
Pane.Body = Body;
Pane.Footer = Footer;

export { Pane as default };
//# sourceMappingURL=Pane.js.map
