import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
/**
 * @文件说明: Services.XYZTile XYZ瓦片服务
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-22 21:34:04
 */

import assign from 'lodash/assign';
import { isBooleanFlase, isArray } from '../../../_util/methods-util';
import { defaultServicesSourceOptions, defaultServicesLayerOptions, getServicesLayerSource, getServicesBaseLayer } from './index';

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

    this.iMapApi = iMapApi;
    this.exports = iMapApi && iMapApi.exports ? iMapApi.exports : {};
    this.proxyURL = iMapApi.proxyURL || '';
    this.options = _extends({}, defaultServicesSourceOptions, defaultServicesLayerOptions);
  }

  /**
   * 获取解析XYZTile服务的图层对象
   * @param {String} id 图层Id名称
   * @param {String} name 图层Name名
   * @param {String} url 服务地址
   * @param {Object} options 解析服务的参数选项
   */


  _createClass(XYZTile, [{
    key: 'getLayer',
    value: function getLayer(id) {
      var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';

      var _this = this;

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

      var opts = assign(this.options, options);
      // 获取图层对象
      var layer = getServicesBaseLayer(opts);
      layer.id = id;
      // 获取图层数据源
      var layerSource = getServicesLayerSource(opts);
      !layerSource.tileSize && (layerSource.tileSize = layerSource.defaultTileSize);
      // 实际服务源地址
      var isProxyUrl = isBooleanFlase(opts.proxy) ? false : true;
      var urls = isArray(url) ? url : [url];
      layerSource.tiles = urls.map(function (item) {
        return isProxyUrl ? '' + _this.proxyURL + item : item;
      });
      layer.source = layerSource;
      // 存储图层的服务类型
      layer.metadata.serviceType = 'XYZTile';
      layer.metadata.serviceName = name || '';
      return layer;
    }
  }]);

  return XYZTile;
}();

export default XYZTile;