import { tap, expect, webhelpers } from '@git.zone/tstest/tapbundle';

import { BlockRegistry } from '../ts_web/elements/00group-input/dees-input-wysiwyg/blocks/block.registry.js';
import { DividerBlockHandler } from '../ts_web/elements/00group-input/dees-input-wysiwyg/blocks/content/divider.block.js';
import { ParagraphBlockHandler } from '../ts_web/elements/00group-input/dees-input-wysiwyg/blocks/text/paragraph.block.js';
import { HeadingBlockHandler } from '../ts_web/elements/00group-input/dees-input-wysiwyg/blocks/text/heading.block.js';

// Import block registration to ensure handlers are registered
import '../ts_web/elements/00group-input/dees-input-wysiwyg/wysiwyg.blockregistration.js';

tap.test('BlockRegistry should register and retrieve handlers', async () => {
  // Test divider handler
  const dividerHandler = BlockRegistry.getHandler('divider');
  expect(dividerHandler).toBeDefined();
  expect(dividerHandler).toBeInstanceOf(DividerBlockHandler);
  expect(dividerHandler?.type).toEqual('divider');

  // Test paragraph handler
  const paragraphHandler = BlockRegistry.getHandler('paragraph');
  expect(paragraphHandler).toBeDefined();
  expect(paragraphHandler).toBeInstanceOf(ParagraphBlockHandler);
  expect(paragraphHandler?.type).toEqual('paragraph');

  // Test heading handlers
  const heading1Handler = BlockRegistry.getHandler('heading-1');
  expect(heading1Handler).toBeDefined();
  expect(heading1Handler).toBeInstanceOf(HeadingBlockHandler);
  expect(heading1Handler?.type).toEqual('heading-1');

  const heading2Handler = BlockRegistry.getHandler('heading-2');
  expect(heading2Handler).toBeDefined();
  expect(heading2Handler).toBeInstanceOf(HeadingBlockHandler);
  expect(heading2Handler?.type).toEqual('heading-2');

  const heading3Handler = BlockRegistry.getHandler('heading-3');
  expect(heading3Handler).toBeDefined();
  expect(heading3Handler).toBeInstanceOf(HeadingBlockHandler);
  expect(heading3Handler?.type).toEqual('heading-3');
});

tap.test('Block handlers should render content correctly', async () => {
  const testBlock = {
    id: 'test-1',
    type: 'paragraph' as const,
    content: 'Test paragraph content'
  };

  const handler = BlockRegistry.getHandler('paragraph');
  expect(handler).toBeDefined();

  if (handler) {
    const rendered = handler.render(testBlock, false);
    // The render() method returns the HTML template structure
    // Content is set later in setup()
    expect(rendered).toContain('contenteditable="true"');
    expect(rendered).toContain('data-block-type="paragraph"');
    expect(rendered).toContain('data-block-id="test-1"');
    expect(rendered).toContain('class="block paragraph"');
  }
});

tap.test('Divider handler should render correctly', async () => {
  const dividerBlock = {
    id: 'test-divider',
    type: 'divider' as const,
    content: ' '
  };

  const handler = BlockRegistry.getHandler('divider');
  expect(handler).toBeDefined();

  if (handler) {
    const rendered = handler.render(dividerBlock, false);
    expect(rendered).toContain('class="block divider"');
    expect(rendered).toContain('tabindex="0"');
    expect(rendered).toContain('<hr>');
    expect(rendered).toContain('data-block-id="test-divider"');
  }
});

tap.test('Heading handlers should render with correct levels', async () => {
  const headingBlock = {
    id: 'test-h1',
    type: 'heading-1' as const,
    content: 'Test Heading'
  };

  const handler = BlockRegistry.getHandler('heading-1');
  expect(handler).toBeDefined();

  if (handler) {
    const rendered = handler.render(headingBlock, false);
    // The render() method returns the HTML template structure
    // Content is set later in setup()
    expect(rendered).toContain('class="block heading-1"');
    expect(rendered).toContain('contenteditable="true"');
    expect(rendered).toContain('data-block-id="test-h1"');
    expect(rendered).toContain('data-block-type="heading-1"');
  }
});

tap.test('getAllTypes should return all registered types', async () => {
  const allTypes = BlockRegistry.getAllTypes();
  expect(allTypes).toContain('divider');
  expect(allTypes).toContain('paragraph');
  expect(allTypes).toContain('heading-1');
  expect(allTypes).toContain('heading-2');
  expect(allTypes).toContain('heading-3');
  expect(allTypes.length).toBeGreaterThanOrEqual(5);
});

export default tap.start();