import { describe, it, expect, vi, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useAppStore } from '@/store/app'

// Mock i18n
vi.mock('@/locales', () => ({
  default: {
    global: {
      locale: { value: 'zh-cn' }
    }
  }
}))

describe('App Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    localStorage.clear()
  })

  describe('初始状态', () => {
    it('侧边栏默认应该是展开的', () => {
      const store = useAppStore()
      expect(store.sidebarOpened).toBe(true)
    })

    it('loading 默认应该是 false', () => {
      const store = useAppStore()
      expect(store.loading).toBe(false)
    })

    it('语言默认应该是 zh-cn', () => {
      const store = useAppStore()
      expect(store.language).toBe('zh-cn')
    })

    it('组件尺寸默认应该是 default', () => {
      const store = useAppStore()
      expect(store.size).toBe('default')
    })
  })

  describe('侧边栏操作', () => {
    it('toggleSidebar 应该切换侧边栏状态', () => {
      const store = useAppStore()

      expect(store.sidebarOpened).toBe(true)

      store.toggleSidebar()
      expect(store.sidebarOpened).toBe(false)

      store.toggleSidebar()
      expect(store.sidebarOpened).toBe(true)
    })

    it('closeSidebar 应该关闭侧边栏', () => {
      const store = useAppStore()

      store.closeSidebar()
      expect(store.sidebarOpened).toBe(false)
    })

    it('openSidebar 应该打开侧边栏', () => {
      const store = useAppStore()

      store.closeSidebar()
      expect(store.sidebarOpened).toBe(false)

      store.openSidebar()
      expect(store.sidebarOpened).toBe(true)
    })
  })

  describe('Loading 状态', () => {
    it('setLoading 应该设置 loading 状态', () => {
      const store = useAppStore()

      store.setLoading(true)
      expect(store.loading).toBe(true)

      store.setLoading(false)
      expect(store.loading).toBe(false)
    })
  })

  describe('语言设置', () => {
    it('changeLanguage 应该切换语言', () => {
      const store = useAppStore()

      store.changeLanguage('en')
      expect(store.language).toBe('en')

      store.changeLanguage('zh-cn')
      expect(store.language).toBe('zh-cn')
    })

    it('changeLanguage 应该保存到 localStorage', () => {
      const store = useAppStore()

      store.changeLanguage('en')

      const saved = localStorage.getItem('language')
      expect(saved).toBe('"en"')
    })

    it('locale 应该返回正确的 Element Plus 语言包', () => {
      const store = useAppStore()

      expect(store.locale).toBeDefined()

      store.changeLanguage('en')
      expect(store.locale).toBeDefined()
    })
  })

  describe('组件尺寸设置', () => {
    it('setSize 应该设置组件尺寸', () => {
      const store = useAppStore()

      store.setSize('small')
      expect(store.size).toBe('small')

      store.setSize('large')
      expect(store.size).toBe('large')

      store.setSize('default')
      expect(store.size).toBe('default')
    })
  })

  describe('从 localStorage 恢复', () => {
    it('应该从 localStorage 恢复语言设置', () => {
      localStorage.setItem('language', '"en"')

      setActivePinia(createPinia())
      const store = useAppStore()

      expect(store.language).toBe('en')
    })

    it('localStorage 值无效时使用默认语言', () => {
      localStorage.setItem('language', 'invalid')

      setActivePinia(createPinia())
      const store = useAppStore()

      expect(store.language).toBe('zh-cn')
    })
  })
})
