import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React from 'react';
import { render as reactRender, unmount as reactUnmount } from 'rc-util/lib/React/render';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
import { getConfirmLocale } from 'antd/es/modal/locale';
import { globalConfig } from 'antd/es/config-provider';
import ConfirmDialog from './ConfirmDialog';
import { jsx as _jsx } from "react/jsx-runtime";
const destroyFns = [];
let defaultRootPrefixCls = '';
function getRootPrefixCls() {
  return defaultRootPrefixCls;
}
export default function confirm(config) {
  const container = document.createDocumentFragment();
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
  let currentConfig = {
    ...config,
    close,
    open: true
  };
  function destroy(...args) {
    const triggerCancel = args.some(param => param && param.triggerCancel);
    if (config.onCancel && triggerCancel) {
      config.onCancel(...args);
    }
    for (let i = 0; i < destroyFns.length; i++) {
      const fn = destroyFns[i];
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      if (fn === close) {
        destroyFns.splice(i, 1);
        break;
      }
    }
    reactUnmount(container);
  }
  function render({
    okText,
    cancelText,
    prefixCls: customizePrefixCls,
    ...props
  }) {
    /**
     * https://github.com/ant-design/ant-design/issues/23623
     *
     * Sync render blocks React event. Let's make this async.
     */
    setTimeout(() => {
      const runtimeLocale = getConfirmLocale();
      const {
        getPrefixCls,
        getIconPrefixCls
      } = globalConfig();
      // because Modal.config  set rootPrefixCls, which is different from other components
      const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
      const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
      const iconPrefixCls = getIconPrefixCls();
      reactRender( /*#__PURE__*/_jsx(ConfirmDialog, {
        ...props,
        prefixCls: prefixCls,
        rootPrefixCls: rootPrefixCls,
        iconPrefixCls: iconPrefixCls,
        okText: okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText),
        cancelText: cancelText || runtimeLocale.cancelText
      }), container);
    });
  }
  function close(...args) {
    currentConfig = {
      ...currentConfig,
      open: false,
      afterClose: () => {
        if (typeof config.afterClose === 'function') {
          config.afterClose();
        }
        // @ts-ignore
        destroy.apply(this, args);
      }
    };
    render(currentConfig);
  }
  function update(configUpdate) {
    if (typeof configUpdate === 'function') {
      currentConfig = configUpdate(currentConfig);
    } else {
      currentConfig = {
        ...currentConfig,
        ...configUpdate
      };
    }
    render(currentConfig);
  }
  render(currentConfig);
  destroyFns.push(close);
  return {
    destroy: close,
    update
  };
}
export function withWarn(props) {
  return {
    icon: /*#__PURE__*/_jsx(ExclamationCircleOutlined, {}),
    okCancel: false,
    ...props,
    type: 'warning'
  };
}
export function withInfo(props) {
  return {
    icon: /*#__PURE__*/_jsx(InfoCircleOutlined, {}),
    okCancel: false,
    ...props,
    type: 'info'
  };
}
export function withSuccess(props) {
  return {
    icon: /*#__PURE__*/_jsx(CheckCircleOutlined, {}),
    okCancel: false,
    ...props,
    type: 'success'
  };
}
export function withError(props) {
  return {
    icon: /*#__PURE__*/_jsx(CloseCircleOutlined, {}),
    okCancel: false,
    ...props,
    type: 'error'
  };
}
export function withConfirm(props) {
  return {
    icon: /*#__PURE__*/_jsx(ExclamationCircleOutlined, {}),
    okCancel: true,
    ...props,
    type: 'confirm'
  };
}
export function modalGlobalConfig({
  rootPrefixCls
}) {
  defaultRootPrefixCls = rootPrefixCls;
}