import { asArray } from './array' ; import { parentAutofocusables } from './DOMutils' ; import { VisibilityCache } from "./is" ; const getParents = ( node , parents = [ ] ) : HTMLElement [ ] => { parents . push ( node ) ; if ( node . parentNode ) { getParents ( node . parentNode as HTMLElement , parents ) ; } return parents ; } ; param nodeA param nodeB returns { boolean | * } export const getCommonParent = ( nodeA , nodeB ) => { const parentsA = getParents ( nodeA ) ; const parentsB = getParents ( nodeB ) ; for ( let i = 0 ; i < parentsA . length ; i += 1 ) { const currentParent = parentsA [ i ] ; if ( parentsB . indexOf ( currentParent ) >= 0 ) { return currentParent ; } } return false ; } ; export const getTopCommonParent = ( baseActiveElement , leftEntry , rightEntries ) : HTMLElement => { const activeElements = asArray ( baseActiveElement ) ; const leftEntries = asArray ( leftEntry ) ; const activeElement = activeElements [ 0 ] ; let topCommon = false ; leftEntries . filter ( Boolean ) . forEach ( ( entry ) => { topCommon = getCommonParent ( topCommon || entry , entry ) || topCommon ; rightEntries . filter ( Boolean ) . forEach ( ( subEntry ) => { const common = getCommonParent ( activeElement , subEntry ) ; if ( common ) { if ( ! topCommon || common . contains ( topCommon ) ) { topCommon = common ; } else { topCommon = getCommonParent ( common , topCommon ) ; } } } ) ; } ) ; return ( topCommon as unknown ) as HTMLInputElement ; } ; export const allParentAutofocusables = ( entries , visibilityCache ) : HTMLInputElement [ ] => entries . reduce ( ( acc , node ) => acc . concat ( parentAutofocusables ( node , visibilityCache ) ) , [ ] as HTMLInputElement [ ] ) ;