import { Observable , combineLatest , distinctUntilChanged , distinctUntilKeyChanged , map , switchMap } from "rxjs" ; import { Viewport , watchElementSize } from "~/browser" ; import { Header } from "../header" ; /**
 * Main area
 */ export interface Main { offset : number ; height : number ; active : boolean ; } /**
 * Watch options
 */ interface WatchOptions { viewport$ : Observable < Viewport > ; header$ : Observable < Header > ; } param el param options returns export function watchMain ( el , { viewport$ , header$ } ) { const adjust$ = header$ . pipe ( map ( ( { height } ) => height ) , distinctUntilChanged ( ) ) ; const border$ = adjust$ . pipe ( switchMap ( ( ) => watchElementSize ( el ) . pipe ( map ( ( { height } ) => ( { top : el . offsetTop , bottom : el . offsetTop + height } ) ) , distinctUntilKeyChanged ( "bottom" ) ) ) ) ; return combineLatest ( [ adjust$ , border$ , viewport$ ] ) . pipe ( map ( ( [ header , { top , bottom } , { offset : { y } , size : { height } } ] ) => { height = Math . max ( 0 , height - Math . max ( 0 , top - y , header ) - Math . max ( 0 , height + y - bottom ) ) ; return { offset : top - header , height , active : top - header <= y } ; } ) , distinctUntilChanged ( ( a , b ) => ( a . offset === b . offset && a . height === b . height && a . active === b . active ) ) ) ; }