import { dirname } from 'node:path';
import { validateWorkflowCallRulesAgainstChildReturns } from './workflowCallContracts.js';
import { getWorkflowSourcePath } from './workflowSourceMetadata.js';
import { getWorkflowTrustInfo } from './workflowTrustSource.js';
function getWorkflowCallValidationKey(workflow, lookupCwd) {
    const sourcePath = getWorkflowSourcePath(workflow);
    if (sourcePath) {
        return sourcePath;
    }
    return `${lookupCwd}:${workflow.name}`;
}
function validateWorkflowCallContractsRecursive(workflow, projectCwd, lookupCwd, visited, deps, allowPathBasedCalls) {
    const validationKey = getWorkflowCallValidationKey(workflow, lookupCwd);
    if (visited.has(validationKey)) {
        return;
    }
    visited.add(validationKey);
    const parentSourcePath = getWorkflowSourcePath(workflow);
    const basePath = parentSourcePath ? dirname(parentSourcePath) : lookupCwd;
    const parentTrustInfo = getWorkflowTrustInfo(workflow, projectCwd);
    for (const step of workflow.steps) {
        if (step.kind !== 'workflow_call') {
            continue;
        }
        if (!allowPathBasedCalls && deps.isWorkflowPath(step.call)) {
            continue;
        }
        const childWorkflow = deps.loadWorkflowByIdentifierForWorkflowCall(step.call, projectCwd, {
            basePath,
            lookupCwd,
            callableArgs: step.args,
            parentTrustInfo,
            skipWorkflowCallContractValidation: true,
        });
        if (!childWorkflow) {
            continue;
        }
        validateWorkflowCallContractsRecursive(childWorkflow, projectCwd, lookupCwd, visited, deps, allowPathBasedCalls);
        validateWorkflowCallRulesAgainstChildReturns(step, childWorkflow);
    }
}
export function validateWorkflowCallContracts(workflow, projectCwd, deps, options) {
    validateWorkflowCallContractsRecursive(workflow, projectCwd, options?.lookupCwd ?? projectCwd, new Set(), deps, options?.allowPathBasedCalls !== false);
}
//# sourceMappingURL=workflowCallContractValidator.js.map