"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _view = _interopRequireDefault(require("../view"));
var _mask = _interopRequireDefault(require("./mask"));
var _utils = require("../utils");
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
var Modal = function (props, ref) {
  var _classNames, _classNames2;
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    _props$view = props.view,
    view = _props$view === void 0 ? false : _props$view,
    _props$visible = props.visible,
    visible = _props$visible === void 0 ? false : _props$visible,
    container = props.container,
    _props$disablePortal = props.disablePortal,
    disablePortal = _props$disablePortal === void 0 ? false : _props$disablePortal,
    _props$placement = props.placement,
    placement = _props$placement === void 0 ? 'center' : _props$placement,
    _props$className = props.className,
    className = _props$className === void 0 ? '' : _props$className,
    _props$maskClassName = props.maskClassName,
    maskClassName = _props$maskClassName === void 0 ? '' : _props$maskClassName,
    _props$containerClass = props.containerClassName,
    containerClassName = _props$containerClass === void 0 ? '' : _props$containerClass,
    _props$containerStyle = props.containerStyle,
    containerStyle = _props$containerStyle === void 0 ? {} : _props$containerStyle,
    _props$showMask = props.showMask,
    showMask = _props$showMask === void 0 ? true : _props$showMask,
    _props$maskClosable = props.maskClosable,
    maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable,
    _props$usePortal = props.usePortal,
    usePortal = _props$usePortal === void 0 ? true : _props$usePortal,
    _props$disableScroll = props.disableScroll,
    disableScroll = _props$disableScroll === void 0 ? false : _props$disableScroll,
    _props$onMaskClick = props.onMaskClick,
    onMaskClick = _props$onMaskClick === void 0 ? function () {} : _props$onMaskClick,
    _props$onClose = props.onClose,
    onClose = _props$onClose === void 0 ? function () {} : _props$onClose,
    _props$onVisibleChang = props.onVisibleChange,
    onVisibleChange = _props$onVisibleChang === void 0 ? function () {} : _props$onVisibleChang,
    children = props.children,
    zIndex = props.zIndex,
    _props$style = props.style,
    styleProp = _props$style === void 0 ? {} : _props$style,
    others = (0, _tslib.__rest)(props, ["prefix", "view", "visible", "container", "disablePortal", "placement", "className", "maskClassName", "containerClassName", "containerStyle", "showMask", "maskClosable", "usePortal", "disableScroll", "onMaskClick", "onClose", "onVisibleChange", "children", "zIndex", "style"]);
  var _useState = (0, _react.useState)(props.visible),
    show = _useState[0],
    setShow = _useState[1];
  (0, _react.useEffect)(function () {
    if (show && !visible) {
      var t1 = setTimeout(function () {
        return setShow(false);
      }, 300);
      return function () {
        return clearTimeout(t1);
      };
    } else {
      setShow(visible);
      onVisibleChange(visible);
    }
  }, [visible]);
  var clsPrefix = prefix + "modal";
  var modalContainer = /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    ref: ref,
    className: (0, _classnames.default)(containerClassName, clsPrefix + "-container", clsPrefix + "-container--" + placement, (_classNames = {}, _classNames[clsPrefix + "-container--" + placement + "-active"] = visible, _classNames[clsPrefix + "-container--" + placement + "-inactive"] = !visible, _classNames)),
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(Object.assign(Object.assign({}, styleProp), containerStyle))
  }), children);
  var element = show ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    "disable-scroll": disableScroll,
    catchTouchMove: disableScroll,
    className: (0, _classnames.default)(className, "" + clsPrefix, clsPrefix + "--" + placement, (_classNames2 = {}, _classNames2[clsPrefix + "--no-portal"] = !usePortal, _classNames2)),
    ref: ref,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)((0, _utils.isNumber)(zIndex) ? {
      zIndex: zIndex
    } : null)
  }, showMask ? /*#__PURE__*/(0, _react.createElement)(_mask.default, {
    ref: ref,
    className: maskClassName,
    visible: visible,
    onClick: function handleMaskClick(e) {
      if (onMaskClick) {
        onMaskClick(e);
      }
      if (maskClosable && onClose) {
        onClose('mask', e);
        onVisibleChange(false, 'mask');
      }
    }
  }) : null, modalContainer) : null;
  if (disablePortal || view) {
    return element;
  }
  if (!usePortal) {
    return /*#__PURE__*/(0, _react.createElement)(_view.default, {
      ref: ref,
      className: clsPrefix + "-theme-wrapper"
    }, element);
  }
  return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Portal, {
    container: container
  }, element);
};
Modal.displayName = 'Modal';
var _default = /*#__PURE__*/(0, _react.forwardRef)(Modal);
exports.default = _default;