'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _hat = require('hat');

var _hat2 = _interopRequireDefault(_hat);

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

// Bing地图图层服务源的类型
var bingLayersTypes = exports.bingLayersTypes = ['vec'];

// Bing地图的限制地图Map属性
/**
 * @文件说明: Providers.Bing 必应Map数据源
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-23 19:03:27
 */

var bingMapOptions = exports.bingMapOptions = {
  minZoom: 1,
  maxZoom: 20,
  transformRequest: function transformRequest(url, resourceType) {
    if (resourceType === 'Tile' && url.startsWith('http://dynamic.t') && url.indexOf('tiles.ditu.live.com/comp/ch/') !== -1) {
      var format = url.match(/\{[\S\s]+\}/);
      // 提取x, y, z
      if (format && format[0]) {
        var xyz = format[0].replace(/\{/g, '').replace(/\}/g, '');
        xyz = xyz.split(',');
        var z = parseInt(xyz[2], 10);
        var x = parseInt(xyz[0], 10);
        var y = parseInt(xyz[1], 10) - 1;
        var idx = 0;
        var result = '';
        for (; idx < z; idx++) {
          result = ((x & 1) + 2 * (y & 1)).toString() + result;
          x >>= 1;
          y >>= 1;
        }
        // 替换xyz
        return {
          url: url.replace(format, result),
          credentials: 'same-origin'
        };
      }
    }
  }
};

// Bing地图瓦片服务地址
var bingTilesUrls = {
  vec: [0, 1, 2, 3].map(function (key) {
    return 'http://dynamic.t' + key + '.tiles.ditu.live.com/comp/ch/{{x},{y},{z}}?it=G,VE,BX,L,LA&mkt=zh-cn,syr&n=z&og=111&ur=CN';
  })
};

var Bing = function () {
  function Bing(iMapApi) {
    (0, _classCallCheck3['default'])(this, Bing);

    this.iMapApi = iMapApi;
    this.id = (0, _hat2['default'])();
  }

  /**
   * 获取Bing地图服务源的图层对象
   * @param {Object} options 服务源的参数选项
   */


  (0, _createClass3['default'])(Bing, [{
    key: 'getLayers',
    value: function getLayers() {
      var _this = this;

      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var layers = {};
      // 生成Bing对应类型的图层集合
      bingLayersTypes.map(function (key) {
        layers[key] = [];
        var layer = {
          id: 'providers_bing_' + key + '_' + _this.id,
          type: 'raster',
          source: {
            type: 'raster',
            tileSize: 256,
            minzoom: bingMapOptions.minZoom,
            maxzoom: bingMapOptions.maxZoom + 1,
            tiles: bingTilesUrls[key]
          },
          minzoom: bingMapOptions.minZoom,
          maxzoom: bingMapOptions.maxZoom + 1,
          metadata: {
            serviceType: 'XYZTile'
          }
        };
        layers[key].push(layer);
      });
      return layers;
    }
  }]);
  return Bing;
}();

exports['default'] = Bing;