"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 Cesium = _interopRequireWildcard(require("cesium"));
var _BaiduMercatorProjection = _interopRequireDefault(require("./BaiduMercatorProjection"));
var _CoordTransform = _interopRequireDefault(require("../transform/CoordTransform"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var BaiduMercatorTilingScheme = /*#__PURE__*/function (_Cesium$WebMercatorTi) {
  function BaiduMercatorTilingScheme(options) {
    var _this;
    _classCallCheck(this, BaiduMercatorTilingScheme);
    _this = _callSuper(this, BaiduMercatorTilingScheme, [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|*}
   */
  _inherits(BaiduMercatorTilingScheme, _Cesium$WebMercatorTi);
  return _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;
    }
  }]);
}(Cesium.WebMercatorTilingScheme);
var _default = exports["default"] = BaiduMercatorTilingScheme;
//# sourceMappingURL=BaiduMercatorTilingScheme.js.map
