"use strict";

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

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

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

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

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

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

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

var _uiIcons = require("@teamleader/ui-icons");

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

var _lodash = _interopRequireDefault(require("lodash.omit"));

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

var _reactResizeDetector = require("react-resize-detector");

var _index = require("../../index");

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

var _excluded = ["children", "className", "scrollable", "title", "form", "onSubmit", "tertiaryAction", "secondaryAction", "primaryAction", "leftAction", "onCloseClick"];

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 Dialog = function Dialog(_ref) {
  var children = _ref.children,
      className = _ref.className,
      _ref$scrollable = _ref.scrollable,
      scrollable = _ref$scrollable === void 0 ? true : _ref$scrollable,
      title = _ref.title,
      form = _ref.form,
      onSubmit = _ref.onSubmit,
      tertiaryAction = _ref.tertiaryAction,
      secondaryAction = _ref.secondaryAction,
      primaryAction = _ref.primaryAction,
      leftAction = _ref.leftAction,
      onCloseClick = _ref.onCloseClick,
      otherProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var bodyRef = (0, _react.useRef)(null);
  var dragHandleRef = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(true),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      showScrollShadow = _useState2[0],
      setShowScrollShadow = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
      reachedScrollEnd = _useState4[0],
      setReachedScrollEnd = _useState4[1];

  var handleScrollShadow = (0, _react.useCallback)(function () {
    var currentRef = bodyRef.current;

    if (currentRef) {
      setShowScrollShadow(currentRef.scrollHeight > currentRef.clientHeight);
    }
  }, [bodyRef]);
  (0, _react.useEffect)(function () {
    handleScrollShadow();
  }, [bodyRef, otherProps.active]);
  (0, _reactResizeDetector.useResizeDetector)({
    refreshMode: 'throttle',
    refreshRate: 250,
    onResize: handleScrollShadow,
    targetRef: bodyRef
  });

  var getHeader = function getHeader() {
    var dragIcon = /*#__PURE__*/_react.default.createElement("div", {
      className: _theme.default['drag-icon'],
      ref: dragHandleRef
    }, /*#__PURE__*/_react.default.createElement(_uiIcons.IconDragMediumFilled, null));

    return /*#__PURE__*/_react.default.createElement(_index.DialogBase.Header, {
      icon: dragIcon,
      onCloseClick: onCloseClick
    }, /*#__PURE__*/_react.default.createElement(_index.Heading3, {
      maxLines: 1
    }, title));
  };

  var getFooter = function getFooter() {
    return /*#__PURE__*/_react.default.createElement(_index.DialogBase.Footer, {
      display: "flex",
      justifyContent: leftAction ? 'space-between' : 'flex-end',
      className: (0, _classnames.default)((0, _defineProperty2.default)({}, _theme.default['scroll-shadow'], !reachedScrollEnd && showScrollShadow))
    }, leftAction && /*#__PURE__*/_react.default.createElement(_index.Button, leftAction), /*#__PURE__*/_react.default.createElement(_index.ButtonGroup, {
      justifyContent: "flex-end"
    }, tertiaryAction && /*#__PURE__*/_react.default.createElement(_index.Button, (0, _extends2.default)({}, tertiaryAction, {
      level: "link"
    })), secondaryAction && /*#__PURE__*/_react.default.createElement(_index.Button, secondaryAction), /*#__PURE__*/_react.default.createElement(_index.Button, (0, _extends2.default)({
      level: "primary"
    }, primaryAction))));
  };

  var classNames = (0, _classnames.default)(_theme.default['dialog'], className);
  var restProps = (0, _lodash.default)(otherProps, ['onCloseClick', 'primaryAction', 'secondaryAction', 'tertiaryAction', 'leftAction']);
  return /*#__PURE__*/_react.default.createElement(_index.DialogBase, (0, _extends2.default)({
    className: classNames
  }, restProps, {
    scrollable: false,
    initialFocusRef: bodyRef,
    dragHandleRef: dragHandleRef,
    form: form,
    onSubmit: onSubmit
  }), title && getHeader(), /*#__PURE__*/_react.default.createElement(_index.DialogBase.Body, {
    ref: bodyRef,
    scrollable: scrollable,
    handleShowScrollShadow: setReachedScrollEnd
  }, children), getFooter());
};

var _default = Dialog;
exports.default = _default;