'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

var _base = require('../../../base');

var _base2 = _interopRequireDefault(_base);

var _antdv = require('../../../_util/antdv');

var _methodsUtil = require('../../../_util/methods-util');

var _marker = require('./marker');

var _marker2 = _interopRequireDefault(_marker);

var _popup = require('./popup');

var _popup2 = _interopRequireDefault(_popup);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

/**
 * @文件说明: 地图内置组件 - 动态实例Vue组件包封装器
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-06-11 16:31:47
 */

var ElementWrapper = {
  inheritAttrs: false,
  mixins: [_antdv.BaseMixin],
  inject: {
    mapProvider: { 'default': function _default() {} }
  },
  props: {
    // 组件包的类型，内置默认：[ Marker | Popup ]
    type: _antdv.PropTypes.string,
    // Slot插槽
    slots: _antdv.PropTypes.any,
    // 组件的传递属性数据
    vProps: _antdv.PropTypes.object,
    // 原生对象的参数选项
    options: _antdv.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(_marker2['default'], { props: (0, _extends3['default'])({}, vProps, { options: options }) })]);
      } else {
        if ((0, _methodsUtil.isFunction)(slots)) {
          return h('div', [slots(h)]);
        } else if ((typeof slots === 'undefined' ? 'undefined' : (0, _typeof3['default'])(slots)) === 'object' && slots instanceof HTMLElement) {
          return h('div', {
            domProps: {
              'innerHTML': slots.outerHTML
            }
          });
        } else if ((0, _methodsUtil.isString)(slots)) {
          var slotNodes = (0, _antdv.getComponentFromProp)(iMapApi.component, type + '.' + slots, {}, false);
          return slotNodes ? (0, _antdv.filterEmpty)(slotNodes((0, _extends3['default'])({ 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(_popup2['default'], { props: (0, _extends3['default'])({}, 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 = (0, _objectWithoutProperties3['default'])(_ref, ['getContainer', 'style', 'class', 'iMapApi']);
  // 获取待创建的DOM对象


  var div = document.createElement('div');
  var element = getContainer ? getContainer() : document.body;
  element.appendChild(div);
  // 构建Vue组件实例
  var V = _base2['default'].Vue || _vue2['default'];
  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);
    }
  });
};

exports['default'] = ElementWrapper;