import { fixtureDefaults } from '@bamboocss/fixture'
import { type Artifact } from '@bamboocss/types'
import { describe, expect, test } from 'vitest'
import { Generator } from '../src'

const formatArtifact = (artifact: Artifact) => {
  if (!artifact) return
  const dir = (artifact.dir ?? []).filter((f) => f !== fixtureDefaults.config.outdir)
  return artifact.files.map((f) => dir + '/' + f.file)
}

describe('setup-artifacts', () => {
  test('filter by ArtifactId', () => {
    const generator = new Generator(fixtureDefaults)
    expect(generator.getArtifacts(['create-recipe']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "recipes/create-recipe.mjs",
        ],
      ]
    `)
    expect(generator.getArtifacts(['create-recipe', 'css-fn']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "css/conditions.mjs",
          "css/utilities.mjs",
          "css/merge-css.mjs",
          "css/merge-css.d.ts",
          "css/css.mjs",
          "css/css.d.ts",
        ],
        [
          "recipes/create-recipe.mjs",
        ],
      ]
    `)
    expect(generator.getArtifacts(['recipes']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "recipes/text-style.mjs",
          "recipes/text-style.d.ts",
          "recipes/tooltip-style.mjs",
          "recipes/tooltip-style.d.ts",
          "recipes/card-style.mjs",
          "recipes/card-style.d.ts",
          "recipes/button-style.mjs",
          "recipes/button-style.d.ts",
          "recipes/checkbox.mjs",
          "recipes/checkbox.d.ts",
          "recipes/badge.mjs",
          "recipes/badge.d.ts",
        ],
      ]
    `)
    expect(generator.getArtifacts(['recipes', 'recipes.button-style']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "recipes/button-style.mjs",
          "recipes/button-style.d.ts",
        ],
      ]
    `)
    expect(generator.getArtifacts(['recipes', 'recipes.button-style', 'recipes.tooltip-style']).map(formatArtifact))
      .toMatchInlineSnapshot(`
        [
          [
            "recipes/tooltip-style.mjs",
            "recipes/tooltip-style.d.ts",
            "recipes/button-style.mjs",
            "recipes/button-style.d.ts",
          ],
        ]
      `)
    expect(generator.getArtifacts(['patterns']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "patterns/flex.mjs",
          "patterns/flex.d.ts",
          "patterns/spacer.mjs",
          "patterns/spacer.d.ts",
          "patterns/center.mjs",
          "patterns/center.d.ts",
          "patterns/link-overlay.mjs",
          "patterns/link-overlay.d.ts",
          "patterns/aspect-ratio.mjs",
          "patterns/aspect-ratio.d.ts",
          "patterns/grid.mjs",
          "patterns/grid.d.ts",
          "patterns/grid-item.mjs",
          "patterns/grid-item.d.ts",
          "patterns/container.mjs",
          "patterns/container.d.ts",
          "patterns/divider.mjs",
          "patterns/divider.d.ts",
          "patterns/float.mjs",
          "patterns/float.d.ts",
          "patterns/bleed.mjs",
          "patterns/bleed.d.ts",
        ],
      ]
    `)

    // A pattern id on its own selects no artifact; paired with `patterns` it narrows that
    // artifact to the one pattern. Named for a pattern that exists — pointed at a removed one,
    // both assertions pass vacuously and stop testing the narrowing at all.
    expect(generator.getArtifacts(['patterns.center']).map(formatArtifact)).toMatchInlineSnapshot('[]')
    expect(generator.getArtifacts(['patterns', 'patterns.center']).map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "patterns/center.mjs",
          "patterns/center.d.ts",
        ],
      ]
    `)
  })

  test('getArtifacts', () => {
    const generator = new Generator(fixtureDefaults)
    expect(generator.getArtifacts().map(formatArtifact)).toMatchInlineSnapshot(`
      [
        [
          "/package.json",
        ],
        [
          "/helpers.mjs",
        ],
        [
          "tokens/index.d.ts",
          "tokens/index.mjs",
          "tokens/tokens.d.ts",
        ],
        [
          "types/global.d.ts",
          "types/index.d.ts",
        ],
        [
          "types/prop-type.d.ts",
          "types/style-props.d.ts",
        ],
        [
          "types/conditions.d.ts",
        ],
        [
          "types/csstype.d.ts",
          "types/static-css.d.ts",
          "types/selectors.d.ts",
          "types/composition.d.ts",
          "types/recipe.d.ts",
          "types/pattern.d.ts",
        ],
        [
          "types/system-types.d.ts",
        ],
        [
          "css/conditions.mjs",
          "css/utilities.mjs",
          "css/merge-css.mjs",
          "css/merge-css.d.ts",
          "css/css.mjs",
          "css/css.d.ts",
        ],
        [
          "css/cva.mjs",
          "css/cva.d.ts",
        ],
        [
          "css/sva.mjs",
          "css/sva.d.ts",
        ],
        [
          "css/cx.mjs",
          "css/cx.d.ts",
        ],
        [
          "recipes/create-recipe.mjs",
        ],
        [
          "recipes/index.mjs",
          "recipes/index.d.ts",
        ],
        [
          "recipes/text-style.mjs",
          "recipes/text-style.d.ts",
          "recipes/tooltip-style.mjs",
          "recipes/tooltip-style.d.ts",
          "recipes/card-style.mjs",
          "recipes/card-style.d.ts",
          "recipes/button-style.mjs",
          "recipes/button-style.d.ts",
          "recipes/checkbox.mjs",
          "recipes/checkbox.d.ts",
          "recipes/badge.mjs",
          "recipes/badge.d.ts",
        ],
        [
          "patterns/index.mjs",
          "patterns/index.d.ts",
        ],
        [
          "patterns/flex.mjs",
          "patterns/flex.d.ts",
          "patterns/spacer.mjs",
          "patterns/spacer.d.ts",
          "patterns/center.mjs",
          "patterns/center.d.ts",
          "patterns/link-overlay.mjs",
          "patterns/link-overlay.d.ts",
          "patterns/aspect-ratio.mjs",
          "patterns/aspect-ratio.d.ts",
          "patterns/grid.mjs",
          "patterns/grid.d.ts",
          "patterns/grid-item.mjs",
          "patterns/grid-item.d.ts",
          "patterns/container.mjs",
          "patterns/container.d.ts",
          "patterns/divider.mjs",
          "patterns/divider.d.ts",
          "patterns/float.mjs",
          "patterns/float.d.ts",
          "patterns/bleed.mjs",
          "patterns/bleed.d.ts",
        ],
        [
          "css/index.mjs",
          "css/index.d.ts",
        ],
      ]
    `)
  })
})
