import React from 'react';
import AntdMessage from 'antd/es/message';

import { ConfigContext, antPrefix } from '../config-provider';
import Icons from '../icons';
import './index.less';

const {
  CheckCircleIcon,
  CloseCircleIcon,
  InfoCircleIcon,
  WarningCircleIcon,
  LoadingIcon,
} = Icons;
const iconMap = {
  success: <CheckCircleIcon fill="currentColor" />,
  error: <CloseCircleIcon fill="currentColor" />,
  info: <InfoCircleIcon fill="currentColor" />,
  warning: <WarningCircleIcon fill="currentColor" />,
  loading: <LoadingIcon fill="currentColor" />,
};

const message = AntdMessage;

message.config({
  prefixCls: `${antPrefix}-message`,
  duration: 2, // 默认2s消失
});

['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
  message[type] = (content, duration, onClose) => {
    if (
      content &&
      Object.prototype.toString.call(content) === '[object Object]' &&
      content.content
    ) {
      return message.open({ ...content, type, icon: iconMap[type] });
    }
    if (typeof duration === 'function') {
      onClose = duration;
      duration = undefined;
    }
    return message.open({
      content,
      duration,
      type,
      onClose,
      icon: iconMap[type],
    });
  };
});

message.warn = message.warning;

export default message;
