"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
require("react");
const reactDom = require("../_util/react-dom.js");
const modal = require("./modal.js");
const index$3 = require("../../icon/react-icon/IconInfoCircleFill/index.js");
const index$2 = require("../../icon/react-icon/IconCheckCircleFill/index.js");
const index$1 = require("../../icon/react-icon/IconExclamationCircleFill/index.js");
const index = require("../../icon/react-icon/IconCloseCircleFill/index.js");
const config = require("./config.js");
const index$4 = require("../ConfigProvider/index.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@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);
};
function ConfirmModal(props) {
  var _a = config.getModalConfig(), prefixCls = _a.prefixCls, simple = _a.simple;
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(modal, {
    ...__assign({
      prefixCls,
      simple
    }, props),
    children: props.content
  });
}
var normalizeConfig = function(_config) {
  if (_config.isNotice) {
    var icon = _config.icon;
    if (!icon && icon !== null) {
      switch (_config.noticeType) {
        case "info":
          icon = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3, {});
          break;
        case "success":
          icon = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {});
          break;
        case "warning":
          icon = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {});
          break;
        case "error":
          icon = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index, {});
          break;
      }
    }
    _config.title = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("span", {
      children: [icon, _config.title]
    });
    _config.hideCancel = true;
  } else {
    _config.title = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("span", {
      children: [_config.icon !== null && (_config.icon || /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {})), _config.title]
    });
  }
  return _config;
};
function confirm(config$1, renderFunc) {
  var root;
  var div = document.createElement("div");
  document.body.appendChild(div);
  var configProviderProps = config.getConfigProviderProps();
  function render(props) {
    var dom = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$4.default, {
      ...__assign({}, configProviderProps),
      children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(ConfirmModal, {
        ...__assign({}, props, {
          onCancel
        })
      })
    });
    if (root) {
      root.render(dom);
    } else {
      root = reactDom.render(dom, div);
    }
  }
  var renderFunction = renderFunc || render;
  var modalConfig = __assign(__assign({}, config$1), {
    visible: false
  });
  var onOk = function() {
    var ret;
    var _onOk = config$1.onOk || config$1.onConfirm;
    if (_onOk) {
      ret = _onOk();
    }
    if (ret && ret.then) {
      modalConfig.confirmLoading = true;
      renderFunction(modalConfig);
      ret.then(function() {
        onCancel(true);
      }, function(e) {
        console.error(e);
        modalConfig.confirmLoading = false;
        renderFunction(modalConfig);
      });
    }
    if (!ret) {
      onCancel(true);
    }
  };
  modalConfig.onOk = onOk;
  modalConfig = normalizeConfig(modalConfig);
  modalConfig.visible = true;
  renderFunction(modalConfig);
  function destroy() {
    root = root === null || root === void 0 ? void 0 : root.unmount();
    if (div.parentNode) {
      div.parentNode.removeChild(div);
    }
    for (var i = 0; i < config.destroyList.length; i++) {
      var fn = config.destroyList[i];
      if (fn === close) {
        config.destroyList.splice(i, 1);
        break;
      }
    }
  }
  function onCancel(isOnOk) {
    !isOnOk && config$1.onCancel && config$1.onCancel();
    modalConfig.visible = false;
    modalConfig.afterClose = function() {
      config$1.afterClose && config$1.afterClose();
      destroy();
    };
    renderFunction(modalConfig);
  }
  function update(newConfig) {
    modalConfig = __assign(__assign(__assign({}, modalConfig), {
      title: config$1.title
    }), newConfig);
    modalConfig = normalizeConfig(modalConfig);
    renderFunction(modalConfig);
  }
  function close() {
    modalConfig.visible = false;
    modalConfig.afterClose = function() {
      config$1.afterClose && config$1.afterClose();
      destroy();
    };
    renderFunction(modalConfig);
  }
  config.destroyList.push(close);
  return {
    close,
    update
  };
}
exports.default = confirm;
exports.normalizeConfig = normalizeConfig;
//# sourceMappingURL=confirm.js.map
