import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import ts from 'typescript';
import { fileURLToPath } from 'node:url';

const currentDirectory = path.dirname(fileURLToPath(import.meta.url));
const sourcePath = path.resolve(
  currentDirectory,
  '../../src/app/home/components/dynamic-form/DynamicFormSaveValues.ts',
);

function loadNormalizer() {
  const source = fs.readFileSync(sourcePath, 'utf8');
  const compiled = ts.transpileModule(source, {
    compilerOptions: { module: ts.ModuleKind.CommonJS },
  }).outputText;
  const loadedModule = { exports: {} };
  new Function('require', 'module', 'exports', compiled)(
    () => {
      throw new Error('DynamicFormSaveValues must not have runtime imports');
    },
    loadedModule,
    loadedModule.exports,
  );
  return loadedModule.exports;
}

test('normalizes only single-line text fields in a dynamic form save snapshot', () => {
  const { normalizeDynamicFormValuesForSave } = loadNormalizer();
  const specs = [
    { name: 'single-line', type: 'string', default: '' },
    { name: 'multiline', type: 'text', default: '' },
    { name: 'string-list', type: 'array[string]', default: [] },
    { name: 'count', type: 'integer', default: 0 },
    {
      name: 'model',
      type: 'model-fallback-selector',
      default: { primary: '', fallbacks: [], reasoning: {} },
    },
  ];
  const values = {
    'single-line': '\t  hello  world \n',
    multiline: '  keep multiline whitespace  \n',
    'string-list': ['  first  ', ' second '],
    count: 3,
    model: {
      primary: 'primary-model',
      fallbacks: ['fallback-model'],
      reasoning: {
        'primary-model': 'high',
        'fallback-model': 'provider_default',
        'removed-model': 'medium',
      },
    },
  };

  assert.deepEqual(normalizeDynamicFormValuesForSave(specs, values), {
    'single-line': 'hello  world',
    multiline: '  keep multiline whitespace  \n',
    'string-list': ['  first  ', ' second '],
    count: 3,
    model: {
      primary: 'primary-model',
      fallbacks: ['fallback-model'],
      reasoning: {
        'primary-model': 'high',
      },
    },
  });
});

test('normalizes missing dynamic form defaults into controlled values', () => {
  const { normalizeDynamicFormFieldValue } = loadNormalizer();

  assert.equal(
    normalizeDynamicFormFieldValue(
      { name: 'api-key', type: 'string', default: undefined },
      undefined,
    ),
    '',
  );
  assert.equal(
    normalizeDynamicFormFieldValue(
      { name: 'enabled', type: 'boolean', default: undefined },
      undefined,
    ),
    false,
  );
  assert.deepEqual(
    normalizeDynamicFormFieldValue(
      { name: 'items', type: 'array[string]', default: undefined },
      undefined,
    ),
    [],
  );
});
