const fileName_1 = "(@effect/core) _src/stream/Channel/operations/buffer.ts";
import * as tsplus_module_1 from "@effect/core/stream/Channel/operations/suspend";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_7 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/unwrap";
/**
 * Creates a channel backed by a buffer. When the buffer is empty, the channel
 * will simply passthrough its input as output. However, when the buffer is
 * non-empty, the value inside the buffer will be passed along as output.
 *
 * @tsplus static ets/Channel/Ops buffer
 */

export function buffer(empty, isEmpty, ref) {
  return tsplus_module_1.suspend(() => bufferInternal(empty, isEmpty, ref));
}

function bufferInternal(empty, isEmpty, ref) {
  return tsplus_module_8.unwrap(() => ref.modify(value => isEmpty(value) ? tsplus_module_7.make(tsplus_module_6.readWith(inElem => tsplus_module_3.zipRight_(tsplus_module_2.write(() => inElem), () => bufferInternal(empty, isEmpty, ref)), inErr => tsplus_module_4.fail(() => inErr), inDone => tsplus_module_5.succeedNow(inDone)), value) : tsplus_module_7.make(tsplus_module_3.zipRight_(tsplus_module_2.write(() => value), () => bufferInternal(empty, isEmpty, ref)), empty), fileName_1 + ":22:15"));
}
//# sourceMappingURL=buffer.mjs.map