export const registerComponent = {
  data() {
    return {
      originProps: null, // 原始属性
    }
  },
  created() {
    this.$emit('register', this);
  },
  methods: {
    setProps: function(props) {
      this.originProps = props;
      this.watchProps && this.watchProps({...this.$props, ...props})
    }
  },
  computed: {
    $dyn_props: function() {
      const props = {
        ...this.$props,
        ...this.originProps
      }
      return props
    }
  }
}


/**
 * 使用组件
 * @param {*} props 
 * @returns 
 */
export const useComponent = function(props) {
  const instance = { value: null }

  /**
   * 获取组件实例
   */
  function getRealInstance() {
    return instance.value
  }

  /**
   * 设置组件实例
   */
  function setRealInstance(component) {
    instance.value = component
  }

  /**
   * 注册组件实例
   */
  function register(component) {
    setRealInstance(component)
  }

  function effectiveCall(fn, ...args) {
    if (getRealInstance()) {
      getRealInstance()[fn](...args)
    }
  }

  const methods =  {
    call: (fn, ...args) => effectiveCall(fn, ...args),
    setProps: props => effectiveCall('setProps', props),
  }



  return [register, methods]
}
