/**
 * @fileoverview Debug memory issue with incremental complexity
 */

import { describe, expect, it } from 'vitest';
import { OrdoJSLexer } from './lexer.js';
import { OrdoJSParser } from './parser.js';

describe('Debug Memory Issue', () => {
  const parseWithTimeout = (source: string, timeoutMs = 5000) => {
    return new Promise((resolve, reject) => {
      const timeout = setTimeout(() => {
        reject(new Error('Parsing timed out - likely infinite loop'));
      }, timeoutMs);

      try {
        const lexer = new OrdoJSLexer(source, 'test.ordo');
        const tokens = lexer.tokenize();
        const parser = new OrdoJSParser(tokens, {}, 'test.ordo');
        const ast = parser.parse();
        clearTimeout(timeout);
        resolve(ast);
      } catch (error) {
        clearTimeout(timeout);
        reject(error);
      }
    });
  };

  it('should handle simple HTML attributes without infinite loop', async () => {
    const source = `
      component TestComponent {
        markup {
          <div class="test">Hello</div>
        }
      }
    `;

    try {
      const ast = await parseWithTimeout(source);
      console.log('✅ Simple HTML attributes parsing succeeded');
      expect((ast as any).component.name).toBe('TestComponent');
    } catch (error) {
      console.log('❌ Simple HTML attributes parsing failed:', error.message);
      throw error;
    }
  });

  it('should handle multiple HTML attributes without infinite loop', async () => {
    const source = `
      component TestComponent {
        markup {
          <div class="test" id="main" data-value="123">Hello</div>
        }
      }
    `;

    try {
      const ast = await parseWithTimeout(source);
      console.log('✅ Multiple HTML attributes parsing succeeded');
      expect((ast as any).component.name).toBe('TestComponent');
    } catch (error) {
      console.log('❌ Multiple HTML attributes parsing failed:', error.message);
      throw error;
    }
  });

  it('should handle nested HTML elements without infinite loop', async () => {
    const source = `
      component TestComponent {
        markup {
          <div>
            <span>Hello</span>
            <p>World</p>
          </div>
        }
      }
    `;

    try {
      const ast = await parseWithTimeout(source);
      console.log('✅ Nested HTML elements parsing succeeded');
      expect((ast as any).component.name).toBe('TestComponent');
    } catch (error) {
      console.log('❌ Nested HTML elements parsing failed:', error.message);
      throw error;
    }
  });

  it('should handle interpolations without infinite loop', async () => {
    const source = `
      component TestComponent {
        markup {
          <div>{count}</div>
        }
      }
    `;

    try {
      const ast = await parseWithTimeout(source);
      console.log('✅ Interpolations parsing succeeded');
      expect((ast as any).component.name).toBe('TestComponent');
    } catch (error) {
      console.log('❌ Interpolations parsing failed:', error.message);
      throw error;
    }
  });

  it('should handle client block with functions without infinite loop', async () => {
    const source = `
      component TestComponent {
        client {
          handleClick(): void {
            console.log('clicked');
          }
        }
        markup {
          <div>Test</div>
        }
      }
    `;

    try {
      const ast = await parseWithTimeout(source);
      console.log('✅ Client block with functions parsing succeeded');
      expect((ast as any).component.name).toBe('TestComponent');
    } catch (error) {
      console.log('❌ Client block with functions parsing failed:', error.message);
      throw error;
    }
  });
});
