import * as tsplus_module_1 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/unwrapScoped";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_7 from "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/foldChannel";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/dieMessage";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/readWith";
import { SinkInternal } from "@effect/core/stream/Sink/operations/_internal/SinkInternal";
/**
 * Creates a sink from a chunk processing function.
 *
 * @tsplus static ets/Sink/Ops fromPush
 */

export function fromPush(push, __tsplusTrace) {
  return new SinkInternal(tsplus_module_2.unwrapScoped(tsplus_module_1.map_(push, pull, __tsplusTrace)));
}

function pull(push, __tsplusTrace) {
  return tsplus_module_12.readWith(input => tsplus_module_10.foldChannel_(tsplus_module_9.fromEffect(() => push(tsplus_module_8.some(input))), ({
    tuple: [either, leftovers]
  }) => tsplus_module_7.fold_(either, e => tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_4.fail(() => e)), z => tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_6.succeedNow(z))), () => pull(push, __tsplusTrace)), err => tsplus_module_4.fail(() => err), () => tsplus_module_10.foldChannel_(tsplus_module_9.fromEffect(() => push(tsplus_module_8.none)), ({
    tuple: [either, leftovers]
  }) => tsplus_module_7.fold_(either, e => tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_4.fail(() => e)), z => tsplus_module_5.zipRight_(tsplus_module_3.write(() => leftovers), () => tsplus_module_6.succeedNow(z))), () => tsplus_module_9.fromEffect(() => tsplus_module_11.dieMessage(() => "empty sink", __tsplusTrace))));
}
//# sourceMappingURL=fromPush.mjs.map