import { describe, it, expect, beforeEach } from 'vitest'
import { useEditorStore } from '../editorStore'

describe('editorStore', () => {
  beforeEach(() => {
    useEditorStore.setState({
      viewMode: 'editor',
      darkMode: false,
      externallyModified: null,
      loadingStates: {},
    })
  })

  describe('setViewMode', () => {
    it('should set view mode to preview', () => {
      useEditorStore.getState().setViewMode('preview')
      expect(useEditorStore.getState().viewMode).toBe('preview')
    })

    it('should set view mode to editor', () => {
      useEditorStore.setState({ viewMode: 'preview' })
      useEditorStore.getState().setViewMode('editor')
      expect(useEditorStore.getState().viewMode).toBe('editor')
    })
  })

  describe('setDarkMode', () => {
    it('should enable dark mode', () => {
      useEditorStore.getState().setDarkMode(true)
      expect(useEditorStore.getState().darkMode).toBe(true)
    })

    it('should disable dark mode', () => {
      useEditorStore.setState({ darkMode: true })
      useEditorStore.getState().setDarkMode(false)
      expect(useEditorStore.getState().darkMode).toBe(false)
    })
  })

  describe('toggleDarkMode', () => {
    it('should toggle from false to true', () => {
      useEditorStore.getState().toggleDarkMode()
      expect(useEditorStore.getState().darkMode).toBe(true)
    })

    it('should toggle from true to false', () => {
      useEditorStore.setState({ darkMode: true })
      useEditorStore.getState().toggleDarkMode()
      expect(useEditorStore.getState().darkMode).toBe(false)
    })
  })

  describe('setExternallyModified', () => {
    it('should set externally modified info', () => {
      useEditorStore.getState().setExternallyModified({ filePath: '/test.md' })
      expect(useEditorStore.getState().externallyModified).toEqual({ filePath: '/test.md' })
    })

    it('should clear externally modified info', () => {
      useEditorStore.setState({ externallyModified: { filePath: '/test.md' } })
      useEditorStore.getState().setExternallyModified(null)
      expect(useEditorStore.getState().externallyModified).toBeNull()
    })
  })

  describe('loading states', () => {
    it('should set loading state', () => {
      useEditorStore.getState().setLoading('file-open', true)
      expect(useEditorStore.getState().isLoading('file-open')).toBe(true)
    })

    it('should return false for unset loading key', () => {
      expect(useEditorStore.getState().isLoading('non-existent')).toBe(false)
    })

    it('should update loading state to false', () => {
      useEditorStore.getState().setLoading('file-open', true)
      useEditorStore.getState().setLoading('file-open', false)
      expect(useEditorStore.getState().isLoading('file-open')).toBe(false)
    })

    it('should handle multiple independent loading states', () => {
      const { setLoading } = useEditorStore.getState()
      setLoading('file-open', true)
      setLoading('markdown-render', true)

      const state = useEditorStore.getState()
      expect(state.isLoading('file-open')).toBe(true)
      expect(state.isLoading('markdown-render')).toBe(true)

      setLoading('file-open', false)
      expect(useEditorStore.getState().isLoading('file-open')).toBe(false)
      expect(useEditorStore.getState().isLoading('markdown-render')).toBe(true)
    })
  })
})
