import { Observable , Subject } from 'rxjs' ; import { distinctUntilChanged , filter , map , pluck , switchMap , takeUntil , } from 'rxjs/operators' ; import { Offset , OffsetNumbers } from '../types' ; import { offsetOfElement } from '../utils/dom' ; import { convertOffsetToPercentOrPixels } from '../utils/misc' ; interface AllMoveObservableOptions { element : HTMLElement ; group : string ; onComplete ? : ( ) => void ; shouldConvertToPercent ? : boolean ; } interface AllMovePayload { group : string ; offset : OffsetNumbers ; } export const allMoveStart$ = new Subject < any > ( ) ; export const allMoveEnd$ = new Subject < any > ( ) ; export const allMoveUpdate$ = new Subject < AllMovePayload > ( ) ; export const createAllMoveObservable = ( { element , group , onComplete , shouldConvertToPercent = true , } ) : Observable < Offset > => allMoveStart$ . pipe ( switchMap ( ( ) => { const move$ = allMoveUpdate$ . pipe ( takeUntil ( allMoveEnd$ ) , filter ( isMemberOfGroup ( group ) ) , pluck ( 'offset' ) , translateMovementToPosition ( element , shouldConvertToPercent ) ) ; move$ . subscribe ( { complete : ( ) => onComplete && onComplete ( ) , } ) ; return move$ ; } ) ) ; /**
 * Do the payload group and observable group match?
 */ const isMemberOfGroup = ( group ) => ( payload ) => payload . group === group ; const translateMovementToPosition = ( element , shouldConvertToPercent ) => ( observable$ ) => observable$ . pipe ( map ( addOffsets ( offsetOfElement ( element ) ) ) , distinctUntilChanged ( ) , map ( convertOffsetToPercentOrPixels ( shouldConvertToPercent , element . parentElement ! ) ) ) ; /**
 * Add the change in mouse position to an origin point.
 */ const addOffsets = ( origin ) => ( change ) => ( { left : origin . left + change . left , top : origin . top + change . top , } ) ;