/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { basename } from 'node:path';
/** Classify an Apex class into a category based on name and content patterns. */
function classifyApex(name, content) {
    if (name.endsWith('_Test') || name.endsWith('Test') || content.includes('@isTest') || content.includes('@IsTest')) {
        return 'test';
    }
    if (content.includes('@InvocableMethod') ||
        content.includes('@RestResource') ||
        content.includes('implements Queueable') ||
        content.includes('implements Database.Batchable') ||
        content.includes('implements Schedulable')) {
        return 'invocable';
    }
    if (content.includes('extends TriggerHandler') ||
        content.includes('implements TriggerHandler') ||
        content.includes('TriggerHandler.')) {
        return 'trigger-handler';
    }
    if (name.includes('Service') || name.includes('Orchestrator')) {
        return 'service';
    }
    if (name.includes('Domain') || content.includes('SObject') || content.includes('__c')) {
        return 'domain';
    }
    return 'utility';
}
export const apexParser = {
    type: 'ApexClass',
    glob: '**/classes/*.cls',
    parse(filePath, content) {
        const name = basename(filePath, '.cls');
        if (!name)
            return null;
        const category = classifyApex(name, content);
        return {
            apiName: name,
            type: 'ApexClass',
            path: filePath,
            category,
            dependencies: [],
        };
    },
    extractDependencies(_entry, content) {
        const deps = new Set();
        // Class instantiation: new ClassName(
        const newPattern = /new\s+([A-Z][A-Za-z0-9_]+)\s*\(/g;
        let match = newPattern.exec(content);
        while (match) {
            deps.add(match[1]);
            match = newPattern.exec(content);
        }
        // SOQL FROM: FROM ObjectName
        const soqlPattern = /FROM\s+([a-z][a-z0-9_]+)/gi;
        match = soqlPattern.exec(content);
        while (match) {
            deps.add(match[1]);
            match = soqlPattern.exec(content);
        }
        // Type annotations: ClassName.method or ClassName variable
        const typePattern = /:\s*([A-Z][A-Za-z0-9_]+)\s/g;
        match = typePattern.exec(content);
        while (match) {
            deps.add(match[1]);
            match = typePattern.exec(content);
        }
        // extends / implements
        const extendsPattern = /(?:extends|implements)\s+([A-Z][A-Za-z0-9_]+)/g;
        match = extendsPattern.exec(content);
        while (match) {
            deps.add(match[1]);
            match = extendsPattern.exec(content);
        }
        // Remove common Apex built-in types
        const builtins = new Set([
            'String',
            'Integer',
            'Boolean',
            'Decimal',
            'Double',
            'Long',
            'Date',
            'Datetime',
            'Time',
            'Id',
            'Blob',
            'Object',
            'List',
            'Set',
            'Map',
            'System',
            'Test',
            'Database',
            'Schema',
            'Limits',
            'UserInfo',
            'Math',
            'JSON',
            'Type',
            'Exception',
            'DmlException',
            'QueryException',
            'Trigger',
            'Assert',
            'SObject',
            'ApexPages',
            'Messaging',
            'Pattern',
            'Matcher',
        ]);
        for (const b of builtins)
            deps.delete(b);
        return [...deps];
    },
};
//# sourceMappingURL=apex-parser.js.map