"use strict";

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

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

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

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

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

var _bem = require("@zarm-design/bem");

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

var _configProvider = require("../config-provider");

var _mask = _interopRequireDefault(require("../mask"));

var _transition = _interopRequireDefault(require("../transition"));

var _trigger = _interopRequireDefault(require("../trigger"));

var _dom = require("../utils/dom");

var _hooks = require("../utils/hooks");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var TRANSITION_NAMES = {
  top: 'move-down',
  bottom: 'move-up',
  center: 'fade',
  left: 'move-left',
  right: 'move-right'
};
var Popup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var width = props.width,
      destroy = props.destroy,
      forceRender = props.forceRender,
      visible = props.visible,
      animationType = props.animationType,
      animationDuration = props.animationDuration,
      lockScroll = props.lockScroll,
      direction = props.direction,
      mask = props.mask,
      maskClassName = props.maskClassName,
      maskStyle = props.maskStyle,
      maskColor = props.maskColor,
      maskOpacity = props.maskOpacity,
      afterOpen = props.afterOpen,
      afterClose = props.afterClose,
      onOpen = props.onOpen,
      onClose = props.onClose,
      onMaskClick = props.onMaskClick,
      onEsc = props.onEsc,
      children = props.children;

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls,
      globalMountContainer = _React$useContext.mountContainer;

  var nodeRef = React.useRef(null);
  var maskRef = React.useRef(null);
  var bem = (0, _bem.createBEM)('popup', {
    prefixCls: prefixCls
  });
  (0, _hooks.useLockScroll)(visible && lockScroll);
  var handleEsc = React.useCallback(function () {
    onEsc === null || onEsc === void 0 ? void 0 : onEsc();
  }, []);

  var handleClick = function handleClick(event) {
    var _maskRef$current;

    if (nodeRef.current !== event.target && nodeRef.current.contains(event.target)) {
      return;
    }

    (_maskRef$current = maskRef.current) === null || _maskRef$current === void 0 ? void 0 : _maskRef$current.click();
  };

  var transitionName = animationType !== null && animationType !== void 0 ? animationType : TRANSITION_NAMES[direction];
  React.useImperativeHandle(ref, function () {
    return nodeRef.current;
  });
  return /*#__PURE__*/React.createElement(_trigger.default, {
    visible: visible,
    onClose: handleEsc
  }, mask && /*#__PURE__*/React.createElement(_mask.default, {
    ref: maskRef,
    className: maskClassName,
    style: maskStyle,
    visible: visible,
    color: maskColor,
    opacity: maskOpacity,
    animationDuration: animationDuration,
    mountContainer: props.mountContainer,
    forceRender: forceRender,
    destroy: destroy,
    onClick: function onClick() {
      onMaskClick === null || onMaskClick === void 0 ? void 0 : onMaskClick();
    }
  }), /*#__PURE__*/React.createElement(_transition.default, {
    nodeRef: nodeRef,
    visible: visible,
    tranisitionName: "".concat(prefixCls, "-").concat(transitionName),
    duration: animationDuration,
    forceRender: forceRender,
    destroy: destroy,
    onEnter: function onEnter() {
      afterOpen === null || afterOpen === void 0 ? void 0 : afterOpen();
    },
    onEnterActive: function onEnterActive() {
      onOpen === null || onOpen === void 0 ? void 0 : onOpen();
    },
    onLeaveActive: function onLeaveActive() {
      onClose === null || onClose === void 0 ? void 0 : onClose();
    },
    onLeaveEnd: function onLeaveEnd() {
      afterClose === null || afterClose === void 0 ? void 0 : afterClose();
    }
  }, function (_ref, setNodeRef) {
    var _props$mountContainer;

    var className = _ref.className,
        style = _ref.style;
    var display = style.display,
        restStyle = (0, _objectWithoutProperties2.default)(style, ["display"]);
    return (0, _dom.renderToContainer)((_props$mountContainer = props.mountContainer) !== null && _props$mountContainer !== void 0 ? _props$mountContainer : globalMountContainer, /*#__PURE__*/React.createElement("div", {
      className: bem('wrapper', [{
        center: direction === 'center'
      }, props.className]),
      style: _objectSpread(_objectSpread({}, props.style), {}, {
        display: display
      }),
      onClick: handleClick
    }, /*#__PURE__*/React.createElement("div", {
      ref: setNodeRef,
      className: bem([(0, _defineProperty2.default)({}, "".concat(direction), !!direction), className]),
      style: _objectSpread(_objectSpread({}, restStyle), {}, {
        width: width
      })
    }, children)));
  }));
});
Popup.displayName = 'Popup';
Popup.defaultProps = {
  visible: false,
  mask: true,
  direction: 'bottom',
  destroy: true,
  lockScroll: true
};
var _default = Popup;
exports.default = _default;