"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const constants = require("./constants.js");
/* @license
 * Copyright 2019 Google LLC. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const deserializeUrl = (url) => !!url && url !== "null" ? toFullUrl(url) : null;
const assertIsArCandidate = () => {
  if (constants.IS_WEBXR_AR_CANDIDATE) {
    return;
  }
  const missingApis = [];
  if (!constants.HAS_WEBXR_DEVICE_API) {
    missingApis.push("WebXR Device API");
  }
  if (!constants.HAS_WEBXR_HIT_TEST_API) {
    missingApis.push("WebXR Hit Test API");
  }
  throw new Error(`The following APIs are required for AR, but are missing in this browser: ${missingApis.join(", ")}`);
};
const toFullUrl = (partialUrl) => {
  const url = new URL(partialUrl, window.location.toString());
  return url.toString();
};
const throttle = (fn, ms) => {
  let timer = null;
  const throttled = (...args) => {
    if (timer != null) {
      return;
    }
    fn(...args);
    timer = self.setTimeout(() => timer = null, ms);
  };
  throttled.flush = () => {
    if (timer != null) {
      self.clearTimeout(timer);
      timer = null;
    }
  };
  return throttled;
};
const debounce = (fn, ms) => {
  let timer = null;
  return (...args) => {
    if (timer != null) {
      self.clearTimeout(timer);
    }
    timer = self.setTimeout(() => {
      timer = null;
      fn(...args);
    }, ms);
  };
};
const clamp = (value, lowerLimit, upperLimit) => Math.max(lowerLimit, Math.min(upperLimit, value));
const CAPPED_DEVICE_PIXEL_RATIO = 1;
const resolveDpr = (() => {
  const HAS_META_VIEWPORT_TAG = (() => {
    var _a;
    if ((_a = document.documentElement.getAttribute("itemtype")) === null || _a === void 0 ? void 0 : _a.includes("schema.org/SearchResultsPage")) {
      return true;
    }
    const metas = document.head != null ? Array.from(document.head.querySelectorAll("meta")) : [];
    for (const meta of metas) {
      if (meta.name === "viewport") {
        return true;
      }
    }
    return false;
  })();
  if (!HAS_META_VIEWPORT_TAG) {
    console.warn('No <meta name="viewport"> detected; <model-viewer> will cap pixel density at 1.');
  }
  return () => HAS_META_VIEWPORT_TAG ? window.devicePixelRatio : CAPPED_DEVICE_PIXEL_RATIO;
})();
const isDebugMode = (() => {
  const debugQueryParameterName = "model-viewer-debug-mode";
  const debugQueryParameter = new RegExp(`[?&]${debugQueryParameterName}(&|$)`);
  return () => self.ModelViewerElement && self.ModelViewerElement.debugMode || self.location && self.location.search && self.location.search.match(debugQueryParameter);
})();
const timePasses = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
const waitForEvent = (target, eventName, predicate = null) => new Promise((resolve) => {
  function handler(event) {
    if (!predicate || predicate(event)) {
      resolve(event);
      target.removeEventListener(eventName, handler);
    }
  }
  target.addEventListener(eventName, handler);
});
exports.CAPPED_DEVICE_PIXEL_RATIO = CAPPED_DEVICE_PIXEL_RATIO;
exports.assertIsArCandidate = assertIsArCandidate;
exports.clamp = clamp;
exports.debounce = debounce;
exports.deserializeUrl = deserializeUrl;
exports.isDebugMode = isDebugMode;
exports.resolveDpr = resolveDpr;
exports.throttle = throttle;
exports.timePasses = timePasses;
exports.toFullUrl = toFullUrl;
exports.waitForEvent = waitForEvent;
//# sourceMappingURL=utilities.js.map
