import Vue , { ComponentOptions } from 'vue' ; import { Store } from 'vuex' ; import { getXComponentXModuleName , isXComponent } from '../components/x-component.utils' ; import { RootXStoreState } from '../store/store.types' ; import { XEvent , XEventPayload } from '../wiring/events.types' ; import { WireMetadata } from '../wiring/wiring.types' ; import { FeatureLocation } from '../types/origin' ; import { XModuleName } from '../x-modules/x-modules.types' ; import { XBus } from './x-bus.types' ; import { getAliasAPI } from './x-plugin.alias' ; import { XComponentAPI , XComponentBusAPI } from './x-plugin.types' ; declare module 'vue/types/vue' { export interface Vue { $x : XComponentAPI ; } } declare module 'vue/types/options' { export interface ComponentOptions < V , Data , Methods , Computed , PropsDef , Props > { xModule ? : XModuleName ; } } interface PrivateExtendedVueComponent extends Vue { $location ? : FeatureLocation ; $store : Store < { x : Partial < RootXStoreState [ 'x' ] > ; } > ; xComponent : Vue | undefined ; } /**
 * Vue global mixin that adds a `$x` object to every component with the  XComponentAPI .
 *
 *  remarks param bus - The  XBus to use inside the components for emitting events.
 *  returns internal export const createXComponentAPIMixin = ( bus ) : ComponentOptions < Vue > & ThisType < PrivateExtendedVueComponent > => ( { inject : { $location : { from : 'location' , default : undefined } } , beforeCreate ( this ) { this . xComponent = getRootXComponent ( this ) ; const aliasAPI = getAliasAPI ( this ) ; const busAPI = getBusAPI ( bus , this ) ; this . $x = Object . assign ( aliasAPI , busAPI ) ; } } ) ; /**
 * Creates an object containing the API related to the  XBus .
 *
 *  param bus - The global  XBus .
 *  param component returns An object containing the  XComponentBusAPI .
 *  internal export function getBusAPI ( bus , component ) { return { emit : < Event extends XEvent > ( event , payload ? , metadata = { } ) => { bus . emit ( event , payload as any , createWireMetadata ( component , metadata ) ) ; component . xComponent ?. $emit ( event , payload ) ; } , on : bus . on . bind ( bus ) } ; } param component param metadata returns A  WireMetadata object.
 *  internal function createWireMetadata ( component , metadata ) { return { moduleName : getXComponentXModuleName ( component . xComponent ) , location : component . $location , ... metadata } ; } param component returns public export function getRootXComponent ( component ) { let xComponent ; while ( component !== undefined && component !== null ) { if ( isXComponent ( component ) ) { xComponent = component ; } component = component . $parent ; } return xComponent ; }