import message, { MessageApi } from 'antd/lib/message';
import { getLagerThanMaxZindex } from '../../../utils/helper';

const _success = (content: string, container?: any, duration?: number) => {
  message.success({
    content,
    className: 'lion-msg-success',
    style: {
      zIndex: getLagerThanMaxZindex()
    },
    getContainer: container ? container : document.body,
    duration: duration ? duration : 3
  });
};

const _error = (content: string, container?: any, duration?: number) => {
  message.error({
    content,
    className: 'lion-msg-error',
    style: {
      zIndex: getLagerThanMaxZindex()
    },
    getContainer: container ? container : document.body,
    duration: duration ? duration : 3
  });
};

const _info = (content: string, container?: any, duration?: number) => {
  message.info({
    content,
    className: 'lion-msg-info',
    style: {
      zIndex: getLagerThanMaxZindex()
    },
    getContainer: container ? container : document.body,
    duration: duration ? duration : 2
  });
};

const _warning = (content: string, container?: any, duration?: number) => {
  message.warning({
    content,
    className: 'lion-msg-warning',
    style: {
      zIndex: getLagerThanMaxZindex()
    },
    getContainer: container ? container : document.body,
    duration: duration ? duration : 3
  });
};


const MessageProxy = {
  success: _success,
  error: _error,
  info: _info,
  warning: _warning,
}

type PrivateMessageFuncName = keyof typeof MessageProxy

const getMessage = () => {
  // 拦截这四种方法
  const interceptedMethods = Object.keys(MessageProxy)

  return new Proxy(message, {
    get(target, propKey: keyof MessageApi, receiver) {
      const originalMember = target[propKey];

      // 非函数或非拦截方法：直接返回
      if (typeof originalMember !== 'function' ||
        !interceptedMethods.includes(propKey)) {
        return originalMember;
      }

      // 包装需拦截的方法
      return function (...args: any[]) {
        // 检查参数中是否有对象
        const hasObjectArg = args.some(arg =>
          typeof arg === 'object' && arg !== null
        );

        // 条件1：参数含对象 -> 使用原方法
        // 条件2：无对象参数 -> 执行自定义逻辑（此处示例为打印日志）
        if (hasObjectArg) {
          return originalMember.apply(target, args);
        } else if (interceptedMethods.includes(propKey)) {
          return MessageProxy[propKey as PrivateMessageFuncName].apply(MessageProxy, args)
          // 可在此处添加自定义逻辑（如日志、转换参数等）
        }
      };
    }
  });
};



export default {
  _success,
  _error,
  _info,
  _warning,
  getMessage
}
