import { describe, expect, it } from 'vitest';
import {
  applyWelcomePlaceholders,
  capsRatio,
  countEmojis,
  hasZalgo,
  isDiscordInvite,
  normalizeHost
} from './phase2.js';

describe('phase2 helpers', () => {
  it('replaces welcome placeholders', () => {
    const result = applyWelcomePlaceholders('Hello {user} on {server}', {
      user: '<@1>',
      server: 'Nexumi'
    });
    expect(result).toBe('Hello <@1> on Nexumi');
  });

  it('expands bracket channel mentions', () => {
    const result = applyWelcomePlaceholders('Join ["123456789012345678"] please', {});
    expect(result).toBe('Join <#123456789012345678> please');
  });

  it('keeps native discord channel mentions', () => {
    const result = applyWelcomePlaceholders('Join <#123456789012345678> please', {});
    expect(result).toBe('Join <#123456789012345678> please');
  });

  it('maps embed text fields with field names', async () => {
    const { mapEmbedTextFields } = await import('./phase2.js');
    const mapped = mapEmbedTextFields(
      { title: 'T:{user}', description: 'D:{user}', footerText: 'F:{user}' },
      (value, field) => `${field}:${value}`
    );
    expect(mapped.title).toBe('title:T:{user}');
    expect(mapped.description).toBe('description:D:{user}');
    expect(mapped.footerText).toBe('footerText:F:{user}');
  });

  it('accepts extended embed fields', async () => {
    const { WelcomeEmbedSchema, embedHasContent } = await import('./phase2.js');
    const parsed = WelcomeEmbedSchema.parse({
      title: 'Hi',
      authorName: 'Nexumi',
      imageUrl: 'https://example.com/banner.png',
      footerText: 'Nexumi',
      timestamp: true
    });
    expect(parsed.authorName).toBe('Nexumi');
    expect(embedHasContent(parsed)).toBe(true);
  });

  it('detects discord invites', () => {
    expect(isDiscordInvite('join https://discord.gg/test')).toBe(true);
    expect(isDiscordInvite('hello world')).toBe(false);
  });

  it('calculates caps ratio', () => {
    expect(capsRatio('HELLO WORLD')).toBeGreaterThan(50);
    expect(capsRatio('hello')).toBe(0);
  });

  it('counts emojis', () => {
    expect(countEmojis('hello 😀😀')).toBe(2);
  });

  it('detects zalgo text', () => {
    expect(hasZalgo('h\u0305e\u0305l\u0305l\u0305o\u0305')).toBe(true);
    expect(hasZalgo('hello')).toBe(false);
  });

  it('normalizes hostnames', () => {
    expect(normalizeHost('https://www.Example.com/path')).toBe('example.com');
  });
});
