import { combineLatest, Subject, map } from 'rxjs';

var price = 31.99;

/** Declare an Observable
 * Data we can observe
 * over time
 */
const qty$ = new Subject<number>();

/** Emit when the action occurs
 * Hey Observable,
 * here is a new value
 */
const onQuantityChanged = (quantity) => qty$.next(quantity);

/** React to emissions
 * Whenever you
 * change, I`II change
 */
const exPreice$ = qty$.pipe(map((q) => q * price));
const tax$ = exPreice$.pipe(map((t) => Math.round(t * 10.75) / 100));
const deliveryFree$ = exPreice$.pipe(map((q) => (q < 35 ? 5.99 : 0)));
const totalPrice$ = combineLatest([exPreice$, deliveryFree$, tax$]).pipe(
  map(([p, d, t]) => p + d + t)
);
totalPrice$.subscribe(console.log);
onQuantityChanged(2); // 70.86