import { buildResolvedRestParameter, expandParameterTypesForArguments, } from "../../../type-system/type-system-call-resolution.js";
const isArityCompatibleForSemanticParameters = (parameters, argumentCount) => {
    let requiredCount = 0;
    let hasRest = false;
    for (const parameter of parameters) {
        if (parameter.isRest) {
            hasRest = true;
            continue;
        }
        if (!parameter.isOptional && parameter.initializer === undefined) {
            requiredCount += 1;
        }
    }
    if (argumentCount < requiredCount) {
        return false;
    }
    if (!hasRest && argumentCount > parameters.length) {
        return false;
    }
    return true;
};
export const getBoundGlobalCallParameterTypes = (callee, argumentCount, ctx) => {
    if (callee.kind !== "identifier" ||
        !callee.resolvedAssembly ||
        !callee.resolvedClrType ||
        !callee.csharpName) {
        return undefined;
    }
    const memberName = callee.csharpName.split(".").pop();
    if (!memberName) {
        return undefined;
    }
    const overloads = ctx.bindings
        .getClrMemberOverloads(callee.resolvedAssembly, callee.resolvedClrType, memberName)
        ?.filter((overload) => overload.semanticSignature !== undefined);
    if (!overloads || overloads.length === 0) {
        return undefined;
    }
    const arityCompatible = overloads.filter((overload) => isArityCompatibleForSemanticParameters(overload.semanticSignature.parameters, argumentCount));
    if (arityCompatible.length !== 1) {
        return undefined;
    }
    const [selected] = arityCompatible;
    if (!selected) {
        return undefined;
    }
    const parameterTypes = expandParameterTypesForArguments(selected.semanticSignature.parameters, selected.semanticSignature.parameters.map((parameter) => parameter.type), argumentCount);
    return {
        parameterTypes,
        returnType: selected.semanticSignature.returnType,
        restParameter: buildResolvedRestParameter(selected.semanticSignature.parameters.map((parameter) => ({
            isRest: parameter.isRest,
        })), parameterTypes),
    };
};
//# sourceMappingURL=bound-global-call-parameters.js.map