import React from 'react';
import AntdNotification from 'antd/es/notification';
import classnames from 'classnames';

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

const {
  CheckCircleIcon,
  CloseCircleIcon,
  InfoCircleIcon,
  WarningCircleIcon,
  CloseIcon,
} = Icons;
const prefixCls = `${antPrefix}-notification`;
const iconMap = {
  success: (
    <CheckCircleIcon
      fill="currentColor"
      fontSize="inherit"
      className={`${prefixCls}-notice-icon-success`}
    />
  ),
  error: (
    <CloseCircleIcon
      fill="currentColor"
      fontSize="inherit"
      className={`${prefixCls}-notice-icon-error`}
    />
  ),
  info: (
    <InfoCircleIcon
      fill="currentColor"
      fontSize="inherit"
      className={`${prefixCls}-notice-icon-info`}
    />
  ),
  warning: (
    <WarningCircleIcon
      fill="currentColor"
      fontSize="inherit"
      className={`${prefixCls}-notice-icon-warning`}
    />
  ),
};

const notification = {
  close: AntdNotification.close,
  destroy: AntdNotification.destroy,
  config: AntdNotification.config,
  useNotification: AntdNotification.useNotification,
};

notification.config({
  closeIcon: <CloseIcon />, // 关闭 icon
  duration: 4.5, // 4.5s 后关闭
});

notification.open = args => {
  return AntdNotification.open({
    ...args,
    prefixCls,
  });
};

['success', 'info', 'warning', 'error'].forEach(type => {
  notification[type] = args => {
    const { className, ...restArgs } = args;
    return notification.open({
      ...restArgs,
      prefixCls,
      type,
      icon: iconMap[type],
      className: classnames(`${prefixCls}-notice-${type}`, className),
    });
  };
});

notification.warn = notification.warning;

export default notification;
