import { Context, Effect, Layer } from "effect" import type { Mutable } from "effect/Types" import * as QueryErrorHandler from "./QueryErrorHandler.js" export interface QueryClient { readonly errorHandler: QueryErrorHandler.QueryErrorHandler } const id = "@reffuse/extension-query/QueryClient" export type TagClassShape = Context.TagClassShape> export type GenericTagClass = Context.TagClass< TagClassShape, typeof id, QueryClient > export const makeGenericTagClass = (): GenericTagClass => Context.Tag(id)() export interface ServiceProps { readonly ErrorHandler?: Context.Tag> } export interface ServiceResult extends Context.TagClass< Self, typeof id, QueryClient > { readonly Live: Layer.Layer< Self | (EH extends QueryErrorHandler.DefaultQueryErrorHandler ? EH : never), never, EH extends QueryErrorHandler.DefaultQueryErrorHandler ? never : EH > } export const Service = () => ( < EH = QueryErrorHandler.DefaultQueryErrorHandler, FallbackA = QueryErrorHandler.Fallback>, HandledE = QueryErrorHandler.Error>, >( props?: ServiceProps ): ServiceResult => { const TagClass = Context.Tag(id)() as ServiceResult (TagClass as Mutable).Live = Layer.effect(TagClass, Effect.Do.pipe( Effect.bind("errorHandler", () => (props?.ErrorHandler ?? QueryErrorHandler.DefaultQueryErrorHandler) as Effect.Effect< QueryErrorHandler.QueryErrorHandler, never, EH extends QueryErrorHandler.DefaultQueryErrorHandler ? never : EH > ) )).pipe( Layer.provideMerge((props?.ErrorHandler ? Layer.empty : QueryErrorHandler.DefaultQueryErrorHandler.Live ) as Layer.Layer) ) return TagClass } )