import { _ as _toConsumableArray } from './toConsumableArray-81040c9e.js';
import { _ as _slicedToArray } from './slicedToArray-76060636.js';
import { r as regenerator } from './index-beef914f.js';
import { a as __awaiter } from './tslib.es6-55ed4bd2.js';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import React__default from 'react';
import { Modal } from '../Modal/index.js';
import clsx from 'clsx';
import { s as styleInject } from './style-inject.es-300983ab.js';

var css_248z = ".function-modal-default [class*=makeStyles-md] {\n  width: 440px;\n  height: auto;\n  max-height: initial;\n}";
styleInject(css_248z);

var FDialog = function FDialog(props) {
  var open = props.open,
    title = props.title,
    size = props.size,
    context = props.context,
    content = props.content,
    onSubmit = props.onSubmit,
    onClose = props.onClose,
    getClose = props.getClose,
    listIndex = props.listIndex,
    ModalProps = props.ModalProps;
  /**
  * 外部想要单独关闭弹窗传出
  */
  React__default.useEffect(function () {
    getClose && getClose(function () {
      return onClose(listIndex);
    });
  }, [getClose, listIndex, onClose]);
  return jsx(Modal, Object.assign({}, ModalProps, {
    className: clsx('function-modal', !size && 'function-modal-default'),
    open: open,
    title: title,
    onCancel: function onCancel() {
      return onClose(listIndex);
    },
    onOk: function onOk() {
      return onSubmit(listIndex);
    },
    size: size || 'md',
    children: content || context,
    okButtonProps: {
      onClick: function onClick() {
        return onSubmit(listIndex);
      }
    },
    cancelButtonProps: {
      onClick: function onClick() {
        return onClose(listIndex);
      }
    }
  }));
};

/**
* 创建模态框的上下文
*/
var FunctionModalContext = /*#__PURE__*/React__default.createContext(Promise.reject);
/**
 * 弹出框组件
 * @param context 主体内容
 * @param title 弹出框标题
 * @param hideClose 隐藏右上角关闭图标 boolean 默认 false
 * @param checkFunction 检查方法，返回 boolean 值，true则关闭弹窗，false反之
 **/
var useModal = function useModal() {
  return React__default.useContext(FunctionModalContext);
};
var setState;
/**
* 函数弹窗组件Provider
*/
var FunctionModalProvider = function FunctionModalProvider(_ref) {
  var children = _ref.children;
  // 模态框组 配置
  var _React$useState = React__default.useState([]),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    confirmationState = _React$useState2[0],
    setConfirmationState = _React$useState2[1];
  // 传出设置模态框数组hook，用于关闭所有弹窗
  setState = function setState() {
    setConfirmationState([]);
    awaitingPromiseRef.current = [];
  };
  // 存储每个模态框的 promise 方法
  var awaitingPromiseRef = React__default.useRef([]);
  /**
  * 模态框初始化方法
  * @param options 模态框配置
  * @return 返回一个promise 用于外部判断弹窗状态
  */
  var openConfirmation = function openConfirmation(options) {
    setConfirmationState(function (pre) {
      pre.push(options);
      return _toConsumableArray(pre);
    });
    return new Promise(function (resolve, reject) {
      awaitingPromiseRef.current.push({
        resolve: resolve,
        reject: reject
      });
    });
  };
  /**
  * 模态框关闭方法
  * @param idx 模态框在模态框数组里的下标，用于拿到对应的配置 和 promise
  * @description 关闭后会清空相关配置
  */
  var handleClose = function handleClose(idx) {
    // 是否有禁用取消停止功能的配置
    if (confirmationState[idx] && !confirmationState[idx].disCatchOnCancel && awaitingPromiseRef.current[idx]) {
      awaitingPromiseRef.current[idx].resolve(false);
    } else if (awaitingPromiseRef.current[idx]) {
      awaitingPromiseRef.current[idx].resolve(true);
    }
    hideDialog(idx);
    deleteOption(idx);
  };
  /**
  * 模态框确定触发的方法
  * @param idx 模态框在模态框数组里的下标，用于拿到对应的配置 和 promise
  */
  var handleSubmit = function handleSubmit(idx) {
    return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regenerator.mark(function _callee() {
      var checkFuc, jude, pRef;
      return regenerator.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            checkFuc = confirmationState && confirmationState[idx] && confirmationState[idx].checkFunction;
            jude = true; // 判断是否有检查方法，如果有则先执行检查方法
            if (!checkFuc) {
              _context.next = 6;
              break;
            }
            _context.next = 5;
            return checkFuc();
          case 5:
            jude = _context.sent;
          case 6:
            if (jude) {
              _context.next = 8;
              break;
            }
            return _context.abrupt("return");
          case 8:
            hideDialog(idx);
            pRef = awaitingPromiseRef.current[idx];
            deleteOption(idx, pRef);
          case 11:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
  };
  /**
  * 删除模态框配置，promise 执行成功操作
  * @param pRef 模态框的promise
  * @param idx 模态框在模态框组里的下标
  */
  var deleteOption = function deleteOption(idx, pRef) {
    setTimeout(function () {
      pRef && pRef.resolve(true);
      setConfirmationState(function (pre) {
        pre.splice(idx, 1);
        awaitingPromiseRef.current.splice(idx, 1);
        return _toConsumableArray(pre);
      });
    }, 200);
  };
  /**
  * 隐藏模态框对应的dom
  * @param idx 模态框在模态框数组里的下标
  * @desc 解决异步导致的闪烁问题
  */
  var hideDialog = function hideDialog(idx) {
    var dialogEl = document.querySelectorAll('.function-modal .MuiDialog-container')[idx];
    if (dialogEl) dialogEl.style.opacity = '0';
  };
  return jsxs(Fragment, {
    children: [jsx(FunctionModalContext.Provider, {
      value: openConfirmation,
      children: children
    }), confirmationState && confirmationState.map(function (option, idx) {
      return jsx(FDialog, Object.assign({
        listIndex: idx,
        open: Boolean(option),
        onSubmit: handleSubmit,
        onClose: handleClose
      }, option), "modal-item-".concat(idx));
    })]
  });
};
var useClose = function useClose() {
  return setState;
};

export { FunctionModalProvider as F, useClose as a, useModal as u };
