const assert = require('assert')
const { test } = require('node:test')
const {
  normalizeTemplateIdArg,
  normalizeCidArg,
  resolveLibraryTemplateId,
  resolveLibraryCid,
} = require('../src/lib/cli-cid')

test('normalizeTemplateIdArg strips matching double quotes', () => {
  assert.strictEqual(normalizeTemplateIdArg('"-OrFkw6Pb8ZeCuMkx-Cg"'), '-OrFkw6Pb8ZeCuMkx-Cg')
})

test('normalizeTemplateIdArg strips matching single quotes', () => {
  assert.strictEqual(normalizeTemplateIdArg("'-OrFkw6Pb8ZeCuMkx-Cg'"), '-OrFkw6Pb8ZeCuMkx-Cg')
})

test('normalizeTemplateIdArg leaves unquoted ids', () => {
  assert.strictEqual(normalizeTemplateIdArg('abc123'), 'abc123')
  assert.strictEqual(normalizeTemplateIdArg('-OrFkw6Pb8ZeCuMkx-Cg'), '-OrFkw6Pb8ZeCuMkx-Cg')
})

test('normalizeCidArg is an alias of normalizeTemplateIdArg', () => {
  assert.strictEqual(normalizeCidArg(' "x" '), normalizeTemplateIdArg(' "x" '))
})

test('resolveLibraryTemplateId prefers --template-id over positional', () => {
  assert.strictEqual(resolveLibraryTemplateId({ templateId: 'from-opt' }, 'from-pos'), 'from-opt')
})

test('resolveLibraryTemplateId uses hidden --cid when --template-id empty', () => {
  assert.strictEqual(resolveLibraryTemplateId({ cid: 'from-cid', templateId: '' }, 'from-pos'), 'from-cid')
})

test('resolveLibraryTemplateId uses positional when options empty', () => {
  assert.strictEqual(resolveLibraryTemplateId({ templateId: '', cid: '   ' }, 'from-pos'), 'from-pos')
})

test('resolveLibraryTemplateId normalizes', () => {
  assert.strictEqual(resolveLibraryTemplateId({ templateId: '"-x"' }, undefined), '-x')
})

test('resolveLibraryCid matches resolveLibraryTemplateId', () => {
  assert.strictEqual(
    resolveLibraryCid({ templateId: 'a' }, 'b'),
    resolveLibraryTemplateId({ templateId: 'a' }, 'b'),
  )
})
