'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapControlsPosition = exports.mapControlsTypeKeys = undefined;

var _babelHelperVueJsxMergeProps = require('babel-helper-vue-jsx-merge-props');

var _babelHelperVueJsxMergeProps2 = _interopRequireDefault(_babelHelperVueJsxMergeProps);

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

var _extends4 = _interopRequireDefault(_extends3);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _assign = require('lodash/assign');

var _assign2 = _interopRequireDefault(_assign);

var _find = require('lodash/find');

var _find2 = _interopRequireDefault(_find);

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

var _attribution = require('./attribution');

var _attribution2 = _interopRequireDefault(_attribution);

var _navigation = require('./navigation');

var _navigation2 = _interopRequireDefault(_navigation);

var _scale = require('./scale');

var _scale2 = _interopRequireDefault(_scale);

var _mouseCoordinates = require('./mouse-coordinates');

var _mouseCoordinates2 = _interopRequireDefault(_mouseCoordinates);

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

// 地图控件的类型枚举名
var mapControlsTypeKeys = exports.mapControlsTypeKeys = ['Attribution', 'Navigation', 'Scale', 'MouseCoordinates'];
// 地图控件的位置枚举
/**
 * @文件说明: Map.Controls - 地图控件
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-31 12:49:01
 */

var mapControlsPosition = exports.mapControlsPosition = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];

var Controls = {
  name: 'PjMap.Controls',
  components: {
    Attribution: _attribution2['default'],
    Navigation: _navigation2['default'],
    Scale: _scale2['default'],
    MouseCoordinates: _mouseCoordinates2['default']
  },
  props: {
    dataList: _antdv.PropTypes.array
  },
  data: function data() {
    return {
      controls: {}
    };
  },

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

      return (0, _defineProperty3['default'])({}, prefixCls + '-controls-wrapper', true);
    },
    groupControls: function groupControls() {
      var _this = this;

      var controls = {};
      this.dataList && this.dataList.length && this.dataList.map(function (item) {
        var control = (0, _assign2['default'])({}, 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 _attribution.defaultAttributionPosition;
        case 'Navigation':
          return _navigation.defaultNavigationPosition;
        case 'Scale':
          return _scale.defaultScalePosition;
        case 'MouseCoordinates':
          return _mouseCoordinates.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 = (0, _extends4['default'])({}, className, (0, _defineProperty3['default'])({}, '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 = (0, _extends4['default'])({
          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(_attribution2['default'], (0, _babelHelperVueJsxMergeProps2['default'])([{ props: props }, { on: on }]));
              break;
            }
          case 'Navigation':
            {
              component = h(_navigation2['default'], (0, _babelHelperVueJsxMergeProps2['default'])([{ 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(_scale2['default'], (0, _babelHelperVueJsxMergeProps2['default'])([{ props: props }, { on: on }]));
              break;
            }
          case 'MouseCoordinates':
            {
              component = h(_mouseCoordinates2['default'], (0, _babelHelperVueJsxMergeProps2['default'])([{ props: props }, { on: on }]));
              break;
            }
          default:
            break;
        }
        _this3.controls[item.id] = component;
        return component;
      });
    },


    // 根据Id获取原始控件数据集的控件项
    getDataControlById: function getDataControlById(id) {
      var control = (0, _find2['default'])(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()]
    );
  }
};

exports['default'] = Controls;