'use strict';

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

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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

var _createClass3 = _interopRequireDefault(_createClass2);

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

var _extends3 = _interopRequireDefault(_extends2);

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

var _assign2 = _interopRequireDefault(_assign);

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

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

// Mapbox地图图层服务源的类型
/**
 * @文件说明: Providers.Mapbox 原生Mapbox数据源
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-23 22:05:31
 */

var mapboxLayersTypes = exports.mapboxLayersTypes = ['vec'];

// Mapbox地图的限制地图Map属性
var mapboxMapOptions = exports.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;
    (0, _methodsUtil.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 = (0, _assign2['default'])({}, item, {
          metadata: (0, _assign2['default'])({}, item.metadata || {}, metadata)
        });
        var layerSource = (0, _assign2['default'])({}, layer);
        // 替换数据源
        if (layerSource.source) {
          layerSource.source = (0, _extends3['default'])({
            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) {
    (0, _classCallCheck3['default'])(this, Mapbox);

    this.iMapApi = iMapApi;
  }

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


  (0, _createClass3['default'])(Mapbox, [{
    key: 'getLayers',
    value: function () {
      var _ref = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee() {
        var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        var opts;
        return _regenerator2['default'].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                opts = (0, _assign2['default'])({}, 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;
}();

exports['default'] = Mapbox;