import { IBizContext } from '@ibiz-template/core';
import { ViewPanelModel } from '@ibiz-template/model';
import { defineComponent, getCurrentInstance, PropType } from 'vue';
import { usePickupViewPanelController } from '@ibiz-template/vue-util';
import { IModal, ViewMode } from '@ibiz-template/runtime';
import { getViewComponentName } from '@/util';

export const PickupViewPanel = defineComponent({
  props: {
    modelData: {
      type: ViewPanelModel,
      required: true,
    },
    context: {
      type: IBizContext,
      required: true,
    },
    params: { type: Object as PropType<IParams>, default: () => ({}) },
  },
  setup(props) {
    const { proxy } = getCurrentInstance()!;
    const c = usePickupViewPanelController(
      proxy,
      props.modelData,
      props.context,
      props.params,
    );

    // 模态对象
    const modal: IModal = { mode: ViewMode.EMBED };

    // 视图组件名称和视图模型路径
    const viewComponentName = getViewComponentName(
      c.model.embedView.source.viewType,
    );
    const viewPath = c.model.embedView.source.modelPath;

    return { c, modal, viewComponentName, viewPath };
  },
  render(h) {
    if (!this.c.complete) {
      return;
    }
    return h(this.viewComponentName, {
      props: {
        context: this.c.context,
        params: this.c.params,
        modal: this.modal,
        modelPath: this.viewPath,
      },
      on: {
        neuronInit: this.c.nerve.onNeuronInit('embedView'),
      },
    });
  },
});