import { describe, expect, test } from 'vitest'
import { globDirname } from '../src/glob-dirname'

const dir = (str: string) => globDirname([str])[0]

describe('globDirname', () => {
  test('should handle various glob patterns correctly', () => {
    expect(dir('.')).toMatchInlineSnapshot(`"."`)
    expect(dir('.*')).toMatchInlineSnapshot(`"."`)
    expect(dir('/.*')).toMatchInlineSnapshot(`"."`)
    expect(dir('/.*/')).toMatchInlineSnapshot(`"."`)
    expect(dir('a/.*/b')).toMatchInlineSnapshot(`"a"`)
    expect(dir('a*/.*/b')).toMatchInlineSnapshot(`"."`)
    expect(dir('*/a/b/c')).toMatchInlineSnapshot(`"."`)
    expect(dir('*')).toMatchInlineSnapshot(`"."`)
    expect(dir('*/')).toMatchInlineSnapshot(`"."`)
    expect(dir('*/*')).toMatchInlineSnapshot(`"."`)
    expect(dir('*/*/')).toMatchInlineSnapshot(`"."`)
    expect(dir('**')).toMatchInlineSnapshot(`"."`)
    expect(dir('**/')).toMatchInlineSnapshot(`"."`)
    expect(dir('**/*')).toMatchInlineSnapshot(`"."`)
    expect(dir('**/*/')).toMatchInlineSnapshot(`"."`)
    expect(dir('/*.js')).toMatchInlineSnapshot(`"."`)
    expect(dir('*.js')).toMatchInlineSnapshot(`"."`)
    expect(dir('**/*.js')).toMatchInlineSnapshot(`"."`)
    expect(dir('{a,b}')).toMatchInlineSnapshot(`"."`)
    expect(dir('/{a,b}')).toMatchInlineSnapshot(`"."`)
    expect(dir('/{a,b}/')).toMatchInlineSnapshot(`"."`)
    expect(dir('(a|b)')).toMatchInlineSnapshot(`"."`)
    expect(dir('/(a|b)')).toMatchInlineSnapshot(`"."`)
    expect(dir('./(a|b)')).toMatchInlineSnapshot(`"."`)
    expect(dir('a/(b c)')).toMatchInlineSnapshot(`"a"`)
    expect(dir('a/(b c)/')).toMatchInlineSnapshot(`"a"`)
    expect(dir('a/(b c)/d')).toMatchInlineSnapshot(`"a"`)
    expect(dir('path/to/*.js')).toMatchInlineSnapshot(`"path/to"`)
    expect(dir('/root/path/to/*.js')).toMatchInlineSnapshot(`"/root/path/to"`)
    expect(dir('chapter/foo [bar]/')).toMatchInlineSnapshot(`"chapter"`)
    expect(dir('path/[a-z]')).toMatchInlineSnapshot(`"path"`)
    expect(dir('[a-z]')).toMatchInlineSnapshot(`"."`)
    expect(dir('path/{to,from}')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/(foo bar)/subdir/foo.*')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/!(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/?(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/+(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/*(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/@(to|from)')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/!/foo')).toMatchInlineSnapshot(`"path/!/foo"`)
    expect(dir('path/?/foo')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/+/foo')).toMatchInlineSnapshot(`"path/+/foo"`)
    expect(dir('path/*/foo')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/@/foo')).toMatchInlineSnapshot(`"path/@/foo"`)
    expect(dir('path/!/foo/')).toMatchInlineSnapshot(`"path/!/foo/"`)
    expect(dir('path/?/foo/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/+/foo/')).toMatchInlineSnapshot(`"path/+/foo/"`)
    expect(dir('path/*/foo/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/@/foo/')).toMatchInlineSnapshot(`"path/@/foo/"`)
    expect(dir('path/**/*')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/**/subdir/foo.*')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/subdir/**/foo.js')).toMatchInlineSnapshot(`"path/subdir"`)
    expect(dir('path/!subdir/foo.js')).toMatchInlineSnapshot(`"path/!subdir/foo.js"`)
    expect(dir('path/{foo,bar}/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/foo')).toMatchInlineSnapshot(`"path/foo"`)
    expect(dir('path/foo/')).toMatchInlineSnapshot(`"path/foo/"`)
    expect(dir('path/foo/bar.js')).toMatchInlineSnapshot(`"path/foo/bar.js"`)
  })

  test('handle escaped characters', () => {
    expect(dir('path/\\*\\*/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*\\*/subdir"`)
    expect(dir('path/\\[\\*\\]/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\[\\*\\]/subdir"`)
    expect(dir('path/\\*(a|b)/subdir/foo.*')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/\\*/(a|b)/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*"`)
    expect(dir('path/\\*\\(a\\|b\\)/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*\\(a\\|b\\)/subdir"`)
    expect(dir('path/\\[foo bar\\]/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\[foo bar\\]/subdir"`)
    expect(dir('path/\\[bar]/')).toMatchInlineSnapshot(`"path/\\[bar]/"`)
    expect(dir('path/\\[bar]')).toMatchInlineSnapshot(`"path/\\[bar]"`)
    expect(dir('[bar]')).toMatchInlineSnapshot(`"."`)
    expect(dir('[bar]/')).toMatchInlineSnapshot(`"."`)
    expect(dir('./\\[bar]')).toMatchInlineSnapshot(`"./\\[bar]"`)
    expect(dir('\\[bar]/')).toMatchInlineSnapshot(`"\\[bar]/"`)
    expect(dir('\\!dir/*')).toMatchInlineSnapshot(`"\\!dir"`)
    expect(dir('[bar\\]/')).toMatchInlineSnapshot(`"[bar\\]/"`)
    expect(dir('path/foo \\[bar]/')).toMatchInlineSnapshot(`"path/foo \\[bar]/"`)
    expect(dir('path/\\{foo,bar}/')).toMatchInlineSnapshot(`"path/\\{foo,bar}/"`)
    expect(dir('\\{foo,bar}/')).toMatchInlineSnapshot(`"\\{foo,bar}/"`)
    expect(dir('\\{foo,bar\\}/')).toMatchInlineSnapshot(`"\\{foo,bar\\}/"`)
    expect(dir('{foo,bar\\}/')).toMatchInlineSnapshot(`"."`)
  })

  test('should handle glob patterns with braces', () => {
    expect(dir('js/t(wo|est)/*.js')).toMatchInlineSnapshot(`"js"`)
    expect(dir('js/t/(wo|est)/*.js')).toMatchInlineSnapshot(`"js/t"`)
    expect(dir('lib/{components,pages}/**/{test,another}/*.txt')).toMatchInlineSnapshot(`"lib"`)
    expect(dir('js/test{0..9}/*.js')).toMatchInlineSnapshot(`"js"`)
    expect(dir('js/t[a-z]st}/*.js')).toMatchInlineSnapshot(`"js"`)
    expect(dir('path/{../,./,{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/{../,./,\\{bar,/baz},qux}/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/\\{../,./,\\{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot(
      `"path/\\{../,./,\\{bar,/baz\\},qux\\}/"`,
    )
    expect(dir('{../,./,{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot(`"."`)
    expect(dir('{../,./,{bar,/baz\\},qux\\}')).toMatchInlineSnapshot(`"."`)
    expect(dir('path/{,/,bar/{baz,qux\\}}/')).toMatchInlineSnapshot(`"path"`)
    expect(dir('path/{,/,bar/{baz,qux}\\}/')).toMatchInlineSnapshot(`"path"`)
  })
})
