{"version":3,"file":"authoring-entity-types-DGM8uYBm.mjs","names":[],"sources":["../../../1-framework/2-authoring/contract/dist/index.mjs","../src/core/authoring-entity-types.ts"],"sourcesContent":["import { instantiateAuthoringEntityType } from \"@prisma-next/framework-components/authoring\";\nimport { blindCast } from \"@prisma-next/utils/casts\";\n//#region src/capability-registry.ts\n/**\n* Deep-merges any number of capability matrices into a single matrix.\n*\n* Merge is purely structural — namespaces and capability flags from later\n* sources overlay earlier ones. Undefined entries are skipped so callers\n* can pass optional sources without pre-filtering.\n*\n* The helper is target-agnostic: it contains no SQL or family-specific\n* knowledge. Higher layers contribute their own capability matrices (for\n* example, a target pack, an extension pack, or the contract author's\n* own `capabilities` block) and call this helper to fold them together.\n*/\nfunction mergeCapabilityMatrices(...sources) {\n\tconst result = {};\n\tfor (const source of sources) {\n\t\tif (source === void 0) continue;\n\t\tfor (const [namespace, capabilities] of Object.entries(source)) {\n\t\t\tconst existing = result[namespace];\n\t\t\tresult[namespace] = existing ? {\n\t\t\t\t...existing,\n\t\t\t\t...capabilities\n\t\t\t} : { ...capabilities };\n\t\t}\n\t}\n\treturn result;\n}\n//#endregion\n//#region src/composed-helpers-scaffolding.ts\n/**\n* Walks an entity-type namespace (after cross-pack merge) and produces the\n* runtime callable surface mirroring its tree shape. Each leaf\n* descriptor becomes a function `(input) => factory(input, ctx)`;\n* nested namespace objects recurse.\n*/\nfunction createEntityHelpersFromNamespace(namespace, options, path = []) {\n\tconst result = {};\n\tfor (const [key, value] of Object.entries(namespace)) {\n\t\tconst currentPath = [...path, key];\n\t\tif (isLeafEntityDescriptor(value)) {\n\t\t\tresult[key] = createEntityHelper(currentPath.join(\".\"), value, options);\n\t\t\tcontinue;\n\t\t}\n\t\tif (typeof value === \"object\" && value !== null && !Array.isArray(value)) result[key] = createEntityHelpersFromNamespace(value, options, currentPath);\n\t}\n\treturn result;\n}\nfunction isLeafEntityDescriptor(value) {\n\tif (typeof value !== \"object\" || value === null || value.kind !== \"entity\") return false;\n\tconst discriminator = value.discriminator;\n\treturn typeof discriminator === \"string\" && discriminator.length > 0;\n}\nfunction createEntityHelper(helperPath, descriptor, options) {\n\treturn (...args) => instantiateAuthoringEntityType(helperPath, descriptor, args, options.ctx);\n}\n//#endregion\n//#region src/enum-type.ts\nfunction member(name, value) {\n\treturn {\n\t\tname,\n\t\tvalue: blindCast(value === void 0 ? name : value)\n\t};\n}\nconst ENUM_TYPE_HANDLE_BRAND = \"__prismaNextEnumTypeHandle__\";\nfunction enumType(name, codec, ...members) {\n\tif (members.length === 0) throw new Error(`enumType(\"${name}\"): must have at least one member.`);\n\tconst seenNames = /* @__PURE__ */ new Set();\n\tconst seenValues = /* @__PURE__ */ new Set();\n\tfor (const m of members) {\n\t\tif (seenNames.has(m.name)) throw new Error(`enumType(\"${name}\"): duplicate member name \"${m.name}\". Member names must be unique.`);\n\t\tseenNames.add(m.name);\n\t\tconst loweredValue = String(m.value);\n\t\tif (seenValues.has(loweredValue)) throw new Error(`enumType(\"${name}\"): duplicate member value \"${loweredValue}\". Member values must be unique.`);\n\t\tseenValues.add(loweredValue);\n\t}\n\tconst values = Object.freeze(members.map((m) => m.value));\n\tconst names = Object.freeze(members.map((m) => m.name));\n\tconst enumMembers = Object.freeze(members.map((m) => ({\n\t\tname: m.name,\n\t\tvalue: m.value\n\t})));\n\tconst membersAccessor = Object.freeze(Object.fromEntries(members.map((m) => [m.name, m.value])));\n\tconst valueSet = new Set(values);\n\tconst valueToName = new Map(members.map((m) => [m.value, m.name]));\n\tconst valueToOrdinal = new Map(values.map((v, i) => [v, i]));\n\treturn {\n\t\t[ENUM_TYPE_HANDLE_BRAND]: true,\n\t\tenumName: name,\n\t\tcodecId: codec.codecId,\n\t\tnativeType: codec.nativeType,\n\t\tenumMembers,\n\t\tvalues,\n\t\tnames,\n\t\tmembers: membersAccessor,\n\t\thas: (v) => valueSet.has(v),\n\t\tnameOf: (v) => valueToName.get(v),\n\t\tordinalOf: (v) => valueToOrdinal.get(v) ?? -1\n\t};\n}\n/**\n* Bind `enumType` to a target's codec typemap. The returned function is the\n* same runtime `enumType`, retyped so member values are constrained to the\n* codec's input type. Target packages call this with their pack's\n* `ExtractCodecTypesFromPack<Pack>` to expose a codec-aware `enumType`.\n*/\nfunction bindEnumType() {\n\treturn enumType;\n}\n/**\n* Returns true when the value is an `EnumTypeHandle` produced by\n* `enumType()`. Used in the lowering pipeline to detect enum handles\n* in field state without importing the brand key at every call site.\n*/\nfunction isEnumTypeHandle(value) {\n\treturn typeof value === \"object\" && value !== null && Reflect.get(value, \"__prismaNextEnumTypeHandle__\") === true;\n}\n//#endregion\nexport { ENUM_TYPE_HANDLE_BRAND, bindEnumType, createEntityHelpersFromNamespace, enumType, isEnumTypeHandle, member, mergeCapabilityMatrices };\n\n//# sourceMappingURL=index.mjs.map","import type { JsonValue } from '@prisma-next/contract/types';\nimport {\n  type AuthoringEntityContext,\n  type AuthoringEntityTypeDescriptor,\n  type AuthoringEntityTypeNamespace,\n  type AuthoringPslBlockDescriptorNamespace,\n  type PslExtensionBlock,\n  resolveEnumCodecId,\n} from '@prisma-next/framework-components/authoring';\nimport { type EnumTypeHandle, enumType } from '@prisma-next/mongo-contract-ts/contract-builder';\nimport { blindCast } from '@prisma-next/utils/casts';\n\nexport const mongoFamilyEnumEntityDescriptor = {\n  kind: 'entity' as const,\n  discriminator: 'enum',\n  output: {\n    factory: (\n      block: PslExtensionBlock,\n      ctx: AuthoringEntityContext,\n    ): EnumTypeHandle | undefined => {\n      const sourceId = ctx.sourceId ?? 'unknown';\n      const diagnostics = ctx.diagnostics;\n\n      const resolved = resolveEnumCodecId(block, ctx);\n      if (resolved === undefined) {\n        return undefined;\n      }\n      const { codecId, codecSpan } = resolved;\n\n      const nativeType = ctx.codecLookup?.targetTypesFor(codecId)?.[0];\n      if (nativeType === undefined) {\n        diagnostics?.push({\n          code: 'PSL_EXTENSION_INVALID_VALUE',\n          message: `enum \"${block.name}\" @@type references unknown codec \"${codecId}\"`,\n          sourceId,\n          span: codecSpan,\n        });\n        return undefined;\n      }\n\n      const codec = ctx.codecLookup?.get(codecId);\n      if (codec === undefined) {\n        diagnostics?.push({\n          code: 'PSL_EXTENSION_INVALID_VALUE',\n          message: `enum \"${block.name}\" @@type codec \"${codecId}\" resolves in targetTypesFor but is absent from codecLookup.get`,\n          sourceId,\n          span: codecSpan,\n        });\n        return undefined;\n      }\n\n      const seenValues = new Set<string>();\n      const members: { name: string; value: unknown }[] = [];\n      let memberError = false;\n\n      for (const [memberName, paramValue] of Object.entries(block.parameters)) {\n        let value: unknown;\n        if (paramValue.kind === 'bare') {\n          try {\n            value = codec.decodeJson(memberName);\n          } catch {\n            diagnostics?.push({\n              code: 'PSL_ENUM_BARE_MEMBER_NON_STRING_CODEC',\n              message: `enum \"${block.name}\" member \"${memberName}\" has no value and codec \"${codecId}\" does not accept a bare name as input`,\n              sourceId,\n              span: paramValue.span,\n            });\n            memberError = true;\n            continue;\n          }\n        } else if (paramValue.kind === 'value') {\n          let jsonValue: unknown;\n          try {\n            jsonValue = JSON.parse(paramValue.raw);\n          } catch {\n            diagnostics?.push({\n              code: 'PSL_EXTENSION_INVALID_VALUE',\n              message: `enum \"${block.name}\" member \"${memberName}\" value \"${paramValue.raw}\" is not valid JSON`,\n              sourceId,\n              span: paramValue.span,\n            });\n            memberError = true;\n            continue;\n          }\n          try {\n            value = codec.decodeJson(\n              blindCast<JsonValue, 'JSON.parse returns a JsonValue-compatible value'>(jsonValue),\n            );\n          } catch (err) {\n            const reason = err instanceof Error ? err.message : String(err);\n            diagnostics?.push({\n              code: 'PSL_EXTENSION_INVALID_VALUE',\n              message: `enum \"${block.name}\" member \"${memberName}\" was rejected by codec \"${codecId}\": ${reason}`,\n              sourceId,\n              span: paramValue.span,\n            });\n            memberError = true;\n            continue;\n          }\n        } else {\n          continue;\n        }\n\n        const valueKey = String(value);\n        if (seenValues.has(valueKey)) {\n          diagnostics?.push({\n            code: 'PSL_ENUM_DUPLICATE_MEMBER_VALUE',\n            message: `enum \"${block.name}\": duplicate member value \"${valueKey}\"`,\n            sourceId,\n            span: paramValue.span,\n          });\n          memberError = true;\n          continue;\n        }\n        seenValues.add(valueKey);\n        members.push({ name: memberName, value });\n      }\n\n      if (memberError) return undefined;\n\n      if (members.length === 0) {\n        diagnostics?.push({\n          code: 'PSL_ENUM_MISSING_TYPE',\n          message: `enum \"${block.name}\" must have at least one member`,\n          sourceId,\n          span: block.span,\n        });\n        return undefined;\n      }\n\n      return enumType(\n        block.name,\n        { codecId, nativeType },\n        ...members.map((m) => ({ name: m.name, value: m.value })),\n      );\n    },\n  },\n} satisfies AuthoringEntityTypeDescriptor;\n\nexport const mongoFamilyEntityTypes: AuthoringEntityTypeNamespace = {\n  enum: mongoFamilyEnumEntityDescriptor,\n};\n\nexport const mongoFamilyPslBlockDescriptors = {\n  enum: {\n    kind: 'pslBlock',\n    keyword: 'enum',\n    discriminator: 'enum',\n    name: { required: true },\n    parameters: {},\n    variadicParameters: true,\n  },\n} as const satisfies AuthoringPslBlockDescriptorNamespace;\n"],"mappings":";;;AAiEA,MAAM,yBAAyB;AAC/B,SAAS,SAAS,MAAM,OAAO,GAAG,SAAS;CAC1C,IAAI,QAAQ,WAAW,GAAG,MAAM,IAAI,MAAM,aAAa,KAAK,mCAAmC;CAC/F,MAAM,4BAA4B,IAAI,IAAI;CAC1C,MAAM,6BAA6B,IAAI,IAAI;CAC3C,KAAK,MAAM,KAAK,SAAS;EACxB,IAAI,UAAU,IAAI,EAAE,IAAI,GAAG,MAAM,IAAI,MAAM,aAAa,KAAK,6BAA6B,EAAE,KAAK,gCAAgC;EACjI,UAAU,IAAI,EAAE,IAAI;EACpB,MAAM,eAAe,OAAO,EAAE,KAAK;EACnC,IAAI,WAAW,IAAI,YAAY,GAAG,MAAM,IAAI,MAAM,aAAa,KAAK,8BAA8B,aAAa,iCAAiC;EAChJ,WAAW,IAAI,YAAY;CAC5B;CACA,MAAM,SAAS,OAAO,OAAO,QAAQ,KAAK,MAAM,EAAE,KAAK,CAAC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