import * as Chunk from "effect-app/Chunk" import * as Effect from "effect-app/Effect" import * as Equal from "effect/Equal" import { NotFoundError } from "../client/errors.js" function getFirstBy( a: Iterable, idKey: keyof A, id: A[typeof idKey], type: Type ) { return Effect .fromOption( Chunk.fromIterable(a).pipe(Chunk.findFirst((_) => Equal.equals(_[idKey], id))) ) .pipe(Effect.mapError(() => new NotFoundError({ type, id }))) } export function makeGetFirstBy() { return (idKey: Id, type: Type) => ( a: Iterable, id: A[Id] ) => getFirstBy(a, idKey, id, type) } export const makeGetFirstById = () => (type: Type) => makeGetFirstBy()("id", type) export function getFirstById( a: Iterable, id: A["id"], type: Type ) { return getFirstBy(a, "id", id, type) }