/**
 * 测试环境设置
 */

import { vi, afterEach } from 'vitest'
import { config } from '@vue/test-utils'

// 全局测试配置
config.global.stubs = {
  // Element Plus 组件存根
  'el-button': true,
  'el-input': true,
  'el-form': true,
  'el-form-item': true,
  'el-message': true,
  'el-message-box': true,
  'el-notification': true,
  'el-loading': true,
  'el-table': true,
  'el-table-column': true,
  'el-pagination': true,
  'el-dialog': true,
  'el-drawer': true,
  'el-card': true,
  'el-tag': true,
  'el-badge': true,
  'el-avatar': true,
  'el-tooltip': true,
  'el-popover': true,
  'el-dropdown': true,
  'el-dropdown-menu': true,
  'el-dropdown-item': true,
  'el-menu': true,
  'el-menu-item': true,
  'el-submenu': true,
  'el-breadcrumb': true,
  'el-breadcrumb-item': true,
  'el-tabs': true,
  'el-tab-pane': true,
  'el-collapse': true,
  'el-collapse-item': true,
  'el-tree': true,
  'el-select': true,
  'el-option': true,
  'el-checkbox': true,
  'el-checkbox-group': true,
  'el-radio': true,
  'el-radio-group': true,
  'el-switch': true,
  'el-slider': true,
  'el-rate': true,
  'el-color-picker': true,
  'el-transfer': true,
  'el-upload': true,
  'el-progress': true,
  'el-skeleton': true,
  'el-empty': true,
  'el-result': true,
  'el-alert': true,
  'el-descriptions': true,
  'el-descriptions-item': true,
  'el-timeline': true,
  'el-timeline-item': true,
  'el-divider': true,
  'el-backtop': true,
  'el-calendar': true,
  'el-image': true,
  'el-carousel': true,
  'el-carousel-item': true,
  'el-steps': true,
  'el-step': true,
  'el-row': true,
  'el-col': true,
  'el-container': true,
  'el-header': true,
  'el-aside': true,
  'el-main': true,
  'el-footer': true,
  'el-space': true,
  'el-affix': true,
  'el-anchor': true,
  'el-anchor-link': true,
  
  // Element Plus Icons
  'el-icon': true,
  
  // Router
  'router-link': true,
  'router-view': true
}

// 全局 mocks
config.global.mocks = {
  $t: (msg: string) => msg, // 国际化mock
  $route: {
    path: '/',
    query: {},
    params: {},
    meta: {}
  },
  $router: {
    push: vi.fn(),
    replace: vi.fn(),
    go: vi.fn(),
    back: vi.fn(),
    forward: vi.fn()
  }
}

// Mock localStorage
Object.defineProperty(window, 'localStorage', {
  value: {
    store: {} as Record<string, string>,
    getItem(key: string) {
      return this.store[key] || null
    },
    setItem(key: string, value: string) {
      this.store[key] = String(value)
    },
    removeItem(key: string) {
      delete this.store[key]
    },
    clear() {
      this.store = {}
    }
  },
  writable: true
})

// Mock sessionStorage
Object.defineProperty(window, 'sessionStorage', {
  value: {
    store: {} as Record<string, string>,
    getItem(key: string) {
      return this.store[key] || null
    },
    setItem(key: string, value: string) {
      this.store[key] = String(value)
    },
    removeItem(key: string) {
      delete this.store[key]
    },
    clear() {
      this.store = {}
    }
  },
  writable: true
})

// Mock window.location
Object.defineProperty(window, 'location', {
  value: {
    href: 'http://localhost:3000/',
    origin: 'http://localhost:3000',
    protocol: 'http:',
    host: 'localhost:3000',
    hostname: 'localhost',
    port: '3000',
    pathname: '/',
    search: '',
    hash: '',
    reload: vi.fn(),
    assign: vi.fn(),
    replace: vi.fn()
  },
  writable: true
})

// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: vi.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(),
    removeListener: vi.fn(),
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn()
  }))
})

// Mock ResizeObserver
global.ResizeObserver = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn()
}))

// Mock IntersectionObserver
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
  observe: vi.fn(),
  unobserve: vi.fn(),
  disconnect: vi.fn()
}))

// Mock fetch
global.fetch = vi.fn()

// Mock console methods for cleaner test output
global.console = {
  ...console,
  // 在测试中静默某些日志
  log: vi.fn(),
  warn: vi.fn(),
  error: vi.fn()
}

// 清理函数
afterEach(() => {
  vi.clearAllMocks()
  localStorage.clear()
  sessionStorage.clear()
})
