import * as tsplus_module_1 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/append";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Returns a new stream that only emits elements that are not equal to the
 * previous element emitted, using the specified function to determine whether
 * two elements are equal.
 *
 * @tsplus fluent ets/Stream changesWith
 */

export function changesWith_(self, f, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => writer(tsplus_module_1.none, f)));
}
/**
 * Returns a new stream that only emits elements that are not equal to the
 * previous element emitted, using the specified function to determine whether
 * two elements are equal.
 *
 * @tsplus static ets/Stream/Aspects changesWith
 */

export const changesWith = (f, __tsplusTrace) => self => changesWith_(self, f, __tsplusTrace);

function writer(last, f) {
  return tsplus_module_11.readWithCause(chunk => {
    const {
      tuple: [newLast, newChunk]
    } = tsplus_module_6.reduce_(chunk, tsplus_module_4.make(last, tsplus_module_3.empty()), ({
      tuple: [option, as]
    }, a) => tsplus_module_1.isSome(option) && f(option.value, a) ? tsplus_module_4.make(tsplus_module_1.some(a), as) : tsplus_module_4.make(tsplus_module_1.some(a), tsplus_module_5.append_(as, a)));
    return tsplus_module_8.zipRight_(tsplus_module_7.write(() => newChunk), () => writer(newLast, f));
  }, cause => tsplus_module_9.failCause(() => cause), () => tsplus_module_10.unit);
}
//# sourceMappingURL=changesWith.mjs.map