import type { DynamicComponentLazyLoadedModalArgs, IDynamicComponentContainer } from './dynamic-component-contracts';
import { toNative, Vue } from 'vue-facing-decorator';
import { globalState } from '../../app/global-state';
import { Component } from '../../app/vuetsx';
import DynamicComponentContainer from './dynamic-component-container';

@Component
class RootDynamicComponentContainerComponent extends Vue implements IDynamicComponentContainer {
	created() {
		globalState.RootDynamicContainerInstance = this;
	}

	getInstance<T>(uuid: string): T {
		return this.getContainer().getInstance(uuid);
	}

	addInstance(uuid: string, componentConstructor: any) {
		this.getContainer().addInstance(uuid, componentConstructor);
	}

	showLazyLoadedModal(args: DynamicComponentLazyLoadedModalArgs): Promise<any> {
		return this.getContainer().showLazyLoadedModal(args);
	}

	private getContainer() {
		return this.$refs.dynamicContainer as typeof DynamicComponentContainer.prototype;
	}

	render(h) {
		return <DynamicComponentContainer ref="dynamicContainer" />;
	}
}

const RootDynamicComponentContainer = toNative(RootDynamicComponentContainerComponent);
export default RootDynamicComponentContainer;
