import { GroqBuilder } from "../groq-builder"; import { Empty, IsAny, LiteralUnion, Simplify, SimplifyDeep, StringKeys, TypeMismatchError, UndefinedToNull, ValueOf } from "../types/utils"; import { FragmentInputTypeTag, IGroqBuilder, Parser, ParserWithWidenedInput } from "../types/public-types"; import { Path, PathEntries, PathValue } from "../types/path-types"; import { DeepRequired } from "../types/deep-required"; import { QueryConfig } from "../types/schema-types"; import { ConditionalKey, ExtractConditionalProjectionTypes } from "./conditional-types"; export type ProjectionKey = IsAny extends true ? string : ProjectionKeyImpl>>>; type ProjectionKeyImpl = ValueOf<{ [Key in keyof Entries]: Entries[Key] extends Array ? `${StringKeys}[]` | Key : Key; }>; export type ProjectionKeyValue = UndefinedToNull>>>; export type ProjectionMap = { [P in LiteralUnion]?: ProjectionFieldConfig : any>; } & { "..."?: true | Parser; }; export type ProjectionMapOrCallback = ProjectionMap | ((q: GroqBuilder) => ProjectionMap); export type ProjectionFieldConfig = true | ProjectionKey | ParserWithWidenedInput | readonly [ProjectionKey, ParserWithWidenedInput] | IGroqBuilder; export type ExtractProjectionResult = (TProjectionMap extends { "...": true; } ? TResultItem : Empty) & (TProjectionMap extends { "...": Parser; } ? TOutput : Empty) & ExtractConditionalProjectionTypes & ExtractProjectionResultFields>>; type ExtractProjectionResultFields = { [P in keyof TProjectionMap]: TProjectionMap[P] extends IGroqBuilder ? TValue : TProjectionMap[P] extends boolean ? P extends keyof TResultItem ? UndefinedToNull : TypeMismatchError<{ error: `⛔️ 'true' can only be used for known properties ⛔️`; expected: keyof TResultItem; actual: P; }> : TProjectionMap[P] extends string ? TProjectionMap[P] extends ProjectionKey ? ProjectionKeyValue : TypeMismatchError<{ error: `⛔️ Naked projections must be known properties ⛔️`; expected: SimplifyDeep>; actual: TProjectionMap[P]; }> : TProjectionMap[P] extends readonly [infer TKey, infer TParser] ? TKey extends ProjectionKey ? TParser extends Parser ? ValidateParserInput, TParserInput, TParserOutput> : TypeMismatchError<{ error: `⛔️ Naked projections must be known properties ⛔️`; expected: SimplifyDeep>; actual: TKey; }> : TypeMismatchError<{ error: `⛔️ Naked projections must be known properties ⛔️`; expected: SimplifyDeep>; actual: TKey; }> : TProjectionMap[P] extends Parser ? P extends keyof TResultItem ? ValidateParserInput, TParserInput, TParserOutput> : TypeMismatchError<{ error: `⛔️ Parser can only be used with known properties ⛔️`; expected: keyof TResultItem; actual: P; }> : never; }; export type ValidateParserInput = TIncomingValue extends TParserInput ? TParserOutput : IsAny extends true ? TParserOutput : TypeMismatchError<{ error: `⛔️ Parser expects a different input type ⛔️`; expected: TParserInput; actual: TIncomingValue; }>; export {};