import { Dictionary } from '../options' ; export function removeElement ( el ) { if ( el . parentNode ) { el . parentNode . removeChild ( el ) ; } } export function elementClosest ( el , selector ) { if ( el . closest ) { return el . closest ( selector ) ; } if ( ! document . documentElement . contains ( el ) ) { return null ; } do { if ( elementMatches ( el , selector ) ) { return el ; } el = ( el . parentElement || el . parentNode ) as HTMLElement ; } while ( el !== null && el . nodeType === 1 ) ; return null ; } export function elementMatches ( el , selector ) { let method = el . matches || ( el as any ) . matchesSelector || ( el as any ) . msMatchesSelector ; return method . call ( el , selector ) ; } export function findElements ( container , selector ) { let containers = container instanceof HTMLElement ? [ container ] : container ; let allMatches = [ ] ; for ( let i = 0 ; i < containers . length ; i += 1 ) { let matches = containers [ i ] . querySelectorAll ( selector ) ; for ( let j = 0 ; j < matches . length ; j += 1 ) { allMatches . push ( matches [ j ] as HTMLElement ) ; } } return allMatches ; } export function findDirectChildren ( parent , selector ? ) { let parents = parent instanceof HTMLElement ? [ parent ] : parent ; let allMatches = [ ] ; for ( let i = 0 ; i < parents . length ; i += 1 ) { let childNodes = parents [ i ] . children ; for ( let j = 0 ; j < childNodes . length ; j += 1 ) { let childNode = childNodes [ j ] ; if ( ! selector || elementMatches ( childNode as HTMLElement , selector ) ) { allMatches . push ( childNode ) ; } } } return allMatches ; } const PIXEL_PROP_RE = /(top|left|right|bottom|width|height)$/i ; export function applyStyle ( el , props ) { for ( let propName in props ) { applyStyleProp ( el , propName , props [ propName ] ) ; } } export function applyStyleProp ( el , name , val ) { if ( val == null ) { el . style [ name ] = '' ; } else if ( typeof val === 'number' && PIXEL_PROP_RE . test ( name ) ) { el . style [ name ] = `${ val }px` ; } else { el . style [ name ] = val ; } } export function getEventTargetViaRoot ( ev ) { return ev . composedPath ?. ( ) [ 0 ] ?? ev . target ; } export function getElRoot ( el ) { return el . getRootNode ? el . getRootNode ( ) as ShadowRoot : document ; } let guid = 0 ; export function getUniqueDomId ( ) { guid += 1 ; return 'fc-dom-' + guid ; }