import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
/**
 * @文件说明: Map.Controls - 地图控件
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-31 12:49:01
 */

import assign from 'lodash/assign';
import find from 'lodash/find';
import { PropTypes } from '../../_util/antdv';
import Attribution, { defaultAttributionPosition } from './attribution';
import Navigation, { defaultNavigationPosition } from './navigation';
import Scale, { defaultScalePosition } from './scale';
import MouseCoordinates, { defaultMouseCoordinatesPosition } from './mouse-coordinates';

// 地图控件的类型枚举名
export var mapControlsTypeKeys = ['Attribution', 'Navigation', 'Scale', 'MouseCoordinates'];
// 地图控件的位置枚举
export var mapControlsPosition = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];

var Controls = {
  name: 'PjMap.Controls',
  components: {
    Attribution: Attribution,
    Navigation: Navigation,
    Scale: Scale,
    MouseCoordinates: MouseCoordinates
  },
  props: {
    dataList: PropTypes.array
  },
  data: function data() {
    return {
      controls: {}
    };
  },

  inject: {
    mapProvider: { 'default': function _default() {} }
  },
  computed: {
    className: function className() {
      var prefixCls = this.mapProvider.prefixCls;

      return _defineProperty({}, prefixCls + '-controls-wrapper', true);
    },
    groupControls: function groupControls() {
      var _this = this;

      var controls = {};
      this.dataList && this.dataList.length && this.dataList.map(function (item) {
        var control = assign({}, item);
        !control.position && (control.position = _this.getDefaultControlPosition(item.type));
        !controls[control.position] && (controls[control.position] = []);
        controls[control.position].push(control);
      });
      return controls;
    }
  },
  methods: {
    // 获取控件类型的默认Position位置
    getDefaultControlPosition: function getDefaultControlPosition(key) {
      switch (key) {
        case 'Attribution':
          return defaultAttributionPosition;
        case 'Navigation':
          return defaultNavigationPosition;
        case 'Scale':
          return defaultScalePosition;
        case 'MouseCoordinates':
          return defaultMouseCoordinatesPosition;
      }
    },


    // 根据控件分组数据渲染控件位置容器
    renderControlsGroupWrapper: function renderControlsGroupWrapper() {
      var _this2 = this;

      var h = this.$createElement;
      var className = this.className,
          groupControls = this.groupControls;

      var keys = Object.keys(groupControls);
      return keys && keys.map(function (key) {
        var classes = _extends({}, className, _defineProperty({}, 'position-' + key, true));
        return h(
          'div',
          { 'class': classes },
          [_this2.renderControlsComponent(groupControls[key])]
        );
      });
    },


    // 根据控件类型渲染地图控件组件
    renderControlsComponent: function renderControlsComponent(controls) {
      var _this3 = this;

      var h = this.$createElement;

      return controls && controls.map(function (item, idx) {
        var props = _extends({
          key: item.id || String(idx),
          id: item.id
        }, item.options || {});
        item.position && (props.position = item.position);
        item.offset && (props.offset = item.offset);
        var on = {
          'update:offset': function updateOffset(val) {
            var control = _this3.getDataControlById(item.id);
            control && _this3.$set(control, 'offset', val);
          }
        };
        // 根据控件类型加载对应的地图控件
        var component = null;
        switch (item.type) {
          case 'Attribution':
            {
              on['update:content'] = function (val) {
                var control = _this3.getDataControlById(item.id);
                control && _this3.$set(control.options, 'content', val);
              };
              component = h(Attribution, _mergeJSXProps([{ props: props }, { on: on }]));
              break;
            }
          case 'Navigation':
            {
              component = h(Navigation, _mergeJSXProps([{ props: props }, { on: on }]));
              break;
            }
          case 'Scale':
            {
              on['update:unit'] = function (val) {
                var control = _this3.getDataControlById(item.id);
                control && _this3.$set(control.options, 'unit', val);
              };
              component = h(Scale, _mergeJSXProps([{ props: props }, { on: on }]));
              break;
            }
          case 'MouseCoordinates':
            {
              component = h(MouseCoordinates, _mergeJSXProps([{ props: props }, { on: on }]));
              break;
            }
          default:
            break;
        }
        _this3.controls[item.id] = component;
        return component;
      });
    },


    // 根据Id获取原始控件数据集的控件项
    getDataControlById: function getDataControlById(id) {
      var control = find(this.dataList, { id: id });
      return control || null;
    },


    // 获取指定Id的地图控件组件Component对象
    getControlComponent: function getControlComponent(id) {
      return this.controls && this.controls[id];
    }
  },
  render: function render() {
    var h = arguments[0];

    return h(
      'section',
      {
        attrs: { 'data-type': 'control' }
      },
      [this.renderControlsGroupWrapper()]
    );
  }
};

export default Controls;