import { describe, expect, it } from 'vitest';

import {
  isValidEsaResourceUrl,
  normalizeEsaFundingContent,
  toggleEsaFaq,
} from '@/business/esa-funding/selectors';

describe('ESA funding selectors', () => {
  it('toggles FAQ expansion', () => {
    expect(toggleEsaFaq(0, 0)).toBeNull();
    expect(toggleEsaFaq(0, 1)).toBe(1);
    expect(toggleEsaFaq(null, 1)).toBe(1);
  });

  it('validates resource URLs', () => {
    expect(isValidEsaResourceUrl('https://example.com')).toBe(true);
    expect(isValidEsaResourceUrl('http://example.com')).toBe(true);
    expect(isValidEsaResourceUrl('#')).toBe(false);
    expect(isValidEsaResourceUrl('mailto:test@example.com')).toBe(false);
    expect(isValidEsaResourceUrl('not a url')).toBe(false);
  });

  it('normalizes multiple parent conversation references', () => {
    const content = normalizeEsaFundingContent({
      parentConversationReferences: [
        { id: 'reference-one', question: 'First question', answer: 'First answer' },
        { question: 'Second question', answer: 'Second answer' },
      ],
    });

    expect(content.parentConversationReferences).toEqual([
      { id: 'reference-one', question: 'First question', answer: 'First answer' },
      { id: 'parent-reference-second-question', question: 'Second question', answer: 'Second answer' },
    ]);
  });

  it('converts legacy parent conversation reference text into Q&A format', () => {
    const content = normalizeEsaFundingContent({
      parentConversationReferences: [
        { id: 'reference-one', text: 'First reference' },
      ],
    });

    expect(content.parentConversationReferences).toEqual([
      {
        id: 'reference-one',
        question: 'If a parent asks about ESA, here is a simple way to explain it:',
        answer: 'First reference',
      },
    ]);
  });

  it('converts legacy parent conversation script into one reference', () => {
    const content = normalizeEsaFundingContent({
      parentConversationScript: 'Legacy script',
    });

    expect(content.parentConversationReferences).toEqual([
      {
        id: 'parent-reference-legacy-script',
        question: 'If a parent asks about ESA, here is a simple way to explain it:',
        answer: 'Legacy script',
      },
    ]);
  });
});
