"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DialogBase = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _uiUtilities = _interopRequireDefault(require("@teamleader/ui-utilities"));

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireDefault(require("react"));

var _reactDom = require("react-dom");

var _Transition = _interopRequireDefault(require("react-transition-group/Transition"));

var _useFocusTrap2 = _interopRequireDefault(require("../../utils/useFocusTrap"));

var _box = _interopRequireDefault(require("../box"));

var _Overlay = _interopRequireDefault(require("../overlay/Overlay"));

var DialogBody = _interopRequireWildcard(require("./Body"));

var DialogFooter = _interopRequireWildcard(require("./Footer"));

var DialogHeader = _interopRequireWildcard(require("./Header"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _useDraggable = _interopRequireDefault(require("./useDraggable"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var DialogBase = function DialogBase(_ref) {
  var _ref$active = _ref.active,
      active = _ref$active === void 0 ? false : _ref$active,
      _ref$backdrop = _ref.backdrop,
      backdrop = _ref$backdrop === void 0 ? 'dark' : _ref$backdrop,
      children = _ref.children,
      className = _ref.className,
      onEscKeyDown = _ref.onEscKeyDown,
      onOverlayClick = _ref.onOverlayClick,
      _ref$scrollable = _ref.scrollable,
      scrollable = _ref$scrollable === void 0 ? true : _ref$scrollable,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'medium' : _ref$size,
      initialFocusRef = _ref.initialFocusRef,
      dragHandleRef = _ref.dragHandleRef,
      form = _ref.form,
      onSubmit = _ref.onSubmit;

  var _useFocusTrap = (0, _useFocusTrap2.default)({
    active: active,
    initialFocusRef: initialFocusRef
  }),
      ref = _useFocusTrap.ref,
      FocusRing = _useFocusTrap.FocusRing;

  (0, _useDraggable.default)({
    active: active,
    dragTargetRef: ref,
    dragHandleRef: dragHandleRef
  });

  if (!active) {
    return null;
  }

  var dialog = /*#__PURE__*/_react.default.createElement(_Transition.default, {
    timeout: 0,
    in: active,
    appear: true
  }, function (state) {
    var _cx;

    var dialogClassNames = (0, _classnames.default)(_uiUtilities.default['box-shadow-300'], _theme.default['dialog-base'], _theme.default["is-".concat(size)], (_cx = {}, (0, _defineProperty2.default)(_cx, _theme.default['is-entering'], state === 'entering'), (0, _defineProperty2.default)(_cx, _theme.default['is-entered'], state === 'entered'), _cx), className);
    return /*#__PURE__*/_react.default.createElement(_Overlay.default, {
      active: active,
      backdrop: backdrop,
      className: _theme.default['overlay'],
      onOverlayClick: onOverlayClick,
      onEscKeyDown: onEscKeyDown
    }, /*#__PURE__*/_react.default.createElement(FocusRing, null, /*#__PURE__*/_react.default.createElement(_box.default, (0, _extends2.default)({
      element: form ? 'form' : 'div',
      ref: ref,
      "data-teamleader-ui": "dialog",
      className: dialogClassNames
    }, form && {
      onSubmit: onSubmit
    }), /*#__PURE__*/_react.default.createElement("div", {
      className: _theme.default['inner']
    }, scrollable ? /*#__PURE__*/_react.default.createElement(_box.default, {
      display: "flex",
      flexDirection: "column",
      overflowY: "auto"
    }, children) : children))));
  });

  return /*#__PURE__*/(0, _reactDom.createPortal)(dialog, document.body);
};

exports.DialogBase = DialogBase;
DialogBase.Header = DialogHeader.default;
DialogBase.Body = DialogBody.default;
DialogBase.Footer = DialogFooter.default;
var _default = DialogBase;
exports.default = _default;