/* stylelint-disable custom-property-no-missing-var-function */
import React, { useEffect, useContext, useMemo } from 'react';
import Raven from '@wii/raven';
import classNames from 'classnames';
import Modal from '../modal';
import { ConfigContext } from '../wau-config-provider';
import './style';
import confirm, { withWarn, withInfo, withSuccess, withError, withConfirm, modalGlobalConfig } from './confirm';
import { jsx as _jsx } from "react/jsx-runtime";
const raven = new Raven();
const OriginModal = ({
  visible,
  open = visible,
  className,
  style,
  ...restProps
}) => {
  const {
    getPrefixCls
  } = useContext(ConfigContext);
  const prefixCls = getPrefixCls();

  // 来自于父应用
  const layout = useMemo(() => {
    if (raven.isChild) {
      return raven.layout;
    }
  }, []);
  useEffect(() => {
    WauModal.raven.send(WauModal.RAVEN_MESSAGE_NAME, {
      visible: open
    });
  }, [open]);
  useEffect(() => {
    return () => {
      WauModal.raven.send(WauModal.RAVEN_MESSAGE_NAME, {
        visible: false
      });
    };
  }, []);
  return /*#__PURE__*/_jsx(Modal, {
    open: open,
    centered: true,
    className: classNames([{
      [`${prefixCls}-modal`]: WauModal.raven.isChild
    }, className]),
    style: {
      top: layout ? -(layout.top || 0) / 2 : undefined,
      left: layout ? -(layout.left || 0) / 2 : undefined,
      ...style
    },
    ...restProps
  });
};
const WauModal = OriginModal;
WauModal.raven = raven;
WauModal.RAVEN_MESSAGE_NAME = 'modal';
WauModal.useModal = Modal.useModal;
const staticFunctions = ['info', 'success', 'error', 'warn', 'warning', 'confirm'];
const staticFunctionsMap = {
  info: withInfo,
  success: withSuccess,
  error: withError,
  warn: withWarn,
  warning: withWarn,
  confirm: withConfirm
};
staticFunctions.forEach(func => {
  WauModal[func] = props => {
    const withFunction = staticFunctionsMap[func];
    return confirm(withFunction(props));
  };
});
WauModal.config = modalGlobalConfig;
WauModal.destroyAll = Modal.destroyAll;
export default WauModal;