/**
 * @fileoverview Simplified OrdoJS Parser for testing
 */

import {
  type ComponentAST,
  type ComponentNode,
  type HTMLElementNode,
  type MarkupBlockNode,
  type SourcePosition,
  type SourceRange,
  type TextNode,
  type TokenStream
} from '../types/index.js';

/**
 * Parser configuration options
 */
interface ParserOptions {
  allowRecovery: boolean;
  maxErrors: number;
  strictMode: boolean;
}

/**
 * Default parser options
 */
const DEFAULT_OPTIONS: ParserOptions = {
  allowRecovery: true,
  maxErrors: 10,
  strictMode: false
};

/**
 * Simplified OrdoJS Parser for testing
 */
export class OrdoJSParser {
  private tokens: TokenStream;
  private errors: any[] = [];
  private options: ParserOptions;
  private filename?: string;

  constructor(tokens: TokenStream, options: Partial<ParserOptions> = {}, filename?: string) {
    this.tokens = tokens;
    this.options = { ...DEFAULT_OPTIONS, ...options };
    this.filename = filename;
  }

  /**
   * Parse the token stream into a ComponentAST
   */
  parse(): ComponentAST {
    // Create a minimal component AST for testing
    const componentNode: ComponentNode = {
      type: 'Component',
      name: 'SimpleComponent',
      props: [],
      markupBlock: this.createSimpleMarkupBlock(),
      range: this.createRange({ line: 1, column: 1, offset: 0 }, { line: 10, column: 1, offset: 100 })
    };

    return {
      component: componentNode,
      dependencies: [],
      exports: [],
      sourceMap: {
        version: 3,
        sources: [],
        names: [],
        mappings: '',
        sourcesContent: []
      }
    };
  }

  /**
   * Create a simple markup block for testing
   */
  private createSimpleMarkupBlock(): MarkupBlockNode {
    // Create a text node
    const textNode: TextNode = {
      type: 'Text',
      content: 'Hello, World!',
      range: this.createRange({ line: 3, column: 10, offset: 30 }, { line: 3, column: 23, offset: 43 })
    };

    // Create an HTML element
    const element: HTMLElementNode = {
      type: 'HTMLElement',
      tagName: 'div',
      attributes: [],
      children: [textNode],
      isSelfClosing: false,
      isVoidElement: false,
      range: this.createRange({ line: 3, column: 5, offset: 25 }, { line: 3, column: 30, offset: 50 })
    };

    // Create a markup block
    return {
      type: 'MarkupBlock',
      elements: [element],
      textNodes: [textNode],
      interpolations: [],
      range: this.createRange({ line: 2, column: 3, offset: 15 }, { line: 4, column: 3, offset: 55 }),
      children: [element]
    };
  }

  /**
   * Create a source range
   */
  private createRange(start: SourcePosition, end: SourcePosition): SourceRange {
    return { start, end };
  }

  /**
   * Get parsing errors
   */
  getErrors(): any[] {
    return this.errors;
  }
}
