import { getRef, parseReturnShape, refItemToShape } from "../refs.js";
function isQuery(propOrQuery) {
    return 'shape' in propOrQuery && 'resolver' in propOrQuery;
}
function getReturnShapeHelper(projectSchema, propOrQuery) {
    let shape;
    if (isQuery(propOrQuery)) {
        const { shapeName } = parseReturnShape(projectSchema, propOrQuery.shape);
        shape = projectSchema.shapes[shapeName];
    }
    else {
        const ref = getRef(projectSchema, propOrQuery);
        if (ref) {
            shape = refItemToShape(projectSchema, ref);
        }
    }
    return shape;
}
export function getReturnShapeRef(context, propOrQuery) {
    return isQuery(propOrQuery) ? parseReturnShape(context, propOrQuery.shape).ref : getRef(context, propOrQuery);
}
const cache = new WeakMap();
export function getReturnShape(projectSchema, propOrQuery) {
    let result = cache.get(propOrQuery);
    if (result === undefined) {
        result = getReturnShapeHelper(projectSchema, propOrQuery);
        cache.set(propOrQuery, result ?? null);
    }
    return result ?? undefined;
}
