// ets_tracing: off
import * as Tp from "../../Collections/Immutable/Tuple";
import { pipe } from "../../Function";
import * as T from "../_internal/effect";
import * as M from "../_internal/managed";
import * as SK from "../Sink";
import { concat_ } from "./concat";
import { fromChunk } from "./fromChunk";
import { repeatEffectChunkOption } from "./repeatEffectChunkOption";
import { run } from "./run";
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a managed resource. Like all `Managed` values, the provided
 * stream is valid only within the scope of `Managed`.
 */

export function peel_(self, sink) {
  return M.chain_(self.proc, pull => {
    const stream = repeatEffectChunkOption(pull);
    const s = SK.exposeLeftover(sink);
    return M.map_(T.toManaged(run(s)(stream)), e => Tp.tuple(e.get(0), concat_(fromChunk(e.get(1)), stream)));
  });
}
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a managed resource. Like all `Managed` values, the provided
 * stream is valid only within the scope of `Managed`.
 */

export function peel(sink) {
  return self => peel_(self, sink);
}
//# sourceMappingURL=peel.js.map