import { normalizedUnionType } from "../types/type-ops.js";
import { collectNarrowingCandidateLeaves } from "./narrowing-candidates.js";
const isArrayLikeCandidate = (type) => type.kind === "arrayType" || type.kind === "tupleType";
const UNKNOWN_ARRAY_TYPE = {
    kind: "arrayType",
    elementType: { kind: "unknownType" },
};
export const narrowTypeByArrayShape = (collector, currentType, wantArray) => {
    if (!currentType)
        return undefined;
    if (wantArray) {
        if (currentType.kind === "unknownType" || currentType.kind === "anyType") {
            return UNKNOWN_ARRAY_TYPE;
        }
        if (currentType.kind === "referenceType" && currentType.name === "object") {
            return UNKNOWN_ARRAY_TYPE;
        }
    }
    const candidates = collectNarrowingCandidateLeaves(collector, currentType);
    const kept = candidates.filter((member) => {
        if (!member)
            return false;
        return wantArray
            ? isArrayLikeCandidate(member)
            : !isArrayLikeCandidate(member);
    });
    if (kept.length === 0)
        return undefined;
    if (kept.length === 1)
        return kept[0];
    return normalizedUnionType(kept);
};
//# sourceMappingURL=array-type-guards.js.map