"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toPull = toPull;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/suspendSucceed"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Function"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/unit"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/acquireReleaseExit"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/failCause"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/definition"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/fold"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipRight"));

var _ChannelExecutor = /*#__PURE__*/require("@effect/core/stream/Channel/ChannelExecutor");

var _ChannelState = /*#__PURE__*/require("@effect/core/stream/Channel/ChannelState");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/stream/Channel/operations/toPull.ts";

/**
 * Interpret a `Channel` to a managed pull.
 *
 * @tsplus getter ets/Channel toPull
 */
function toPull(self) {
  return tsplus_module_6.map_(tsplus_module_5.acquireReleaseExit(() => tsplus_module_3.succeed(() => new _ChannelExecutor.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 (0, _ChannelExecutor.readUpstream)(channelState, () => interpret(exec.run(), exec), fileName_1 + ":43:26");
      }
  }
}
//# sourceMappingURL=toPull.js.map