import { Node } from 'web-tree-sitter';
import { JSIndexer } from './js-indexer.js';
import { ExportMetadata, SymbolType } from '@scandiumsys/owstra-types';

export class TypeScriptIndexer extends JSIndexer {
  public override readonly language: string = 'typescript';

  public override extractExports(root: Node, filePath: string): ExportMetadata[] {
    const exports = super.extractExports(root, filePath);

    const addExport = (name: string, type: SymbolType, node: Node) => {
      exports.push({
        name,
        type,
        filePath,
        line: node.startPosition.row + 1,
        column: node.startPosition.column + 1,
        parameters: this.extractParameters(node)
      });
    };

    // 1. Interfaces
    const interfaceNodes = root.descendantsOfType('interface_declaration');
    for (const node of interfaceNodes) {
        const nameNode = node.childForFieldName('name');
        if (nameNode) addExport(nameNode.text, 'interface' as any, node);
    }

    // 2. Types
    const typeNodes = root.descendantsOfType('type_alias_declaration');
    for (const node of typeNodes) {
        const nameNode = node.childForFieldName('name');
        if (nameNode) addExport(nameNode.text, 'type' as any, node);
    }

    return exports;
  }

  // extractImports and extractCalls are mostly same as JS for TS
}
