import type { UnionToIntersection } from "@effect-ts/core/Utils" import type { Annotation } from "../_schema.js" import * as MO from "../_schema.js" import type { Schema } from "../_schema/schema.js" import * as Arbitrary from "../Arbitrary.js" import * as Constructor from "../Constructor.js" import * as Encoder from "../Encoder.js" import * as Guard from "../Guard.js" import * as Parser from "../Parser.js" import { unsafe } from "./condemn.js" export interface SchemaDefaultSchema< ParserInput, ParsedShape, ConstructorInput, Encoded, Api > extends Schema { (_: ConstructorInput): ParsedShape readonly Parser: Parser.Parser readonly Constructor: Constructor.Constructor readonly Encoder: Encoder.Encoder readonly Guard: Guard.Guard readonly Arbitrary: Arbitrary.Gen readonly annotate: ( identifier: Annotation, meta: Meta ) => DefaultSchema } export type DefaultSchema = & SchemaDefaultSchema & CarryFromApi const carryOver = ["matchW", "matchS", "props"] as const type CarryOverFromApi = typeof carryOver[number] type CarryFromApi = UnionToIntersection< { [k in keyof Api]: k extends CarryOverFromApi ? { [h in k]: Api[h] } : never }[keyof Api] > export function withDefaults( self: Schema ): DefaultSchema { const of_ = Constructor.for(self) >= unsafe function schemed(_: ConstructorInput) { return of_(_) } Object.defineProperty(schemed, MO.SchemaContinuationSymbol, { value: self }) Object.defineProperty(schemed, "Api", { get() { return self.Api } }) Object.defineProperty(schemed, ">>>", { value: self[">>>"] }) Object.defineProperty(schemed, "Parser", { value: Parser.for(self) }) Object.defineProperty(schemed, "Constructor", { value: Constructor.for(self) }) Object.defineProperty(schemed, "Encoder", { value: Encoder.for(self) }) Object.defineProperty(schemed, "Guard", { value: Guard.for(self) }) Object.defineProperty(schemed, "Arbitrary", { value: Arbitrary.for(self) }) Object.defineProperty(schemed, "annotate", { value: (annotation: Annotation, meta: Meta) => withDefaults(self.annotate(annotation, meta)) }) for (const k of carryOver) { Object.defineProperty(schemed, k, { get() { return self.Api[k] } }) } // @ts-expect-error return schemed }