"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

exports.__esModule = true;
exports.useBodyStyles = void 0;

var _react = require("react");

var _elementResizeEvent = _interopRequireWildcard(require("element-resize-event"));

var _DOMHelper = _interopRequireDefault(require("../DOMHelper"));

var useBodyStyles = function useBodyStyles(ref, options) {
  var _useState = (0, _react.useState)({}),
      bodyStyles = _useState[0],
      setBodyStyles = _useState[1];

  var overflow = options.overflow,
      drawer = options.drawer,
      prefix = options.prefix;
  var windowResizeListener = (0, _react.useRef)();
  var contentElement = (0, _react.useRef)();
  var updateBodyStyles = (0, _react.useCallback)(function (_event, entering) {
    var dialog = ref.current;
    var scrollHeight = dialog ? dialog.scrollHeight : 0;
    var styles = {
      overflow: 'auto'
    };

    if (dialog) {
      // default margin
      var headerHeight = 46;
      var footerHeight = 46;
      var contentHeight = 30;
      var headerDOM = dialog.querySelector("." + prefix('header'));
      var footerDOM = dialog.querySelector("." + prefix('footer'));
      var contentDOM = dialog.querySelector("." + prefix('content'));
      headerHeight = headerDOM ? _DOMHelper.default.getHeight(headerDOM) + headerHeight : headerHeight;
      footerHeight = footerDOM ? _DOMHelper.default.getHeight(footerDOM) + footerHeight : footerHeight;
      contentHeight = contentDOM ? _DOMHelper.default.getHeight(contentDOM) + contentHeight : contentHeight;

      if (drawer) {
        styles.height = contentHeight - (headerHeight + footerHeight);
      } else {
        /**
         * Header height + Footer height + Dialog margin
         */
        var excludeHeight = headerHeight + footerHeight + (entering ? 70 : 60);
        var bodyHeight = _DOMHelper.default.getHeight(window) - excludeHeight;
        var maxHeight = scrollHeight >= bodyHeight ? bodyHeight : scrollHeight;
        styles.maxHeight = maxHeight;
      }
    }

    setBodyStyles(styles);
  }, [drawer, prefix, ref]);
  var onDestroyEvents = (0, _react.useCallback)(function () {
    var _windowResizeListener, _windowResizeListener2;

    (_windowResizeListener = windowResizeListener.current) === null || _windowResizeListener === void 0 ? void 0 : (_windowResizeListener2 = _windowResizeListener.off) === null || _windowResizeListener2 === void 0 ? void 0 : _windowResizeListener2.call(_windowResizeListener);

    if (contentElement.current) {
      (0, _elementResizeEvent.unbind)(contentElement.current);
    }
  }, []);
  var onChangeBodyStyles = (0, _react.useCallback)(function (entering) {
    if (overflow) {
      var _ref$current;

      updateBodyStyles(null, entering);
      contentElement.current = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.querySelector("." + prefix('content'));
      windowResizeListener.current = _DOMHelper.default.on(window, 'resize', updateBodyStyles);
      (0, _elementResizeEvent.default)(contentElement.current, updateBodyStyles);
    }
  }, [overflow, prefix, ref, updateBodyStyles]);
  (0, _react.useEffect)(function () {
    onDestroyEvents();
  }, [onDestroyEvents]);
  return [overflow ? bodyStyles : {}, onChangeBodyStyles, onDestroyEvents];
};

exports.useBodyStyles = useBodyStyles;