/* eslint-disable @typescript-eslint/no-explicit-any */ import { flow, pipe } from "@effect-ts-app/core/Function" import * as MO from "./_schema.js" import { Constructor, Parser, These as Th } from "./_schema.js" export function include>(props: Props) { return >( fnc: (props: Props) => NewProps ) => include_(props, fnc) } export function include_< Props extends Record, NewProps extends Record >(props: Props, fnc: (props: Props) => NewProps) { return fnc(props) } export function onParseOrConstruct< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >(mod: (i: ParsedShape) => Th.These) { return (self: MO.Schema) => onParseOrConstruct_(self, mod) } export function onParseOrConstruct_< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >( self: MO.Schema, mod: (i: ParsedShape) => Th.These ) { return pipe(self, onParse(mod), onConstruct(mod)) } export function onParse< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >(mod: (i: ParsedShape) => Th.These) { return (self: MO.Schema) => onParse_(self, mod) } export function onParse_< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >( self: MO.Schema, mod: (i: ParsedShape) => Th.These ) { return pipe(self, MO.parser(flow(Parser.for(self), Th.chain(mod)))) } export function onConstruct< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >(mod: (i: ParsedShape) => Th.These) { return (self: MO.Schema) => onConstruct_(self, mod) } export function onConstruct_< ParserInput, ParsedShape, ConstructorInput, Encoded, Api, Errors extends MO.AnyError >( self: MO.Schema, mod: (i: ParsedShape) => Th.These ) { return pipe(self, MO.constructor(flow(Constructor.for(self), Th.chain(mod)))) } export type DomainError = MO.RequiredKeyE export function domainResponse(errors: DomainError[], success: () => A) { if (errors.length) { return Th.fail(domainError(errors)) } return Th.succeed(success()) } export function domainResponse2(errors: MO.AnyError[], success: () => A) { if (errors.length) { return Th.fail(MO.compositionE(Chunk.fromIterable(errors))) } return Th.succeed(success()) } export function domainError(errors: DomainError[]) { return MO.compositionE(Chunk.fromIterable([MO.nextE(MO.structE(Chunk.fromIterable(errors)))])) } export function domainE(key: string, message: string) { // TODO return MO.requiredKeyE(key, domainEE(message)) } export function domainEE(message: string) { return MO.leafE(MO.parseStringE(message)) }