/* eslint-disable */
import { Modal, message as antdMessage } from 'antd-v4';
import { Modal as mobileModal } from 'antd-mobile-v2';
import * as commonFunc from '@/components/Common/commonFunc';/* 通用单据方法 */
import commonConfig from '../config';

const { warning } = Modal;
const antdMobileAlert = mobileModal.alert;

let debounceCount;
let secondContent = [];
let width;

const queryWidth = () => {
  width = document?.documentElement?.getBoundingClientRect()?.width <= 750 ? '80%' : 500;
}

const openConfirm = (config) => {

  queryWidth();
  const { message, ...rest } = config;
  secondContent.push(<p style={{ whiteSpace: 'pre-wrap', wordWrap: 'break-word' }}>{message}</p>)

  if (debounceCount) {
     clearTimeout(debounceCount);
  };

  const fn = () => {
    const userinfo = JSON.parse(localStorage.getItem(`${commonConfig.prefix}userinfo`)) || {};
    const language = userinfo.sLanguage || 'sChinese'; // 默认简体中文
    const isEnglish = language === 'sEnglish';

    // 使用语言设置
    const sTitle = isEnglish ? 'Reminder' :  language === 'sBig5' ? '溫馨提示' :  '温馨提示';
    const okText =  language === 'sEnglish' ? 'Confirm' :  language === 'sBig5' ? '確認': '确认';
    if (location.pathname.toLocaleLowerCase().includes('mobile')) {
      antdMobileAlert(sTitle, secondContent || '出错了', [
        { text: '确认', },
      ])
    } else {
      warning({
        title: sTitle,
        content: <div style={{ maxHeight: '50vh', overflowY: 'auto' }} >{secondContent || '出错了'}</div>,
        okText: okText,
        zIndex: 1000,
        width,
        ...rest,
      });
    }
  }

  debounceCount = setTimeout(() => {
    fn();
    secondContent = [];
  });
};

/**
 * 替换message.error信息提示方式为warning
 */
export const message = {
  ...antdMessage,
  error: (content, ...rest) => {
    const userinfo = JSON.parse(localStorage.getItem(`${commonConfig.prefix}userinfo`)) || {};
    const commonConst = JSON.parse(localStorage.getItem(`${commonConfig.prefix}commonConst`)) || {};
    const requiredText = commonFunc.showMessage(commonConst, 'required');
    const language = userinfo.sLanguage || 'sChinese'; // 默认简体中文
    // const requiredText = language === 'sEnglish' ? ' is required' :
    //   (language === 'sBig5' ? '為必填項' : '为必填项');
    const sContent = content?.replace('为必填项', requiredText);
    openConfirm({ message: sContent, ...rest });
  },
};
