import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_2 from "@effect/core/stream/Stream/definition";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/identity";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/pipeTo";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
export function defaultIfEmpty_(self, emptyValue) {
  if (tsplus_module_1.isChunk(emptyValue)) {
    return defaultIfEmptyChunk(self, emptyValue);
  }

  if (tsplus_module_2.isStream(emptyValue)) {
    return defaultIfEmptyStream(self, emptyValue);
  }

  return defaultIfEmptyValue(self, emptyValue);
}
/**
 * If this stream is empty, produce the specified element or chunk of elements,
 * or switch to the specified stream.
 *
 * @tsplus static ets/Stream/Aspects defaultIfEmpty
 */

export const defaultIfEmpty = stream => self => defaultIfEmpty_(self, stream);
/**
 * Produces the specified element if this stream is empty.
 */

function defaultIfEmptyValue(self, a) {
  return defaultIfEmptyChunk(self, tsplus_module_3.single(a));
}
/**
 * Produces the specified chunk if this stream is empty.
 */


function defaultIfEmptyChunk(self, chunk) {
  return defaultIfEmptyStream(self, new StreamInternal(tsplus_module_4.write(() => chunk)));
}
/**
 * Switches to the provided stream in case this one is empty.
 */


function defaultIfEmptyStream(self, stream) {
  const writer = tsplus_module_9.readWith(input => tsplus_module_5.isEmpty(input) ? writer : tsplus_module_7.zipRight_(tsplus_module_4.write(() => input), () => tsplus_module_6.identity()), e => tsplus_module_8.fail(() => e), () => {
    void 0;
    return stream.channel;
  });
  void 0;
  return new StreamInternal(tsplus_module_10.pipeTo_(self.channel, () => writer));
}
//# sourceMappingURL=defaultIfEmpty.mjs.map