"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadIntrinsics = loadIntrinsics;
const typescript_1 = __importDefault(require("typescript"));
const path_1 = __importDefault(require("path"));
// ---------------------------------------------------------------------------
// Resolution
// ---------------------------------------------------------------------------
const STEPS_CLASS_NAME = 'Steps';
/**
 * Resolve the path to the runtime index.ts file within the package.
 * Uses the package-relative path rather than __dirname.
 */
function resolveRuntimePath() {
    // The runtime/index.ts lives relative to the package root.
    // From packages/core/src/compiler/discovery/ → ../../runtime/index.ts
    return path_1.default.resolve(__dirname, '../../runtime/index.ts');
}
/**
 * Load all Steps class static method symbols from the runtime module.
 *
 * This scans packages/core/src/runtime/index.ts for the `Steps` class
 * and collects every static method symbol. The compiler uses these symbols
 * to recognise intrinsic calls.
 */
function loadIntrinsics(context, runtimePathOverride) {
    const runtimePath = runtimePathOverride ?? resolveRuntimePath();
    const sourceFile = context.program.getSourceFile(runtimePath);
    if (!sourceFile) {
        context.logger.warn(`Could not load runtime source file: ${runtimePath}`);
        return null;
    }
    // Find the Steps class declaration
    let stepsClass;
    for (const stmt of sourceFile.statements) {
        if (typescript_1.default.isClassDeclaration(stmt) && stmt.name?.escapedText === STEPS_CLASS_NAME) {
            stepsClass = stmt;
            break;
        }
    }
    if (!stepsClass) {
        context.logger.warn(`Could not find ${STEPS_CLASS_NAME} class in runtime`);
        return null;
    }
    // Collect all static method symbols
    let createFunctionSymbol;
    const methods = new Map();
    for (const member of stepsClass.members) {
        if (!typescript_1.default.isMethodDeclaration(member))
            continue;
        // Only static members
        const isStatic = member.modifiers?.some(m => m.kind === typescript_1.default.SyntaxKind.StaticKeyword);
        if (!isStatic)
            continue;
        const symbol = context.checker.getSymbolAtLocation(member.name);
        if (!symbol)
            continue;
        const name = symbol.getName();
        methods.set(name, symbol);
        if (name === 'createFunction') {
            createFunctionSymbol = symbol;
        }
    }
    if (!createFunctionSymbol) {
        context.logger.warn('Could not find Steps.createFunction() symbol');
        return null;
    }
    context.logger.debug(`Loaded ${methods.size} intrinsic methods: ${[...methods.keys()].join(', ')}`);
    return {
        createFunction: createFunctionSymbol,
        methods,
    };
}
//# sourceMappingURL=intrinsics.js.map