"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _layer = require("ol/layer");
var _proj = require("ol/proj");
var _projzh = _interopRequireDefault(require("../../../../_util/projzh"));
var _source = require("ol/source");
var _TileGrid = _interopRequireDefault(require("ol/tilegrid/TileGrid"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
// 导入proj控件,使用其方法注入gcj02坐标系

var bd09Mercator = new _proj.Projection({
  code: 'BD-09',
  extent: [-20037726.37, -12474104.17, 20037726.37, 12474104.17],
  units: 'm'
});

// 将BD09墨卡托投影坐标系注册进OpenLayers
(0, _proj.addProjection)(bd09Mercator);

// 覆盖默认的转换方法
(0, _proj.addCoordinateTransforms)('EPSG:4326', bd09Mercator, _projzh["default"].ll2bmerc, _projzh["default"].bmerc2ll);
(0, _proj.addCoordinateTransforms)('EPSG:3857', bd09Mercator, _projzh["default"].smerc2bmerc, _projzh["default"].bmerc2smerc);
var getSource = function getSource() {
  var bmercResolutions = new Array(19);
  for (var i = 0; i < 19; ++i) {
    bmercResolutions[i] = Math.pow(2, 18 - i);
  }
  var urls = [0, 1, 2, 3].map(function (sub) {
    return 'http://maponline' + sub + '.bdimg.com/tile/?qt=vtile&x={x}&y={y}&z={z}&styles=pl&scaler=2&udt=20210506&from=jsapi3_0';
  });
  return new _source.XYZ({
    projection: 'BD-09',
    maxZoom: 18,
    crossOrigin: 'anonymous',
    tileUrlFunction: function tileUrlFunction(tileCoord) {
      var x = tileCoord[1];
      var y = -tileCoord[2] - 1;
      var z = tileCoord[0];
      var hash = (x << z) + y;
      var index = hash % urls.length;
      index = index < 0 ? index + urls.length : index;
      if (x < 0) {
        x = 'M' + -x;
      }
      if (y < 0) {
        y = 'M' + -y;
      }
      return urls[index].replace('{x}', x).replace('{y}', y).replace('{z}', z);
    },
    tilePixelRatio: 2,
    tileGrid: new _TileGrid["default"]({
      resolutions: bmercResolutions,
      origin: [0, 0]
    })
  });
};

// 获取百度底图
var getLayer = function getLayer(_ref) {
  var dark = _ref.dark;
  var mapLayer = new _layer.Tile({
    source: getSource(),
    className: dark && 'map-dark',
    zIndex: 0
  });
  return [mapLayer];
};

// 转换方法
var transform = function transform(coordinate) {
  return (0, _proj.transform)(coordinate, 'EPSG:4326', 'BD-09');
};
var _default = exports["default"] = {
  mercator: bd09Mercator,
  transform: transform,
  getSource: getSource,
  getLayer: getLayer
};
//# sourceMappingURL=BaiduImageryProvider.js.map
