import ts from 'typescript';
/**
 * TypeScript AST Chunker.
 *
 * Chunks by function, class, interface, type alias, enum declarations.
 * Detects React components (PascalCase) and hooks (use* prefix).
 * Context: `File: <path> | Context: The '<name>' <kind>`
 */
export class TypeScriptChunker {
    strategy = 'typescript-ast';
    chunk(content, filePath, type) {
        const scriptKind = filePath.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
        const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, scriptKind);
        const chunks = [];
        const lines = content.split('\n');
        const visit = (node, depth) => {
            // Only chunk top-level declarations (class methods stay inside the class chunk)
            if (depth > 1)
                return;
            const extracted = this.extractDeclaration(node, sourceFile, lines, filePath, type);
            if (extracted) {
                chunks.push(extracted);
                return;
            }
            ts.forEachChild(node, (child) => visit(child, depth + 1));
        };
        ts.forEachChild(sourceFile, (child) => visit(child, 0));
        return chunks;
    }
    extractDeclaration(node, sourceFile, lines, filePath, contentType) {
        let name = null;
        let kind = null;
        if (ts.isFunctionDeclaration(node) && node.name) {
            name = node.name.text;
            kind = this.classifyFunction(name);
        }
        else if (ts.isClassDeclaration(node) && node.name) {
            name = node.name.text;
            kind = 'class';
        }
        else if (ts.isInterfaceDeclaration(node)) {
            name = node.name.text;
            kind = 'interface';
        }
        else if (ts.isTypeAliasDeclaration(node)) {
            name = node.name.text;
            kind = 'type alias';
        }
        else if (ts.isEnumDeclaration(node)) {
            name = node.name.text;
            kind = 'enum';
        }
        else if (ts.isVariableStatement(node)) {
            // Handle: export const MyComponent = () => { ... }
            for (const decl of node.declarationList.declarations) {
                if (ts.isIdentifier(decl.name) && decl.initializer) {
                    if (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer)) {
                        name = decl.name.text;
                        kind = this.classifyFunction(name);
                    }
                }
            }
        }
        if (!name || !kind)
            return null;
        const startPos = node.getStart(sourceFile);
        const endPos = node.getEnd();
        const startLine = sourceFile.getLineAndCharacterOfPosition(startPos).line + 1;
        const endLine = sourceFile.getLineAndCharacterOfPosition(endPos).line + 1;
        const text = lines.slice(startLine - 1, endLine).join('\n');
        const contextPrefix = `File: ${filePath} | Context: The '${name}' ${kind}`;
        return {
            content: text,
            contextPrefix,
            filePath,
            type: contentType,
            strategy: this.strategy,
            label: `${kind}: ${name}`,
            startLine,
            endLine,
            metadata: { name, kind },
        };
    }
    classifyFunction(name) {
        if (name.startsWith('use') && name[3]?.toUpperCase() === name[3]) {
            return 'hook';
        }
        if (name[0]?.toUpperCase() === name[0]) {
            return 'component';
        }
        return 'function';
    }
}
//# sourceMappingURL=typescript-chunker.js.map