{"version":3,"file":"mongrule.mjs","names":[],"sources":["../../../src/types/mongrule.ts"],"sourcesContent":["import type { SavedGroupsPayload, WidenPrimitives } from \"./growthbook\";\n\ntype OrCondition = {\n  $or: ConditionInterface[];\n};\ntype NorCondition = {\n  $nor: ConditionInterface[];\n};\ntype AndCondition = {\n  $and: ConditionInterface[];\n};\ntype NotCondition = {\n  $not: ConditionInterface;\n};\n/** Names one saved group. Unknown keys are ignored. */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type SavedGroupReference<A = any> = {\n  id: string;\n  /** Optional. Checks this attribute instead of the entry's own. */\n  attributeKey?: AttributePath<A>;\n};\n\n/**\n * A reference to one saved group. Top level like `$and`, not scoped to an\n * attribute. The plural authoring operator `$savedGroups` never reaches an\n * SDK, so it has no type here.\n */\nexport type SavedGroupCondition = {\n  $savedGroup: SavedGroupReference;\n};\nexport type Operator =\n  | \"$in\"\n  | \"$ini\"\n  | \"$inGroup\"\n  | \"$nin\"\n  | \"$nini\"\n  | \"$notInGroup\"\n  | \"$gt\"\n  | \"$gte\"\n  | \"$lt\"\n  | \"$lte\"\n  | \"$regex\"\n  | \"$regexi\"\n  | \"$ne\"\n  | \"$eq\"\n  | \"$size\"\n  | \"$elemMatch\"\n  | \"$all\"\n  | \"$alli\"\n  | \"$not\"\n  | \"$type\"\n  | \"$exists\"\n  | \"$vgt\"\n  | \"$vgte\"\n  | \"$vlt\"\n  | \"$vlte\"\n  | \"$vne\"\n  | \"$veq\";\nexport type VarType =\n  | \"string\"\n  | \"number\"\n  | \"boolean\"\n  | \"array\"\n  | \"object\"\n  | \"null\"\n  | \"undefined\";\nexport type ConditionTypeMismatch = {\n  path: string;\n  /** `$eq` for implicit equality */\n  operator: string;\n  /** Condition value */\n  expected: unknown;\n  /** Attribute value, `null` when missing */\n  actual: unknown;\n};\nexport type TypeMismatchCallback = (mismatch: ConditionTypeMismatch) => void;\nexport type EvalConditionOptions = {\n  /** Required for saved group operators to match anyone */\n  savedGroups?: SavedGroupsPayload;\n  onTypeMismatch?: TypeMismatchCallback;\n};\nexport type OperatorConditionValue = {\n  $in?: (string | number)[];\n  $ini?: (string | number)[];\n  $inGroup?: string;\n  $nin?: (string | number)[];\n  $nini?: (string | number)[];\n  $notInGroup?: string;\n  $gt?: number | string;\n  $gte?: number | string;\n  $lt?: number | string;\n  $lte?: number | string;\n  $regex?: string;\n  $regexi?: string;\n  $ne?: number | string;\n  $eq?: number | string;\n  $exists?: boolean;\n  $all?: ConditionValue[];\n  $alli?: ConditionValue[];\n  $size?: number | ConditionValue;\n  $type?: VarType;\n  $elemMatch?: ConditionInterface | OperatorConditionValue;\n  $not?: ConditionValue;\n};\n\nexport type ConditionValue =\n  | OperatorConditionValue\n  | string\n  | number\n  | boolean\n  // eslint-disable-next-line\n  | Array<any>\n  // eslint-disable-next-line\n  | Record<string, any>\n  | null;\n\nexport type OperatorCondition = {\n  [key: string]: ConditionValue;\n};\n\ntype UntypedCondition =\n  | OrCondition\n  | NorCondition\n  | AndCondition\n  | NotCondition\n  | SavedGroupCondition\n  | OperatorCondition;\n\n// With an attributes type, a condition written in code is checked against it:\n// unknown attributes, and operators or values that don't fit the attribute's\n// type, are errors. Without one (the default) any condition is accepted.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ConditionInterface<A = any> = A extends unknown\n  ? string extends keyof A\n    ? UntypedCondition\n    : {\n        $or?: ConditionInterface<A>[];\n        $nor?: ConditionInterface<A>[];\n        $and?: ConditionInterface<A>[];\n        $not?: ConditionInterface<A>;\n        $savedGroup?: SavedGroupReference<A>;\n      } & {\n        [P in AttributePath<A>]?: AttributeConditionValue<AttributeValue<A, P>>;\n      }\n  : never;\n\ntype ObjectOf<V> = V extends readonly unknown[]\n  ? never\n  : V extends object\n    ? V\n    : never;\ntype ElementOf<V> = V extends readonly (infer E)[] ? E : never;\ntype Scalar = string | number | boolean;\n// Adds the operators when some part of the attribute's type supports them\ntype OperatorsFor<T, Operators> = [T] extends [never] ? unknown : Operators;\n\n/** Attribute names, and dot paths into object attributes (`company.plan`) */\nexport type AttributePath<A, Depth extends 0[] = []> = A extends unknown\n  ? string extends keyof A\n    ? string\n    : {\n        [K in keyof A & string]:\n          | K\n          // Stops after 4 levels, so a recursive type can't loop forever\n          | (Depth[\"length\"] extends 3\n              ? never\n              : `${K}.${AttributePath<ObjectOf<A[K]>, [...Depth, 0]>}`);\n      }[keyof A & string]\n  : never;\n\n// The type of the attribute at a path. `unknown` leaves its conditions unchecked.\ntype AttributeValue<A, P extends string> = A extends unknown\n  ? P extends keyof A\n    ? A[P]\n    : P extends `${infer K}.${infer Rest}`\n      ? K extends keyof A\n        ? AttributeValue<ObjectOf<A[K]>, Rest>\n        : unknown\n      : unknown\n  : never;\n\n/** What an attribute of type V can be compared to: a value to equal, or operators */\nexport type AttributeConditionValue<V> = unknown extends V\n  ? ConditionValue\n  : V | null | AttributeOperators<V>;\n\n/** The operators that work on an attribute of type V */\nexport type AttributeOperators<V> = {\n  $exists?: boolean;\n  $type?: VarType;\n  $not?: AttributeConditionValue<V>;\n  // A missing attribute is null, so any attribute can be compared to null\n  $eq?: Extract<V, Scalar> | null;\n  $ne?: Extract<V, Scalar> | null;\n} &\n  // An array attribute matches when any of its elements is in the list\n  OperatorsFor<\n    Extract<V | ElementOf<V>, Scalar>,\n    {\n      $in?: (Extract<V | ElementOf<V>, Scalar> | null)[];\n      $nin?: (Extract<V | ElementOf<V>, Scalar> | null)[];\n      $inGroup?: string;\n      $notInGroup?: string;\n    }\n  > &\n  OperatorsFor<\n    Extract<V | ElementOf<V>, string>,\n    {\n      $ini?: (Extract<V | ElementOf<V>, string> | null)[];\n      $nini?: (Extract<V | ElementOf<V>, string> | null)[];\n    }\n  > &\n  OperatorsFor<\n    Extract<V, string | number>,\n    {\n      [K in\n        | \"$gt\"\n        | \"$gte\"\n        | \"$lt\"\n        | \"$lte\"\n        | \"$veq\"\n        | \"$vne\"\n        | \"$vgt\"\n        | \"$vgte\"\n        | \"$vlt\"\n        | \"$vlte\"]?: WidenPrimitives<Extract<V, string | number>>;\n    }\n  > &\n  OperatorsFor<Extract<V, string>, { $regex?: string; $regexi?: string }> &\n  OperatorsFor<\n    ElementOf<V>,\n    {\n      $size?: AttributeConditionValue<number>;\n      // Operators test each element, and a condition tests an element's fields\n      $elemMatch?:\n        | AttributeOperators<ElementOf<V>>\n        | ConditionInterface<ObjectOf<ElementOf<V>>>;\n      $all?: AttributeConditionValue<ElementOf<V>>[];\n      $alli?: AttributeConditionValue<ElementOf<V>>[];\n    }\n  >;\n\nexport type ParentConditionInterface = {\n  id: string;\n  condition: ConditionInterface;\n  gate?: boolean;\n};\n\n// eslint-disable-next-line\nexport type TestedObj = Record<string, any>;\n"],"mappings":"","ignoreList":[]}