import { forwardRef, useState, useImperativeHandle } from "react";
import Modal from "../modal.js";
import { jsx } from "../../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
function HookModal(props, ref) {
  var _a = __read(useState(true), 2), visible = _a[0], setVisible = _a[1];
  var _b = __read(useState(props), 2), config = _b[0], setConfig = _b[1];
  useImperativeHandle(ref, function() {
    return {
      update: setConfig,
      close: function() {
        setVisible(false);
      }
    };
  });
  function onOk() {
    var ret = config.onOk && config.onOk();
    if (ret && ret.then) {
      setConfig(__assign(__assign({}, config), {
        confirmLoading: true
      }));
      ret.then(function() {
        setVisible(false);
      }, function(e) {
        console.error(e);
        setConfig(__assign(__assign({}, config), {
          confirmLoading: false
        }));
      });
    }
    if (!ret) {
      setVisible(false);
    }
  }
  function onCancel() {
    config.onCancel && config.onCancel();
    setVisible(false);
  }
  return /* @__PURE__ */ jsx(Modal, {
    ...__assign({
      unmountOnExit: true,
      simple: true
    }, config, {
      visible,
      onOk,
      onCancel
    }),
    children: config.content
  });
}
const HookModal$1 = forwardRef(HookModal);
export {
  HookModal$1 as default
};
//# sourceMappingURL=hookModal.js.map
