"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _TMSSource2 = _interopRequireDefault(require("./TMSSource"));
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
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; } }
/**
 * @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 TMSSource}.
 *
 * @extends TMSSource
 *
 * @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} crs - The crs projection in which to fetch the data.
 * @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.
 * @property {Object} vendorSpecific - An object containing vendor specific
 * parameters. This object is read simply with the `key` being the name of the
 * parameter and `value` being the value of the parameter. If used, this
 * property should be set in the constructor parameters.
 *
 * @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 (_TMSSource) {
  (0, _inherits2["default"])(WMTSSource, _TMSSource);
  var _super = _createSuper(WMTSSource);
  /**
   * @param {Object} source - An object that can contain all properties of a
   * WMTSSource and {@link Source}. Only `url`, `name` and `crs` are mandatory.
   *
   * @constructor
   */
  function WMTSSource(source) {
    var _this;
    (0, _classCallCheck2["default"])(this, WMTSSource);
    if (!source.name) {
      throw new Error('New WMTSSource: name is required');
    }
    _this = _super.call(this, source);
    _this.isWMTSSource = true;

    // Add ? at the end of the url if it is not already in the given URL
    if (!_this.url.endsWith('?')) {
      _this.url = "".concat(_this.url, "?");
    }
    _this.url = "".concat(_this.url) + "LAYER=".concat(source.name) + "&FORMAT=".concat(_this.format) + '&SERVICE=WMTS' + "&VERSION=".concat(source.version || '1.0.0') + '&REQUEST=GetTile' + "&STYLE=".concat(source.style || 'normal') + "&TILEMATRIXSET=".concat(source.tileMatrixSet) + '&TILEMATRIX=%TILEMATRIX&TILEROW=%ROW&TILECOL=%COL';
    _this.vendorSpecific = source.vendorSpecific;
    for (var name in _this.vendorSpecific) {
      if (Object.prototype.hasOwnProperty.call(_this.vendorSpecific, name)) {
        _this.url = "".concat(_this.url, "&").concat(name, "=").concat(_this.vendorSpecific[name]);
      }
    }
    return _this;
  }
  return (0, _createClass2["default"])(WMTSSource);
}(_TMSSource2["default"]);
var _default = WMTSSource;
exports["default"] = _default;