const fileName_1 = "(@effect/core) _src/stream/Channel/operations/toPull.ts";
import * as tsplus_module_1 from "@effect/core/io/Effect/operations/suspendSucceed";
import * as tsplus_module_2 from "@tsplus/stdlib/data/Function";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/acquireReleaseExit";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/failCause";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Either/definition";
import * as tsplus_module_9 from "@effect/core/io/Exit/operations/fold";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/zipRight";
import { ChannelExecutor, readUpstream } from "@effect/core/stream/Channel/ChannelExecutor";
import { concreteChannelState } from "@effect/core/stream/Channel/ChannelState";
/**
 * Interpret a `Channel` to a managed pull.
 *
 * @tsplus getter ets/Channel toPull
 */

export function toPull(self) {
  return tsplus_module_6.map_(tsplus_module_5.acquireReleaseExit(() => tsplus_module_3.succeed(() => new ChannelExecutor(() => self, undefined, tsplus_module_2.identity), fileName_1 + ":14:19"), (exec, exit) => {
    const finalize = exec.close(exit, fileName_1 + ":16:34");
    return finalize == null ? tsplus_module_4.unit : finalize;
  }, fileName_1 + ":13:35"), exec => tsplus_module_1.suspendSucceed(() => interpret(exec.run(), exec), fileName_1 + ":19:40"), fileName_1 + ":19:8");
}

function interpret(channelState, exec) {
  void 0;

  switch (channelState._tag) {
    case "Done":
      {
        return tsplus_module_9.fold_(exec.getDone(), cause => tsplus_module_7.failCause(() => cause, fileName_1 + ":30:36"), done => tsplus_module_3.succeed(() => tsplus_module_8.left(done), fileName_1 + ":31:80"));
      }

    case "Emit":
      {
        return tsplus_module_3.succeed(() => tsplus_module_8.right(exec.getEmit()), fileName_1 + ":35:28");
      }

    case "Effect":
      {
        return tsplus_module_10.zipRight_(channelState.effect, () => interpret(exec.run(), exec), fileName_1 + ":38:42");
      }

    case "Read":
      {
        return readUpstream(channelState, () => interpret(exec.run(), exec), fileName_1 + ":43:26");
      }
  }
}
//# sourceMappingURL=toPull.mjs.map