// ets_tracing: off
import * as CK from "../../../../Collections/Immutable/Chunk";
import * as Tp from "../../../../Collections/Immutable/Tuple";
import * as O from "../../../../Option";
import * as CH from "../../Channel";
import * as C from "../core";
/**
 * 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.
 */

export function changesWith_(self, equal) {
  const writer = last => CH.readWithCause(chunk => {
    const {
      tuple: [newLast, newChunk]
    } = CK.reduce_(chunk, Tp.tuple(last, CK.empty()), ({
      tuple: [op, os]
    }, o1) => {
      if (O.isSome(op)) {
        if (equal.equals(op.value, o1)) {
          return Tp.tuple(O.some(o1), os);
        }
      }

      return Tp.tuple(O.some(o1), CK.append_(os, o1));
    });
    return CH.zipRight_(CH.write(newChunk), writer(newLast));
  }, cause => CH.failCause(cause), _ => CH.unit);

  return new C.Stream(self.channel[">>>"](writer(O.none)));
}
/**
 * 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.
 *
 * @ets_data_first changesWith_
 */

export function changesWith(equal) {
  return self => changesWith_(self, equal);
}
//# sourceMappingURL=changesWith.js.map