"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _Source2 = _interopRequireDefault(require("./Source"));

var _URLBuilder = _interopRequireDefault(require("../Provider/URLBuilder"));

var _Crs = _interopRequireDefault(require("../Core/Geographic/Crs"));

/**
 * @classdesc
 * An object defining the source of resources to get from a
 * [WMTS]{@link http://www.opengeospatial.org/standards/wmts} server. It inherits
 * from {@link Source}.
 *
 * @extends Source
 *
 * @property {boolean} isWMTSSource - Used to checkout whether this source is a
 * WMTSSource. Default is true. You should not change this, as it is used
 * internally for optimisation.
 * @property {string} name - The name of the layer, used in the generation of
 * the url.
 * @property {string} version - The version of the WMTS server to request on.
 * Default value is '1.0.0'.
 * @property {string} style - The style to query on the WMTS server. Default
 * value is 'normal'.
 * @property {string} projection - The projection in which to fetch the data. If
 * not specified, it is deduced from `tileMatrixSet`. Default value is
 * 'EPSG:3857'.
 * @property {string} tileMatrixSet - Tile matrix set of the layer, used in the
 * generation of the url. Default value is 'WGS84'.
 * @property {Object} tileMatrixSetLimits - Limits of the tile matrix set. Each
 * limit has for key its level number, and their properties are the
 * `minTileRow`, `maxTileRow`, `minTileCol` and `maxTileCol`.
 * @property {number} tileMatrixSetLimits.minTileRow - Minimum row for tiles at
 * the specified level.
 * @property {number} tileMatrixSetLimits.maxTileRow - Maximum row for tiles at
 * the specified level.
 * @property {number} tileMatrixSetLimits.minTileCol - Minimum column for tiles
 * at the specified level.
 * @property {number} tileMatrixSetLimits.maxTileCol - Maximum column for tiles
 * at the specified level.
 * @property {Object} zoom - Object containing the minimum and maximum values of
 * the level, to zoom in the source.
 * @property {number} zoom.min - The minimum level of the source. Default value
 * is 2.
 * @property {number} zoom.max - The maximum level of the source. Default value
 * is 20.
 *
 * @example
 * // Create the source
 * const wmtsSource = new itowns.WMTSSource({
 *     name: 'DARK',
 *     tileMatrixSet: 'PM',
 *     url: 'http://server.geo/wmts',
 *     format: 'image/jpg',
 * });
 *
 * // Create the layer
 * const colorLayer = new itowns.ColorLayer('darkmap', {
 *     source: wmtsSource,
 * });
 *
 * // Add the layer
 * view.addLayer(colorLayer);
 */
var WMTSSource =
/*#__PURE__*/
function (_Source) {
  (0, _inherits2["default"])(WMTSSource, _Source);

  /**
   * @param {Object} source - An object that can contain all properties of a
   * WMTSSource and {@link Source}. Only `url` and `name` are mandatory.
   *
   * @constructor
   */
  function WMTSSource(source) {
    var _this;

    (0, _classCallCheck2["default"])(this, WMTSSource);

    if (!source.name) {
      throw new Error('New WMTSSource: name is required');
    }

    if (!source.projection) {
      throw new Error('New WMTSSource: projection is required');
    }

    _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(WMTSSource).call(this, source));
    _this.isWMTSSource = true;
    _this.format = _this.format || 'image/png';
    _this.version = source.version || '1.0.0';
    _this.tileMatrixSet = source.tileMatrixSet || 'WGS84';
    _this.style = source.style || 'normal';
    _this.name = source.name;
    _this.url = "".concat(_this.url) + "?LAYER=".concat(_this.name) + "&FORMAT=".concat(_this.format) + '&SERVICE=WMTS' + "&VERSION=".concat(_this.version) + '&REQUEST=GetTile' + "&STYLE=".concat(_this.style) + "&TILEMATRIXSET=".concat(_this.tileMatrixSet) + '&TILEMATRIX=%TILEMATRIX&TILEROW=%ROW&TILECOL=%COL';
    _this.zoom = source.zoom;
    _this.tileMatrixSetLimits = source.tileMatrixSetLimits;
    _this.projection = _Crs["default"].formatToTms(source.projection);

    if (!_this.zoom) {
      if (_this.tileMatrixSetLimits) {
        var arrayLimits = Object.keys(_this.tileMatrixSetLimits);
        var size = arrayLimits.length;
        var maxZoom = Number(arrayLimits[size - 1]);
        _this.zoom = {
          min: maxZoom - size + 1,
          max: maxZoom
        };
      } else {
        _this.zoom = {
          min: 2,
          max: 20
        };
      }
    }

    return _this;
  }

  (0, _createClass2["default"])(WMTSSource, [{
    key: "urlFromExtent",
    value: function urlFromExtent(extent) {
      return _URLBuilder["default"].xyz(extent, this);
    }
  }, {
    key: "extentInsideLimit",
    value: function extentInsideLimit(extent) {
      return extent.zoom >= this.zoom.min && extent.zoom <= this.zoom.max && (this.tileMatrixSetLimits == undefined || extent.row >= this.tileMatrixSetLimits[extent.zoom].minTileRow && extent.row <= this.tileMatrixSetLimits[extent.zoom].maxTileRow && extent.col >= this.tileMatrixSetLimits[extent.zoom].minTileCol && extent.col <= this.tileMatrixSetLimits[extent.zoom].maxTileCol);
    }
  }]);
  return WMTSSource;
}(_Source2["default"]);

var _default = WMTSSource;
exports["default"] = _default;