/**
 * Copyright (c) 2022 International Business Group, Ant Group. All rights reserved.
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), the rights to use, copy, modify, merge, and/or distribute the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 1. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE; and
 * 2. If applicable, the use of the Software is also subject to the terms and conditions of any non-disclosure agreement signed by you and the relevant Ant Group entity.
 */
var hidden;
var visibilityChange;
var DEFAULT_TIMEOUT = 5000;
var timeStamp;
/**
 * @returns
 */
function getSupportedProperty(document) {
  if (typeof document === 'undefined') return;
  if (typeof document.hidden !== 'undefined') {
    hidden = 'hidden';
    visibilityChange = 'visibilitychange';
  } else if (typeof document.mozHidden !== 'undefined') {
    hidden = 'mozHidden';
    visibilityChange = 'mozvisibilitychange';
  } else if (typeof document.msHidden !== 'undefined') {
    hidden = 'msHidden';
    visibilityChange = 'msvisibilitychange';
  } else if (typeof document.webkitHidden !== 'undefined') {
    hidden = 'webkitHidden';
    visibilityChange = 'webkitvisibilitychange';
  }
}
getSupportedProperty(document);
/**
 * @returns
 */
function isPageHidden() {
  if (!hidden) return false;
  return document[hidden];
}
/**
 * @param fallback
 * @param timeout
 */
var timer;
export function checkCallAppSuccess(fallback, timeout) {
  timeStamp = +new Date();
  timer = setTimeout(function () {
    var pageHidden = isPageHidden();
    if (!pageHidden && fallback) {
      fallback();
    }
  }, timeout || DEFAULT_TIMEOUT);
}
export function addEventListener(options) {
  if (typeof visibilityChange !== 'undefined') {
    document.addEventListener(visibilityChange, function () {
      return handlePageHidden(options);
    });
  } else {
    window.addEventListener('pagehide', function () {
      return handlePageHidden(options);
    });
  }
}
function handlePageHidden(options) {
  var evokeAppBy = options.evokeAppBy;
  if (evokeAppBy === null || evokeAppBy === void 0 ? void 0 : evokeAppBy.url) {
    handleSuccessCb(options);
    clearTimer();
  }
}
/**
 * @param successCb
 */
function handleSuccessCb(_ref) {
  var evokeAppBy = _ref.evokeAppBy,
    successCb = _ref.successCb,
    timeout = _ref.timeout;
  if (successCb) {
    // hack
    var newTimeStamp = +new Date();
    var newTimeout = timeout || DEFAULT_TIMEOUT;
    var preTimeStamp = timeStamp || +new Date();
    if (newTimeStamp - preTimeStamp < newTimeout) {
      successCb(evokeAppBy);
    }
  }
}
function clearTimer() {
  if (timer) {
    clearTimeout(timer);
  }
}
/**
 * @param  {[type]}  url
 * @return {Boolean}     [description]
 */
export function checkIsHttpUrl(url) {
  return /^(http|https|itms-apps|itms-appss|market):\/\//.test(url);
}