/*
 * 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';
export const triggerParser = {
    type: 'ApexTrigger',
    glob: '**/triggers/*.trigger',
    parse(filePath, content) {
        const name = basename(filePath, '.trigger');
        if (!name)
            return null;
        // Extract object name from: trigger TriggerName on ObjectName (...)
        const triggerPattern = /trigger\s+\w+\s+on\s+([A-Za-z][A-Za-z0-9_]+)/;
        const match = triggerPattern.exec(content);
        const objectName = match?.[1] ?? '';
        return {
            apiName: name,
            type: 'ApexTrigger',
            path: filePath,
            category: objectName ? `trigger-on-${objectName}` : 'trigger',
            dependencies: [],
        };
    },
    extractDependencies(_entry, content) {
        const deps = new Set();
        // Object the trigger fires on
        const triggerPattern = /trigger\s+\w+\s+on\s+([A-Za-z][A-Za-z0-9_]+)/;
        const triggerMatch = triggerPattern.exec(content);
        if (triggerMatch) {
            deps.add(triggerMatch[1]);
        }
        // Handler class instantiation: new HandlerClassName(
        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);
        }
        // Static method calls: ClassName.methodName(
        const staticPattern = /([A-Z][A-Za-z0-9_]+)\.\w+\s*\(/g;
        match = staticPattern.exec(content);
        while (match) {
            deps.add(match[1]);
            match = staticPattern.exec(content);
        }
        // Remove Apex built-ins
        const builtins = new Set(['Trigger', 'System', 'Database', 'Test']);
        for (const b of builtins)
            deps.delete(b);
        return [...deps];
    },
};
//# sourceMappingURL=trigger-parser.js.map