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';

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

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

    // 视图模型路径
    const viewPath = c.model.embedView.source.modelPath;

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