/* eslint-disable @typescript-eslint/no-explicit-any */ import * as D from "@effect-ts/core/Collections/Immutable/Dictionary" import type { None, Some } from "@effect-ts/core/Option" import type { ComputeFlat, UnionToIntersection } from "@effect-ts/core/Utils" import { array, prop, props } from "./_schema.js" import type * as MO from "./_schema.js" import { positiveInt } from "./custom.js" import { optProp } from "./ext.js" type AdaptSchema = { [K in Key]: Props[K] } // TODO: adapt error types too; low prio const adaptedSchema = (properties: Props) => (keys: readonly Key[]): AdaptSchema => D.filterWithIndex_(properties, key => keys.includes(key as Key)) as any // TODO: keep existing fields export const adaptRes = (properties: Props) => { const adapt = adaptedSchema(properties) return (keys: readonly Key[]) => props({ items: prop(array(props(adapt(keys)))), // TODO: hide count when not asked for $count. and demand non-opt count, when asked. count: optProp(positiveInt) }) } export type Adapted< Props extends MO.PropertyRecord, Key extends keyof Props > = /* copy pasted from return type of function */ MO.SchemaProperties<{ items: MO.Property< MO.SchemaDefaultSchema< unknown, readonly ComputeFlat< UnionToIntersection< { [k in keyof AdaptSchema]: AdaptSchema< Props, Key >[k] extends MO.AnyProperty ? AdaptSchema[k]["_optional"] extends "optional" ? { readonly [h in k]?: | MO.ParsedShapeOf[k]["_schema"]> | undefined } : { readonly [h in k]: MO.ParsedShapeOf< AdaptSchema[k]["_schema"] > } : never }[Key] > >[], readonly ComputeFlat< UnionToIntersection< { [k in keyof AdaptSchema]: AdaptSchema< Props, Key >[k] extends MO.AnyProperty ? AdaptSchema[k]["_optional"] extends "optional" ? { readonly [h in k]?: | MO.ParsedShapeOf[k]["_schema"]> | undefined } : { readonly [h in k]: MO.ParsedShapeOf< AdaptSchema[k]["_schema"] > } : never }[Key] > >[], readonly ComputeFlat< UnionToIntersection< { [k in keyof AdaptSchema]: AdaptSchema< Props, Key >[k] extends MO.AnyProperty ? AdaptSchema[k]["_optional"] extends "optional" ? { readonly [ h in AdaptSchema[k]["_as"] extends Some ? AdaptSchema[k]["_as"]["value"] : k ]?: | MO.EncodedOf[k]["_schema"]> | undefined } : { readonly [ h in AdaptSchema[k]["_as"] extends Some ? AdaptSchema[k]["_as"]["value"] : k ]: MO.EncodedOf[k]["_schema"]> } : never }[Key] > >[], // eslint-disable-next-line @typescript-eslint/ban-types {} >, "required", None, None > }>