import React from 'react';
import Modal from 'antd/es/modal';
import 'antd/es/modal/style/index';
import { ExclamationCircleFilled, CloseCircleFilled } from '@ant-design/icons';
import { XuiPromptModalProps, OpenPromptModalFunction, ModalParams } from './xui-prompt-modal.d';

const getIcon = (icon?: ModalParams['icon']) => {
  /** icon 为 warn 或未传入 */
  if (icon === 'warning' || !icon) {
    return <ExclamationCircleFilled />;
  }

  if (icon === 'error') {
    return <CloseCircleFilled style={{ color: 'red' }} />;
  }

  return icon;
};

const openPromptModal: OpenPromptModalFunction = params => {
  const icon = getIcon(params.icon);

  if (params.type === 'confirm') {
    const { title, content, onOk, onCancel, okText } = params;
    Modal.confirm({
      icon,
      title,
      content,
      okText: okText || '确定',
      cancelText: '取消',
      onOk,
      onCancel,
    });
  } else {
    const { title, content, okText, onOk } = params;
    Modal.warn({
      icon,
      title,
      content,
      okText: okText || '知道了',
      onOk,
    });
  }
};

const XuiPromptModal = {} as XuiPromptModalProps;

XuiPromptModal.openPromptModal = openPromptModal;

export default XuiPromptModal;
