import { generate, baseParse, transform } from '@vue/compiler-core'
import {
  transformAssetUrl,
  createAssetUrlTransformWithOptions,
  AssetURLOptions,
  normalizeOptions
} from '../src/templateTransformAssetUrl'
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
import { transformBind } from '../../compiler-core/src/transforms/vBind'
function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
  const ast = baseParse(template)
  const t = options
    ? createAssetUrlTransformWithOptions(normalizeOptions(options))
    : transformAssetUrl
  transform(ast, {
    nodeTransforms: [t, transformElement],
    directiveTransforms: {
      bind: transformBind
    }
  })
  return generate(ast, { mode: 'module' })
}
describe('compiler sfc: transform asset url', () => {
  test('transform assetUrls', () => {
    const result = compileWithAssetUrls(`
			 
			 
			 
			 
			 `)
    expect(result.code).toMatchSnapshot()
  })
  /**
   * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
   */
  test('support uri fragment', () => {
    const result = compileWithAssetUrls(
      ''
    )
    expect(result.code).toMatchSnapshot()
  })
  /**
   * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
   */
  test('support uri is empty', () => {
    const result = compileWithAssetUrls('')
    expect(result.code).toMatchSnapshot()
  })
  test('with explicit base', () => {
    const { code } = compileWithAssetUrls(
      `
		`)
    expect(result.code).toMatchSnapshot()
  })
  /**
   * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
   */
  test('support uri fragment', () => {
    const result = compileWithAssetUrls(
      ''
    )
    expect(result.code).toMatchSnapshot()
  })
  /**
   * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
   */
  test('support uri is empty', () => {
    const result = compileWithAssetUrls('')
    expect(result.code).toMatchSnapshot()
  })
  test('with explicit base', () => {
    const { code } = compileWithAssetUrls(
      ` ` + // -> /foo/bar.png
      `
` + // -> /foo/bar.png
      ` ` + // -> /foo/bar.png
      `
` + // -> /foo/bar.png
      ` ` + // -> bar.png (untouched)
        `
` + // -> bar.png (untouched)
        ` `, // -> @theme/bar.png (untouched)
      {
        base: '/foo'
      }
    )
    expect(code).toMatchSnapshot()
  })
  test('with includeAbsolute: true', () => {
    const { code } = compileWithAssetUrls(
      `
`, // -> @theme/bar.png (untouched)
      {
        base: '/foo'
      }
    )
    expect(code).toMatchSnapshot()
  })
  test('with includeAbsolute: true', () => {
    const { code } = compileWithAssetUrls(
      ` ` +
        `
` +
        ` ` +
        `
` +
        ` `,
      {
        includeAbsolute: true
      }
    )
    expect(code).toMatchSnapshot()
  })
})
`,
      {
        includeAbsolute: true
      }
    )
    expect(code).toMatchSnapshot()
  })
})