/**
 * Inline-assertion smoke test for mentionPresets.
 *
 * The `@djangocfg/ui-tools` package has no test runner configured
 * (`package.json` only has build / clean / dev / playground / check).
 * Rather than introduce vitest just for this, we keep the assertions
 * inline and runnable as a one-shot Node script.
 *
 * Run:
 *   cd packages/ui-tools
 *   npx tsx src/tools/MarkdownEditor/mentionPresets.test.ts
 *
 * Or just trust `npm run check` (tsc --noEmit) to catch type drift —
 * the file is excluded from tsup's `entry`, so it never ships in `dist`.
 */
import { mentionPresets } from './mentionPresets';

let failures = 0;
const eq = (label: string, got: string, want: string) => {
  const ok = got === want;
  if (!ok) failures += 1;
  // eslint-disable-next-line no-console
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${label}`);
  if (!ok) {
    // eslint-disable-next-line no-console
    console.log(`        got:  ${JSON.stringify(got)}`);
    // eslint-disable-next-line no-console
    console.log(`        want: ${JSON.stringify(want)}`);
  }
};
const includes = (label: string, got: string, needle: string) => {
  const ok = got.includes(needle);
  if (!ok) failures += 1;
  // eslint-disable-next-line no-console
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${label}`);
  if (!ok) {
    // eslint-disable-next-line no-console
    console.log(`        got:    ${JSON.stringify(got)}`);
    // eslint-disable-next-line no-console
    console.log(`        needle: ${JSON.stringify(needle)}`);
  }
};

// ── plainAt ──
eq(
  'plainAt label present',
  mentionPresets.plainAt({ label: 'Vps-audi', id: 'uuid' }),
  '@Vps-audi',
);
eq(
  'plainAt empty label falls back to id',
  mentionPresets.plainAt({ label: '', id: 'uuid' }),
  '@uuid',
);

// ── plainLabel ──
eq(
  'plainLabel returns label as-is',
  mentionPresets.plainLabel({ label: 'X', id: '1' }),
  'X',
);

// ── markdownLink ──
{
  const link = mentionPresets.markdownLink('https://x.com/u/');
  const out = link({ label: 'X Y', id: '1' });
  includes('markdownLink contains label text', out, 'X Y');
  includes('markdownLink contains encoded id', out, 'https://x.com/u/1');
  includes('markdownLink wraps with [@...](', out, '[@X Y](https://x.com/u/1)');

  const escaped = link({ label: 'A_B*C[D]', id: 'id 2' });
  includes('markdownLink escapes label specials', escaped, '\\_');
  includes('markdownLink escapes brackets', escaped, '\\[');
  includes('markdownLink escapes asterisk', escaped, '\\*');
  includes('markdownLink encodes id space', escaped, 'id%202');
}

// ── customUri ──
eq(
  'customUri Notion-style',
  mentionPresets.customUri('cmdop', 'machine')({ label: 'My Box', id: 'uuid' }),
  '@[My Box](cmdop://machine/uuid)',
);

// ── slackStyle ──
eq(
  'slackStyle drops label, emits <@id>',
  mentionPresets.slackStyle({ label: 'X', id: 'U123' }),
  '<@U123>',
);

// ── htmlSpan ──
{
  const out = mentionPresets.htmlSpan('m')({ label: 'X', id: '1' });
  includes('htmlSpan uses class', out, 'class="m"');
  includes('htmlSpan exposes data-mention-id', out, 'data-mention-id="1"');
  includes('htmlSpan body shows label with @', out, '@X');
}

if (failures > 0) {
  // eslint-disable-next-line no-console
  console.error(`\n${failures} assertion(s) failed.`);
  process.exit(1);
} else {
  // eslint-disable-next-line no-console
  console.log('\nAll mentionPresets assertions passed.');
}
