"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
var _getToken = require("../_util/getToken");
var _excluded = ["request", "format", "exceptions", "width", "height"];
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); }
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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, 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); }
var getUrlAndRectangle = function getUrlAndRectangle(options) {
  var map = options.map,
    token = options.token,
    params = options.params;
  var rectangle = map.camera.computeViewRectangle();

  //地理坐标（弧度）转经纬度坐标
  var extent = [rectangle.west / Math.PI * 180, rectangle.south / Math.PI * 180, rectangle.east / Math.PI * 180, rectangle.north / Math.PI * 180];
  var _params = _objectSpread({
    service: 'WMS',
    version: '1.1.1',
    request: 'GetMap',
    format: 'image/png',
    transparent: 'true',
    styles: '',
    layers: 'zhsw:putian_water_supply_line',
    exceptions: 'application/vnd.ogc.se_inimage',
    srs: 'EPSG:4326',
    width: parseInt(map._container.clientWidth),
    height: parseInt(map._container.clientHeight),
    bbox: extent.join(',')
  }, params || {});
  var url = (0, _getToken.getResource)(options.url || '/geoserver/wms', {
    token: token,
    params: _objectSpread({}, _params)
  });
  var request = _params.request,
    format = _params.format,
    exceptions = _params.exceptions,
    width = _params.width,
    height = _params.height,
    infoParameters = _objectWithoutProperties(_params, _excluded);
  return {
    url: url,
    rectangle: rectangle,
    getFeatureInfoParameters: _objectSpread(_objectSpread({}, infoParameters), {}, {
      width: 256,
      height: 256,
      request: 'GetFeatureInfo',
      info_format: 'application/json'
    })
  };
};
var getLayer = function getLayer(options) {
  return new _cesium.ImageryLayer(new _cesium.SingleTileImageryProvider(getUrlAndRectangle(options)));
};
var ImageLayer = /*#__PURE__*/function () {
  function ImageLayer(options) {
    var _this = this;
    _classCallCheck(this, ImageLayer);
    var map = options.map,
      token = options.token,
      zIndex = options.zIndex,
      params = options.params;
    this.map = map;
    this.token = token;
    this.params = params;
    this.layer = getLayer(options);
    this.getFeatureInfo;
    this.zIndex = map.getImageryLastIndex(zIndex);
    map.addLayer(this.layer, this.zIndex);

    // 添加相机监听事件
    map.camera.moveEnd.addEventListener(function () {
      _this.replaceLayer();
      // this.updateLayer();
    });

    // const {url, rectangle } = getUrlAndRectangle(options)
    // this.entity = map.entities.add({
    //     name: "Rotating rectangle with rotating texture coordinate",
    //     rectangle: {
    //         coordinates: rectangle,
    //         material: url,
    //         classificationType: ClassificationType.TERRAIN,
    //     },
    // });
  }
  return _createClass(ImageLayer, [{
    key: "updateLayer",
    value: function updateLayer() {
      var _getUrlAndRectangle = getUrlAndRectangle({
          map: this.map,
          token: this.token,
          params: this.params
        }),
        url = _getUrlAndRectangle.url,
        rectangle = _getUrlAndRectangle.rectangle;
      this.entity.rectangle.material._image.setValue(url);
      this.entity.rectangle.coordinates.setValue(rectangle);
    }
  }, {
    key: "replaceLayer",
    value: function replaceLayer() {
      var _this2 = this;
      var layer = getLayer({
        map: this.map,
        token: this.token,
        params: this.params
      });
      this.map.addLayer(layer, this.zIndex);
      layer.imageryProvider.readyPromise.then(function () {
        _this2.map.removeLayer(_this2.layer);
        _this2.layer = layer;
      });
    }
  }]);
}();
var _default = exports["default"] = ImageLayer;
//# sourceMappingURL=ImageWmsLayer.js.map
