import { Disposable } from '../types' ; import { DomListener , $ , createSvg , getEventPath , removeNode , addClass , } from './dom' ; export * from './drag' ; export * from './rect' ; export * from './dom' ; export * from './is' ; export * from './env' ; export * from './emitter' ; export * from './component' ; export * from './patch' ; export function clamp ( n , lower = 0 , upper = 1 ) { return Math . max ( Math . min ( n , upper ) , lower ) ; } const disposableMap = new Map ( ) ; export function getDisposableMap ( ) { return disposableMap ; } export function addDisposable < T extends Disposable > ( key , disposable ) { if ( ! disposableMap . has ( key ) ) disposableMap . set ( key , [ ] ) ; disposableMap . get ( key ) ! . push ( disposable ) ; return disposable ; } export function dispose ( key ) { if ( disposableMap . has ( key ) ) { disposableMap . get ( key ) ! . forEach ( ( item ) => item . dispose ( ) ) ; disposableMap . delete ( key ) ; } } export function addDisposableListener < K extends keyof GlobalEventHandlersEventMap > ( key , node , type , handler , useCapture ? ) ; export function addDisposableListener ( key , el , type , listener , options ? ) { const domListener = new DomListener ( el , type , listener , options ) ; if ( key ) addDisposable ( key , domListener ) ; return domListener ; } export function throttle ( fn , ctx ? ) { let pending = false ; let first = true ; let args = null ; return function ( ) { args = arguments ; if ( first ) { first = false ; return fn . apply ( ctx , args ) ; } if ( pending ) return ; pending = true ; requestAnimationFrame ( ( ) => { fn . apply ( ctx , args ) ; pending = false ; } ) ; } ; } export function repeatStr ( str , t ) { const ret = [ ] ; for ( let i = 0 ; i < t ; ++ i ) { ret . push ( str ) ; } return ret . join ( '' ) ; } function padStart ( v , len = 2 , str = '0' ) { v = String ( v ) ; if ( v . length >= 2 ) return v ; for ( let i = 0 , l = len - v . length ; i < l ; i ++ ) { v = str + v ; } return v ; } export function formatTime ( seconds ) { if ( ! isFinite ( seconds ) ) return '-' ; if ( seconds <= 0 ) return '0:00' ; seconds = Math . round ( seconds ) ; if ( seconds < 60 ) return `0:${ padStart ( seconds ) }` ; if ( seconds < 3600 ) { return `${ Math . floor ( seconds / 60 ) }:${ padStart ( seconds % 60 ) }` ; } return `${ Math . floor ( seconds / 3600 ) }:${ padStart ( Math . floor ( ( seconds % 3600 ) / 60 ) ) }:${ padStart ( seconds % 60 ) }` ; } export const internalUtils = { $ , clamp , addDisposableListener , addDisposable , createSvg , dispose , getEventPath , removeNode , addClass , } ;