// tracing: off import { pipe } from "@effect-ts/core/Function" import * as S from "../custom.js" import { leafE, unknownArrayE } from "../custom.js" import * as Arbitrary from "../custom/Arbitrary.js" import * as Encoder from "../custom/Encoder.js" import * as Guard from "../custom/Guard.js" import * as Th from "../custom/These.js" import { minLengthIdentifier } from "./length.js" export function nonEmptyArray( self: S.Schema ): S.DefaultSchema< unknown, NonEmptyReadonlyArray, NonEmptyReadonlyArray, readonly Encoded[], { self: Api } > { const guardSelf = Guard.for(self) const arbitrarySelf = Arbitrary.for(self) const encodeSelf = Encoder.for(self) const fromChunk = pipe( S.identity( (u): u is NonEmptyReadonlyArray => Array.isArray(u) && u.length > 0 && u.every(guardSelf) ), S.parser((u: Chunk) => { const ar = u.toArray const nar = ar.toNonEmpty return nar.match(() => Th.fail(leafE(unknownArrayE(u)) as any), Th.succeed) }), S.encoder((u): Chunk => Chunk.fromIterable(u)), S.arbitrary( _ => _.array(arbitrarySelf(_), { minLength: 1 }) as any as Arbitrary.Arbitrary< NonEmptyReadonlyArray > ) ) return pipe( S.chunk(self)[">>>"](fromChunk), S.mapParserError(_ => ((_ as any).errors as Chunk).unsafeHead.error), S.constructor((_: NonEmptyReadonlyArray) => Th.succeed(_)), S.encoder(u => u.map(encodeSelf)), S.mapApi(() => ({ self: self.Api })), S.withDefaults, S.annotate(minLengthIdentifier, { self, minLength: 1 }), S.annotate(S.arrayIdentifier, { self }) ) }