import { ReplaySubject } from "rxjs";
import { pairwise, filter, first } from "rxjs/operators";

// we are interested in last 2 events, because we wait transition from <false> to <true>
const screenRevealManagerSubject$ = new ReplaySubject<boolean>(2);

export const emitScreenRevealManagerIsReadyToShow = () => {
  screenRevealManagerSubject$.next(true);
};

export const emitScreenRevealManagerIsNotReadyToShow = () => {
  screenRevealManagerSubject$.next(false);
};

export const waitUntilScreenRevealManagerIsReady = () => {
  return screenRevealManagerSubject$.pipe(
    pairwise(), // emit consecutive pairs: [prev, curr]
    filter(
      ([previousIsReady, currentIsReady]) => !previousIsReady && currentIsReady
    ), // detect transition from not_ready to ready
    first()
  );
};
