import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _typeof from 'babel-runtime/helpers/typeof';
import _extends from 'babel-runtime/helpers/extends';
/**
 * @文件说明: 地图内置组件 - 动态实例Vue组件包封装器
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-06-11 16:31:47
 */

import Vue from 'vue';
import Base from '../../../base';
import { BaseMixin, PropTypes, getComponentFromProp, filterEmpty } from '../../../_util/antdv';
import { isFunction, isString } from '../../../_util/methods-util';
import DefaultMarker from './marker';
import DefaultPopup from './popup';

var ElementWrapper = {
  inheritAttrs: false,
  mixins: [BaseMixin],
  inject: {
    mapProvider: { 'default': function _default() {} }
  },
  props: {
    // 组件包的类型，内置默认：[ Marker | Popup ]
    type: PropTypes.string,
    // Slot插槽
    slots: PropTypes.any,
    // 组件的传递属性数据
    vProps: PropTypes.object,
    // 原生对象的参数选项
    options: PropTypes.object.def({})
  },
  computed: {
    iMapApi: function iMapApi() {
      var iMapApi = this.mapProvider.iMapApi;

      return iMapApi;
    }
  },
  methods: {
    // 渲染Marker标注Slot插槽
    renderMarkerSlotScope: function renderMarkerSlotScope() {
      var h = this.$createElement;
      var type = this.type,
          slots = this.slots,
          iMapApi = this.iMapApi,
          vProps = this.vProps,
          options = this.options;

      if (!slots) {
        return h('div', [h(DefaultMarker, { props: _extends({}, vProps, { options: options }) })]);
      } else {
        if (isFunction(slots)) {
          return h('div', [slots(h)]);
        } else if ((typeof slots === 'undefined' ? 'undefined' : _typeof(slots)) === 'object' && slots instanceof HTMLElement) {
          return h('div', {
            domProps: {
              'innerHTML': slots.outerHTML
            }
          });
        } else if (isString(slots)) {
          var slotNodes = getComponentFromProp(iMapApi.component, type + '.' + slots, {}, false);
          return slotNodes ? filterEmpty(slotNodes(_extends({ iMapApi: iMapApi }, vProps))) : null;
        } else {
          return null;
        }
      }
    }
  },
  render: function render() {
    var h = arguments[0];
    var type = this.type,
        iMapApi = this.iMapApi,
        vProps = this.vProps,
        options = this.options;
    // 判断是否无插槽对象，则直接采用默认组件

    switch (type) {
      case 'marker':
        return this.renderMarkerSlotScope();
      case 'popup':
        return h(DefaultPopup, { props: _extends({}, vProps, { iMapApi: iMapApi, options: options }) });
      default:
        return null;
    }
  }
};

ElementWrapper.newInstance = function newElementWrapperInstance(properties, callback) {
  var _ref = properties || {},
      getContainer = _ref.getContainer,
      style = _ref.style,
      className = _ref['class'],
      iMapApi = _ref.iMapApi,
      props = _objectWithoutProperties(_ref, ['getContainer', 'style', 'class', 'iMapApi']);
  // 获取待创建的DOM对象


  var div = document.createElement('div');
  var element = getContainer ? getContainer() : document.body;
  element.appendChild(div);
  // 构建Vue组件实例
  var V = Base.Vue || Vue;
  new V({
    el: div,
    provide: function provide() {
      return {
        mapProvider: iMapApi.component.proxyVm._data
      };
    },
    mounted: function mounted() {
      var vm = this;
      this.$nextTick(function () {
        callback({
          component: vm,
          destroy: function destroy() {
            vm.$destroy();
            vm.$el.parentNode.removeChild(vm.$el);
          }
        });
      });
    },
    render: function render() {
      var h = arguments[0];

      var p = {
        ref: 'elementWrapper',
        style: style,
        'class': className,
        props: props
      };
      return h(ElementWrapper, p);
    }
  });
};

export default ElementWrapper;