import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _extends from 'babel-runtime/helpers/extends';
/**
 * @文件说明: Providers.Mapbox 原生Mapbox数据源
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-23 22:05:31
 */

import assign from 'lodash/assign';
import { fetchGetJson } from '../../_util/methods-util';

// Mapbox地图图层服务源的类型
export var mapboxLayersTypes = ['vec'];

// Mapbox地图的限制地图Map属性
export var mapboxMapOptions = {
  mapCRS: null
};

// 默认服务数据源的参数选项
var DEFAULT_OPTIONS = {
  // Mapbox地图的样式地址
  style: null,
  // Mapbox地图的Token密钥
  accessToken: null
};

/**
 * 解析Mapbox服务的服务地址获取图层、样式等信息
 * @param {String} style Mapbox地图的样式链接
 * @param {String} accessToken Mapbox地图的token密钥
 */
var fetchMapboxService = function fetchMapboxService(style, accessToken) {
  return new Promise(function (resolve, reject) {
    // 转换真实的服务信息地址
    var url = 'https://api.mapbox.com/' + style.replace(/mapbox:\/\/styles\//g, 'styles/v1/') + '?access_token=' + accessToken;
    fetchGetJson(url).then(function (data) {
      var layers = [];
      var layersSource = [];
      data && data.layers && data.layers.map(function (item) {
        // 拼接必要的metadata属性
        var metadata = { serviceType: 'VTS' };
        metadata.visibility = item.layout && item.layout.visibility && item.layout.visibility === 'none' ? 'none' : 'visible';
        var layer = assign({}, item, {
          metadata: assign({}, item.metadata || {}, metadata)
        });
        var layerSource = assign({}, layer);
        // 替换数据源
        if (layerSource.source) {
          layerSource.source = _extends({
            id: layerSource.source
          }, data.sources[layerSource.source]);
        }
        layers.push(layer);
        layersSource.push(layerSource);
      });
      data.layers = layers;
      resolve({ style: data, layers: { vec: layersSource } });
    })['catch'](function () {
      console.error('解析Mapbox样式服务失败，请检查[style]或[accessToken]属性是否有效.');
      reject();
    });
  });
};

var Mapbox = function () {
  function Mapbox(iMapApi) {
    _classCallCheck(this, Mapbox);

    this.iMapApi = iMapApi;
  }

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


  _createClass(Mapbox, [{
    key: 'getLayers',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        var opts;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                opts = assign({}, DEFAULT_OPTIONS, options);

                if (!(!opts.style || !opts.accessToken || !GeoGlobe)) {
                  _context.next = 4;
                  break;
                }

                console.error('渲染Mapbox地图的[style]、[accessToken]必填项属性不可缺少.');
                return _context.abrupt('return', null);

              case 4:
                _context.next = 6;
                return fetchMapboxService(opts.style, opts.accessToken, GeoGlobe);

              case 6:
                return _context.abrupt('return', _context.sent);

              case 7:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function getLayers() {
        return _ref.apply(this, arguments);
      }

      return getLayers;
    }()
  }]);

  return Mapbox;
}();

export default Mapbox;