import { describe, expect, it } from 'vitest'
import { Linter } from 'eslint'
// @ts-expect-error — plain ESM JS plugin without .d.ts
import tvuPlugin from '../eslint-plugin/index.js'

function lintJs(code: string, ruleOptions: Record<string, unknown> = {}) {
  const linter = new Linter()
  return linter.verify(
    code,
    {
      files: ['**/*.js'],
      languageOptions: { ecmaVersion: 'latest', sourceType: 'module' },
      plugins: { '@nancyzeng0210/tvu-design-system': tvuPlugin as any },
      rules: {
        '@nancyzeng0210/tvu-design-system/icon-from-dist': ['error', ruleOptions],
      },
    },
    { filename: 'test.js' }
  )
}

describe('eslint-plugin / icon-from-dist', () => {
  it('allows relative local SVG imports by default for existing-product increments', () => {
    const msgs = lintJs(`import AlertIcon from './icons/alert.svg'`)
    expect(msgs).toEqual([])
  })

  it('flags relative local SVG imports in strict mode', () => {
    const msgs = lintJs(`import AlertIcon from './icons/alert.svg'`, { forbidArbitrarySvg: true })
    expect(msgs.some((m) => m.messageId === 'arbitraryIconImport')).toBe(true)
  })

  it('flags alias local SVG imports in strict mode', () => {
    const msgs = lintJs(`import AlertIcon from '@/assets/icons/alert.svg?raw'`, { forbidArbitrarySvg: true })
    expect(msgs.some((m) => m.messageId === 'arbitraryIconImport')).toBe(true)
  })

  it('flags CDN SVG imports in strict mode', () => {
    const msgs = lintJs(`const loadIcon = () => import('https://cdn.example.com/icons/alert.svg')`, { forbidArbitrarySvg: true })
    expect(msgs.some((m) => m.messageId === 'arbitraryIconImport')).toBe(true)
  })

  it('flags deep imports into TVU icon internals', () => {
    const msgs = lintJs(`import AlertIcon from '@nancyzeng0210/tvu-design-system/dist/icons/alert.svg'`)
    expect(msgs.some((m) => m.messageId === 'deepImport')).toBe(true)
  })

  it('allows documented TVU icon exports', () => {
    const msgs = lintJs(`
      import { AlertIcon } from '@nancyzeng0210/tvu-design-system/icons/esm'
      import alertSvg from '@nancyzeng0210/tvu-design-system/icons/svg/alert.svg'
    `)
    expect(msgs).toEqual([])
  })

  it('leaves non-DS package icon modules out of scope', () => {
    const msgs = lintJs(`import { Search } from 'lucide-vue-next'`)
    expect(msgs).toEqual([])
  })
})
