import "react";
import { render } from "../_util/react-dom.js";
import Modal from "./modal.js";
import IconInfoCircleFill from "../../icon/react-icon/IconInfoCircleFill/index.js";
import IconCheckCircleFill from "../../icon/react-icon/IconCheckCircleFill/index.js";
import IconExclamationCircleFill from "../../icon/react-icon/IconExclamationCircleFill/index.js";
import IconCloseCircleFill from "../../icon/react-icon/IconCloseCircleFill/index.js";
import { destroyList, getConfigProviderProps, getModalConfig } from "./config.js";
import ConfigProvider from "../ConfigProvider/index.js";
import { jsx, jsxs } 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);
};
function ConfirmModal(props) {
  var _a = getModalConfig(), prefixCls = _a.prefixCls, simple = _a.simple;
  return /* @__PURE__ */ 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__ */ jsx(IconInfoCircleFill, {});
          break;
        case "success":
          icon = /* @__PURE__ */ jsx(IconCheckCircleFill, {});
          break;
        case "warning":
          icon = /* @__PURE__ */ jsx(IconExclamationCircleFill, {});
          break;
        case "error":
          icon = /* @__PURE__ */ jsx(IconCloseCircleFill, {});
          break;
      }
    }
    _config.title = /* @__PURE__ */ jsxs("span", {
      children: [icon, _config.title]
    });
    _config.hideCancel = true;
  } else {
    _config.title = /* @__PURE__ */ jsxs("span", {
      children: [_config.icon !== null && (_config.icon || /* @__PURE__ */ jsx(IconExclamationCircleFill, {})), _config.title]
    });
  }
  return _config;
};
function confirm(config, renderFunc) {
  var root;
  var div = document.createElement("div");
  document.body.appendChild(div);
  var configProviderProps = getConfigProviderProps();
  function render$1(props) {
    var dom = /* @__PURE__ */ jsx(ConfigProvider, {
      ...__assign({}, configProviderProps),
      children: /* @__PURE__ */ jsx(ConfirmModal, {
        ...__assign({}, props, {
          onCancel
        })
      })
    });
    if (root) {
      root.render(dom);
    } else {
      root = render(dom, div);
    }
  }
  var renderFunction = renderFunc || render$1;
  var modalConfig = __assign(__assign({}, config), {
    visible: false
  });
  var onOk = function() {
    var ret;
    var _onOk = config.onOk || config.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 < destroyList.length; i++) {
      var fn = destroyList[i];
      if (fn === close) {
        destroyList.splice(i, 1);
        break;
      }
    }
  }
  function onCancel(isOnOk) {
    !isOnOk && config.onCancel && config.onCancel();
    modalConfig.visible = false;
    modalConfig.afterClose = function() {
      config.afterClose && config.afterClose();
      destroy();
    };
    renderFunction(modalConfig);
  }
  function update(newConfig) {
    modalConfig = __assign(__assign(__assign({}, modalConfig), {
      title: config.title
    }), newConfig);
    modalConfig = normalizeConfig(modalConfig);
    renderFunction(modalConfig);
  }
  function close() {
    modalConfig.visible = false;
    modalConfig.afterClose = function() {
      config.afterClose && config.afterClose();
      destroy();
    };
    renderFunction(modalConfig);
  }
  destroyList.push(close);
  return {
    close,
    update
  };
}
export {
  confirm as default,
  normalizeConfig
};
//# sourceMappingURL=confirm.js.map
