'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var classnames = require('classnames');
var Close = require('@bigbinary/neeto-icons/Close');
var Portal = require('./Portal-nCvANFeL.js');
var Button = require('./Button.js');
require('react-router-dom');
require('qs');
var jsxRuntime = require('react/jsx-runtime');
require('@bigbinary/neeto-hotkeys');
require('./overlayManager.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('./index-CNZKW5X0.js');
require('@bigbinary/neeto-cist');
require('ramda');
require('./en-B1i-pQJm.js');
require('./index-BgzkP01c.js');
require('@babel/runtime/helpers/toConsumableArray');
require('dayjs');
require('dayjs/plugin/localeData');
require('dayjs/plugin/utc');
require('dayjs/plugin/weekday');
require('dayjs/plugin/weekOfYear');
require('i18next');
require('@babel/runtime/helpers/esm/extends');
require('@babel/runtime/helpers/esm/objectWithoutPropertiesLoose');
require('@babel/runtime/helpers/esm/inheritsLoose');
require('react-dom');
require('./Spinner.js');
require('./Tooltip.js');
require('@tippyjs/react');
require('tippy.js');
require('./usePrefersReducedMotion-D2Ck84HM.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var Close__default = /*#__PURE__*/_interopDefault(Close);

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__*/jsxRuntime.jsx("div", {
    "data-testid": "pane-body",
    className: classnames__default.default("neeto-ui-pane__body neeto-ui-flex neeto-ui-flex-col neeto-ui-items-start neeto-ui-justify-start", _defineProperty__default.default({
      "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__*/jsxRuntime.jsx("div", {
    className: classnames__default.default("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__*/jsxRuntime.jsx("div", {
    className: classnames__default.default("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__default.default(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__default.default(_ref, _excluded);
  var _useState = React.useState(false),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    hasTransitionCompleted = _useState2[0],
    setHasTransitionCompleted = _useState2[1];
  var paneWrapperRef = React.useRef(null);
  var backdropRef = React.useRef(null);
  var observerRef = React.useRef(new ResizeObserver(function (_ref2) {
    var _ref3 = _slicedToArray__default.default(_ref2, 1),
      entry = _ref3[0];
    return updateHeaderHeight(entry.target, paneWrapperRef);
  }));
  Portal.useOverlayManager(paneWrapperRef, isOpen);
  var _useOverlay = Portal.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;
  React.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__*/jsxRuntime.jsx(Portal.Portal, {
    bringToFront: isOpen,
    rootId: "neeto-ui-portal",
    children: /*#__PURE__*/jsxRuntime.jsx(Portal.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__*/jsxRuntime.jsx(Portal.Backdrop, {
        ref: backdropRef,
        className: classnames__default.default("neeto-ui-pane__backdrop neeto-ui-flex neeto-ui-justify-end", backdropClassName),
        children: /*#__PURE__*/jsxRuntime.jsxs("div", _objectSpread(_objectSpread({
          "data-testid": "pane-wrapper",
          ref: paneWrapperRef,
          className: classnames__default.default("neeto-ui-pane__wrapper", _defineProperty__default.default({
            "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__*/jsxRuntime.jsx(Button, {
            "aria-label": "Close",
            className: "neeto-ui-pane__close",
            "data-testid": "pane-close-button",
            icon: Close__default.default,
            size: "small",
            style: "text",
            onClick: handleOverlayClose
          }), hasTransitionCompleted && /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, {
            children: typeof children === "function" ? children({
              setFocusField: setFocusField
            }) : children
          })]
        }), "pane-wrapper")
      }, "pane-backdrop")
    })
  });
};
Pane.Header = Header;
Pane.Body = Body;
Pane.Footer = Footer;

module.exports = Pane;
//# sourceMappingURL=Pane.js.map
