import * as Chunk from "@effect/data/Chunk";
import * as Debug from "@effect/data/Debug";
import * as RA from "@effect/data/ReadonlyArray";
import * as Cause from "@effect/io/Cause";
import * as Effect from "@effect/io/Effect";
import * as core from "@effect/io/internal_effect_untraced/core";
import { forEachWithIndex } from "@effect/io/internal_effect_untraced/effect";
import { invokeWithInterrupt } from "@effect/io/internal_effect_untraced/fiberRuntime";
import { complete } from "@effect/io/internal_effect_untraced/request";
/** @internal */
export const make = /*#__PURE__*/Debug.untracedMethod(restore => runAll => new core.RequestResolverImpl(requests => restore(runAll)(requests.map(_ => _.map(_ => _.request)))));
/** @internal */
export const makeWithEntry = /*#__PURE__*/Debug.untracedMethod(restore => runAll => new core.RequestResolverImpl(requests => restore(runAll)(requests)));
/** @internal */
export const makeBatched = /*#__PURE__*/Debug.untracedMethod(restore => run => new core.RequestResolverImpl(requests => requests.length > 1 ? Effect.forEachDiscard(requests, block => invokeWithInterrupt(restore(run)(block.filter(_ => !_.state.completed).map(_ => _.request)), block)) : requests.length === 1 ? restore(run)(requests[0].filter(_ => !_.state.completed).map(_ => _.request)) : core.unit()));
/** @internal */
export const around = /*#__PURE__*/Debug.untracedDual(3, restore => (self, before, after) => new core.RequestResolverImpl(requests => Effect.acquireUseRelease(before, () => restore(self.runAll)(requests), after), Chunk.make("Around", self, before, after)));
/** @internal */
export const batchN = /*#__PURE__*/Debug.untracedDual(2, restore => (self, n) => new core.RequestResolverImpl(requests => {
  return n < 1 ? Effect.die(Cause.IllegalArgumentException("RequestResolver.batchN: n must be at least 1")) : restore(self.runAll)(Array.from(Chunk.map(Chunk.reduce(Chunk.unsafeFromArray(requests), Chunk.empty(), (acc, chunk) => Chunk.concat(acc, Chunk.chunksOf(Chunk.unsafeFromArray(chunk), n))), chunk => Array.from(chunk))));
}, Chunk.make("BatchN", self, n)));
/** @internal */
export const contramapContext = /*#__PURE__*/Debug.untracedDual(2, restore => (self, f) => new core.RequestResolverImpl(requests => Effect.contramapContext(restore(self.runAll)(requests), context => restore(f)(context)), Chunk.make("ContramapContext", self, f)));
/** @internal */
export const eitherWith = /*#__PURE__*/Debug.untracedDual(3, restore => (self, that, f) => new core.RequestResolverImpl(batch => Effect.forEach(batch, requests => {
  const [as, bs] = RA.partitionMap(restore(f))(requests);
  return Effect.zipWithPar(restore(self.runAll)(Array.of(as)), restore(that.runAll)(Array.of(bs)), () => void 0);
}), Chunk.make("EitherWith", self, that, f)));
/** @internal */
export const fromFunction = /*#__PURE__*/Debug.untracedMethod(restore => f => makeBatched(requests => Effect.forEachDiscard(requests, request => complete(request, core.exitSucceed(restore(f)(request))))).identified("FromFunction", f));
/** @internal */
export const fromFunctionBatched = /*#__PURE__*/Debug.untracedMethod(restore => f => makeBatched(as => forEachWithIndex(restore(f)(as), (res, i) => complete(as[i], core.exitSucceed(res)))).identified("FromFunctionBatched", f));
/** @internal */
export const fromFunctionEffect = /*#__PURE__*/Debug.untracedMethod(restore => f => makeBatched(requests => Effect.forEachParDiscard(requests, a => Effect.flatMap(Effect.exit(restore(f)(a)), e => complete(a, e)))).identified("FromFunctionEffect", f));
/** @internal */
export const never = /*#__PURE__*/Debug.untracedMethod(() => _ => make(() => Effect.never()).identified("Never"));
/** @internal */
export const provideContext = /*#__PURE__*/Debug.untracedDual(2, () => (self, context) => contramapContext(self, _ => context).identified("ProvideContext", self, context));
/** @internal */
export const race = /*#__PURE__*/Debug.untracedDual(2, restore => (self, that) => new core.RequestResolverImpl(requests => Effect.race(restore(self.runAll)(requests), restore(that.runAll)(requests))).identified("Race", self, that));
//# sourceMappingURL=dataSource.mjs.map