import { Token } from '../types';
import { TokenizerStrategy } from '../tokenizer.interface';
import type { Tree, Node, Parser } from 'web-tree-sitter';

export class TreeSitterTokenizer implements TokenizerStrategy {
  constructor(private parser: Parser) {}

  tokenize(content: string): Token[] {
    const tree = this.parser.parse(content);
    return this.collectTokens(tree, content);
  }

  private collectTokens(tree: Tree, code: string): Token[] {
    const tokens: Token[] = [];

    function visit(node: Node) {
      if (node.childCount === 0) {
        tokens.push({
          text: code.slice(node.startIndex, node.endIndex),
          type: node.type,
          startIndex: node.startIndex,
          endIndex: node.endIndex,
          startPosition: node.startPosition,
        });
        return;
      }
      // By iterating over all children (not just named), we include punctuation
      for (let i = 0; i < node.childCount; i++) {
        const child = node.child(i);
        if (child) visit(child);
      }
    }

    visit(tree.rootNode);
    return tokens;
  }
}