var _excluded = ["safeArea", "portal", "animation", "referenceDOM", "offset", "visible", "maskClosable", "onVisibleChange", "maskProps", "children"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useImperativeHandle, useRef, forwardRef, useEffect } from 'react';
import { createPortal } from 'react-dom';
import getClassNameByAnimation from "./../api/getClassNameByAnimation";

// 内库使用-start
import DOMUtil from "./../../../utils/DOMUtil";
import SafeArea from "./../../SafeArea";
import Tooltip from "./../../Tooltip";
// 内库使用-end

/* 测试使用-start
import { DOMUtil, SafeArea, Tooltip } from 'seedsui-react'
测试使用-end */

var Modal = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _maskProps4;
  var safeArea = _ref.safeArea,
    portal = _ref.portal,
    _ref$animation = _ref.animation,
    animation = _ref$animation === void 0 ? 'zoom' : _ref$animation,
    _ref$referenceDOM = _ref.referenceDOM,
    externalReferenceDOM = _ref$referenceDOM === void 0 ? null : _ref$referenceDOM,
    _ref$offset = _ref.offset,
    offset = _ref$offset === void 0 ? null : _ref$offset,
    visible = _ref.visible,
    _ref$maskClosable = _ref.maskClosable,
    maskClosable = _ref$maskClosable === void 0 ? true : _ref$maskClosable,
    onVisibleChange = _ref.onVisibleChange,
    _ref$maskProps = _ref.maskProps,
    maskProps = _ref$maskProps === void 0 ? {} : _ref$maskProps,
    children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  // 增加保护
  // eslint-disable-next-line
  if (!maskProps) maskProps = {};
  var modalRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      modalDOM: modalRef.current,
      getModalDOM: function getModalDOM() {
        return modalRef.current;
      }
    };
  });
  useEffect(function () {
    // 更新模态框位置对齐目标元素
    updateModalPosition();
    // eslint-disable-next-line
  }, [visible]);

  // 受控显隐时, 需要更新容器位置
  function updateModalPosition() {
    var _maskProps, _maskProps2;
    var maskDOM = modalRef === null || modalRef === void 0 ? void 0 : modalRef.current;

    // 参考元素
    var referenceDOM = typeof externalReferenceDOM === 'function' ? externalReferenceDOM() : externalReferenceDOM;
    if (!referenceDOM || !maskDOM) return;
    if (visible && referenceDOM && maskDOM && !((_maskProps = maskProps) !== null && _maskProps !== void 0 && (_maskProps = _maskProps.style) !== null && _maskProps !== void 0 && _maskProps.top) && !((_maskProps2 = maskProps) !== null && _maskProps2 !== void 0 && (_maskProps2 = _maskProps2.style) !== null && _maskProps2 !== void 0 && _maskProps2.bottom)) {
      Tooltip.updatePositionByReferenceDOM(maskDOM, {
        referenceDOM: referenceDOM,
        parentDOM: portal,
        animation: animation,
        offset: offset
      });
    }
  }

  // 构建动画
  var animationClassName = getClassNameByAnimation(animation);

  // 点击遮罩
  function handleMaskClick(e) {
    var _maskProps3;
    if ((_maskProps3 = maskProps) !== null && _maskProps3 !== void 0 && _maskProps3.onClick) maskProps.onClick(e);
    if (maskClosable && onVisibleChange) onVisibleChange(false);
    e.stopPropagation();
  }

  // 点击模态框
  function handleModalClick(e) {
    e.stopPropagation();
  }

  // 获取激活状态样式
  function getActiveClass() {
    return visible ? ' active' : '';
  }
  var ModalNode = /*#__PURE__*/React.createElement("div", _extends({
    "data-animation": animation
  }, maskProps, {
    className: "mask modal-mask".concat((_maskProps4 = maskProps) !== null && _maskProps4 !== void 0 && _maskProps4.className ? ' ' + maskProps.className : '').concat(getActiveClass()),
    onClick: handleMaskClick,
    ref: modalRef
  }), /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: DOMUtil.classNames('modal-animation modal', SafeArea.getSafeAreaClassName(safeArea), animationClassName ? ' ' + animationClassName : '', props.className, getActiveClass()),
    "data-animation": animation,
    onClick: handleModalClick
  }), children));
  if (portal === null || portal === false) {
    return ModalNode;
  }
  return /*#__PURE__*/createPortal(ModalNode, portal || document.getElementById('root') || document.body);
});
export default Modal;