"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 _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 _Fetcher = _interopRequireDefault(require("../Provider/Fetcher"));

var _C3DTilesLayer = require("../Layer/C3DTilesLayer");

function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function () {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { 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; } }

var C3DTilesSource = /*#__PURE__*/function (_Source) {
  (0, _inherits2["default"])(C3DTilesSource, _Source);

  var _super = _createSuper(C3DTilesSource);

  function C3DTilesSource(source) {
    var _this;

    (0, _classCallCheck2["default"])(this, C3DTilesSource);
    _this = _super.call(this, source);
    _this.baseUrl = _this.url.slice(0, _this.url.lastIndexOf('/') + 1);
    _this.whenReady = _Fetcher["default"].json(_this.url, _this.networkOptions).then(function (tileset) {
      // Verify that extensions of the tileset have been registered to
      // $3dTilesExtensions
      if (tileset.extensionsUsed) {
        var _iterator = _createForOfIteratorHelper(tileset.extensionsUsed),
            _step;

        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var extensionUsed = _step.value;

            // if current extension is not registered
            if (!_C3DTilesLayer.$3dTilesExtensions.isExtensionRegistered(extensionUsed)) {
              if (tileset.extensionsRequired && tileset.extensionsRequired.includes(extensionUsed)) {
                console.error("3D Tiles tileset required extension \"".concat(extensionUsed, "\" must be registered to $3dTilesExtensions global object of iTowns to be parsed and used."));
              } else {
                console.warn("3D Tiles tileset used extension \"".concat(extensionUsed, "\" must be registered to $3dTilesExtensions global object of iTowns to be parsed and used."));
              }
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      }

      return tileset;
    });
    return _this;
  }

  return C3DTilesSource;
}(_Source2["default"]);

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