"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 _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

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

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

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

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

var _Extent = require("../Core/Geographic/Extent");

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

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 { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

/**
 * @classdesc
 * An object defining the source of resources to get from a [TMS]{@link
 * https://wiki.osgeo.org/wiki/Tile_Map_Service_Specification} server. It
 * inherits from {@link Source}.
 *
 * @extends Source
 *
 * @property {boolean} isTMSSource - Used to checkout whether this source is a
 * TMSSource. Default is true. You should not change this, as it is used
 * internally for optimisation.
 * @property {boolean} isInverted - The isInverted property is to be set to the
 * correct value, true or false (default being false) if the computation of the
 * coordinates needs to be inverted to match the same scheme as OSM, Google Maps
 * or other system. See [this link]{@link
 * https://alastaira.wordpress.com/2011/07/06/converting-tms-tile-coordinates-to-googlebingosm-tile-coordinates/}
 * for more information.
 * @property {string} tileMatrixSet - Tile matrix set of the layer, used in the
 * generation of the coordinates to build the url. Default value is 'WGS84'.
 * @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 0.
 * @property {number} zoom.max - The maximum level of the source. Default value
 * is 20.
 *
 * @example
 * // Create the source
 * const tmsSource = new itowns.TMSSource({
 *     format: 'image/png',
 *     url: 'http://osm.io/styles/${z}/${x}/${y}.png',
 *     attribution: {
 *         name: 'OpenStreetMap',
 *         url: 'http://www.openstreetmap.org/',
 *     },
 *     tileMatrixSet: 'PM',
 * });
 *
 * // Create the layer
 * const colorLayer = new itowns.ColorLayer('OPENSM', {
 *     source: tmsSource,
 * });
 *
 * // Add the layer
 * view.addLayer(colorLayer);
 */
var TMSSource = /*#__PURE__*/function (_Source) {
  (0, _inherits2["default"])(TMSSource, _Source);

  var _super = _createSuper(TMSSource);

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

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

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

    _this = _super.call(this, source);
    _this.isTMSSource = true;

    if (!source.extent) {
      // default to the global extent
      _this.extent = _Extent.globalExtentTMS.get(source.projection);
    }

    _this.zoom = source.zoom;
    _this.isInverted = source.isInverted || false;
    _this.format = _this.format || 'image/png';
    _this.url = source.url;
    _this.projection = _Crs["default"].formatToTms(source.projection);
    _this.tileMatrixSetLimits = source.tileMatrixSetLimits;

    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: 0,
          max: 20
        };
      }
    }

    return _this;
  }

  (0, _createClass2["default"])(TMSSource, [{
    key: "urlFromExtent",
    value: function urlFromExtent(extent) {
      return _URLBuilder["default"].xyz(extent, this);
    }
  }, {
    key: "handlingError",
    value: function handlingError(err) {
      console.warn("err ".concat(this.url), err);
    }
  }, {
    key: "extentInsideLimit",
    value: function extentInsideLimit(extent) {
      // This layer provides data starting at level = layer.source.zoom.min
      // (the zoom.max property is used when building the url to make
      //  sure we don't use invalid levels)
      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 TMSSource;
}(_Source2["default"]);

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