import { BindState , ExtendedHTMLElement } from "./types" ; import getParents from "./get-parents" ; function getState ( element ) { const closestStateContainer = element . closest ( "[x-state]" ) as ExtendedHTMLElement ; if ( ! closestStateContainer ) { return { state : { } } ; } const { state } = closestStateContainer ; const labeledState = getLabeledState ( element , "x-label" ) ; return Object . assign ( { } , labeledState , { state } ) ; } function getLabeledState ( element , labelKey ) { const labeledStateContainers = getParents ( element , labelKey ) ; const ret = { } ; for ( let i = labeledStateContainers . length ; i -- ; ) { const labeledStateContainer = labeledStateContainers [ i ] as ExtendedHTMLElement ; const label = labeledStateContainer . getAttribute ( labelKey ) ; if ( label ) { ret [ label ] = labeledStateContainer . state ; } } return ret ; } export default getState ;