// 基础版本（无UI框架依赖）
type MessageType = 'success' | 'error' | 'warning' | 'info'
type MessageOptions = {
    duration?: number
    showClose?: boolean
}

// 消息队列（防止重复提示）
let messageQueue = new Set<string>()

export const showMessage = (
    message: string,
    type: MessageType = 'info',
    options: MessageOptions = {}
): void => {
    // 防重复机制
    if (messageQueue.has(message)) return
    messageQueue.add(message)

    // 创建消息容器
    const messageEl = document.createElement('div')
    messageEl.className = `message ${type}`
    messageEl.innerHTML = `
    <span class="icon">${type === 'error' ? '❗' : '💡'}</span>
    <span>${message}</span>
    ${options.showClose ? '<button class="close">×</button>' : ''}
  `

    // 样式注入
    const style = document.createElement('style')
    style.textContent = `
    .message {
      position: fixed;
      top: 20px;
      right: 20px;
      padding: 12px 16px;
      border-radius: 4px;
      background: #fff;
      box-shadow: 0 2px 12px rgba(0,0,0,0.1);
      display: flex;
      align-items: center;
      gap: 8px;
      z-index: 9999;
    }
    .success { border-left: 4px solid #67c23a; }
    .error { border-left: 4px solid #f56c6c; }
    .warning { border-left: 4px solid #e6a23c; }
    .close {
      border: none;
      background: transparent;
      cursor: pointer;
      margin-left: 12px;
    }
  `
    document.head.appendChild(style)

    // 添加到DOM
    document.body.appendChild(messageEl)

    // 自动移除
    setTimeout(() => {
        messageEl.remove()
        style.remove()
        messageQueue.delete(message)
    }, options.duration || 3000)

    // 手动关闭
    messageEl.querySelector('.close')?.addEventListener('click', () => {
        messageEl.remove()
        style.remove()
        messageQueue.delete(message)
    })
}

// 快捷方法
export const message = {
    success: (msg: string, opts?: MessageOptions) => showMessage(msg, 'success', opts),
    error: (msg: string, opts?: MessageOptions) => showMessage(msg, 'error', opts),
    warning: (msg: string, opts?: MessageOptions) => showMessage(msg, 'warning', opts),
    info: (msg: string, opts?: MessageOptions) => showMessage(msg, 'info', opts)
}