"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _BaiduMercatorProjection = _interopRequireDefault(require("./BaiduMercatorProjection"));
var _CoordTransform = _interopRequireDefault(require("../transform/CoordTransform"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var BaiduMercatorTilingScheme = /*#__PURE__*/function (_Cesium$WebMercatorTi) {
  _inherits(BaiduMercatorTilingScheme, _Cesium$WebMercatorTi);
  var _super = _createSuper(BaiduMercatorTilingScheme);
  function BaiduMercatorTilingScheme(options) {
    var _this;
    _classCallCheck(this, BaiduMercatorTilingScheme);
    _this = _super.call(this, options);
    var projection = new _BaiduMercatorProjection["default"]();
    _this._projection.project = function (cartographic, result) {
      result = result || {};
      result = _CoordTransform["default"].WGS84ToGCJ02(Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude));
      result = _CoordTransform["default"].GCJ02ToBD09(result[0], result[1]);
      result[0] = Math.min(result[0], 180);
      result[0] = Math.max(result[0], -180);
      result[1] = Math.min(result[1], 74.000022);
      result[1] = Math.max(result[1], -71.988531);
      result = projection.lngLatToPoint({
        lng: result[0],
        lat: result[1]
      });
      return new Cesium.Cartesian2(result.x, result.y);
    };
    _this._projection.unproject = function (cartesian, result) {
      result = result || {};
      result = projection.mercatorToLngLat({
        lng: cartesian.x,
        lat: cartesian.y
      });
      result = _CoordTransform["default"].BD09ToGCJ02(result.lng, result.lat);
      result = _CoordTransform["default"].GCJ02ToWGS84(result[0], result[1]);
      return new Cesium.Cartographic(Cesium.Math.toRadians(result[0]), Cesium.Math.toRadians(result[1]));
    };
    _this.resolutions = options.resolutions || [];
    return _this;
  }

  /**
   *
   * @param x
   * @param y
   * @param level
   * @param result
   * @returns {module:cesium.Rectangle|*}
   */
  _createClass(BaiduMercatorTilingScheme, [{
    key: "tileXYToNativeRectangle",
    value: function tileXYToNativeRectangle(x, y, level, result) {
      var tileWidth = this.resolutions[level];
      var west = x * tileWidth;
      var east = (x + 1) * tileWidth;
      var north = ((y = -y) + 1) * tileWidth;
      var south = y * tileWidth;
      if (!Cesium.defined(result)) {
        return new Cesium.Rectangle(west, south, east, north);
      }
      result.west = west;
      result.south = south;
      result.east = east;
      result.north = north;
      return result;
    }

    /**
     *
     * @param position
     * @param level
     * @param result
     * @returns {undefined|*}
     */
  }, {
    key: "positionToTileXY",
    value: function positionToTileXY(position, level, result) {
      var rectangle = this._rectangle;
      if (!Cesium.Rectangle.contains(rectangle, position)) {
        return undefined;
      }
      var projection = this._projection;
      var webMercatorPosition = projection.project(position);
      if (!Cesium.defined(webMercatorPosition)) {
        return undefined;
      }
      var tileWidth = this.resolutions[level];
      var xTileCoordinate = Math.floor(webMercatorPosition.x / tileWidth);
      var yTileCoordinate = -Math.floor(webMercatorPosition.y / tileWidth);
      if (!Cesium.defined(result)) {
        return new Cesium.Cartesian2(xTileCoordinate, yTileCoordinate);
      }
      result.x = xTileCoordinate;
      result.y = yTileCoordinate;
      return result;
    }
  }]);
  return BaiduMercatorTilingScheme;
}(Cesium.WebMercatorTilingScheme);
var _default = BaiduMercatorTilingScheme;
exports["default"] = _default;
//# sourceMappingURL=BaiduMercatorTilingScheme.js.map
