import { Toast } from 'vant';

/**
 *
 * @type {{loading: (function({message?: *, className?: *, loadingType?: *, overlay?: *, forbidClick?: *, duration?: *})), success: (function({message?: *, className?: *, loadingType?: *, overlay?: *, forbidClick?: *, duration?: *})), fail: (function({message?: *, className?: *, loadingType?: *, overlay?: *, forbidClick?: *, duration?: *})), clear: (function())}}
 */
let ToastService = {
  loading({
    message,
    className,
    loadingType,
    overlay,
    forbidClick,
    duration,
    getContainer,
    closeOnClick,
    onClose
  } = {}) {
    const sec = 2000;
    if (onClose) {
      const timer = setTimeout(() => {
        onClose();
        clearTimeout(timer);
      }, duration || sec);
    }
    return Toast.loading({
      message: message || '',
      className: className || 'device_toast',
      loadingType: loadingType || 'circular',
      overlay: overlay || false,
      forbidClick: forbidClick || true,
      duration: duration || 0,
      getContainer: getContainer || 'body',
      closeOnClick: closeOnClick || true
    });
  },
  success({ message, className, loadingType, overlay, forbidClick, duration, onClose }) {
    const sec = 2000;
    if (onClose) {
      const timer = setTimeout(() => {
        onClose();
        clearTimeout(timer);
      }, duration || sec);
    }
    return Toast.success({
      message: message || '',
      className: className || 'device_toast',
      loadingType: loadingType || 'circular',
      overlay: overlay || false,
      forbidClick: forbidClick || true,
      duration: duration || sec
    });
  },
  fail({ message, className, loadingType, overlay, forbidClick, duration, onClose }) {
    const sec = 2000;
    // 官方的onoClose方法有bug,已暂停使用
    if (onClose) {
      const timer = setTimeout(() => {
        onClose();
        clearTimeout(timer);
      }, duration || sec);
    }
    return Toast.fail({
      message: message || '操作失败',
      className: className || 'device_toast',
      loadingType: loadingType || 'circular',
      overlay: overlay || false,
      forbidClick: forbidClick || true,
      duration: duration || 2000
    });
  },
  clear() {
    return Toast.clear();
  }
};

export default ToastService;
