import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/concat";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/write";
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/unit";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/suspend";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
export function utfEncodeFor(bom = tsplus_module_1.empty(), __tsplusTrace) {
  return stream => {
    void 0;
    return new StreamInternal(tsplus_module_12.suspend(() => {
      const transform = tsplus_module_10.readWith(received => {
        if (tsplus_module_2.isEmpty(received)) {
          return transform;
        }

        const bytes = tsplus_module_5.reduce_(received, tsplus_module_1.empty(), (acc, string) => {
          // @ts-expect-error
          const encoder = new TextEncoder();
          const bytes = encoder.encode(string);
          return tsplus_module_4.concat_(acc, tsplus_module_3.from(bytes));
        });
        return tsplus_module_7.zipRight_(tsplus_module_6.write(() => bytes), () => transform);
      }, err => tsplus_module_8.fail(() => err), () => tsplus_module_9.unit);
      const channel = tsplus_module_7.zipRight_(tsplus_module_6.write(() => bom), () => transform);
      return tsplus_module_11.pipeTo_(stream.channel, () => channel);
    }));
  };
}
//# sourceMappingURL=utfEncodeFor.mjs.map