import memorizeOne from 'memoize-one';

// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required
/**
 * @deprecated
 * This file is deprecated and will be removed in the next major release.
 * Please use `getBrowserInfo` function to get the browser info on demand.
 * Static `browser` info is no longer supported.
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var result = {
  mac: false,
  windows: false,
  ie: false,
  ie_version: 0,
  gecko: false,
  gecko_version: 0,
  chrome: false,
  chrome_version: 0,
  android: false,
  ios: false,
  webkit: false,
  safari: false,
  safari_version: 0,
  supportsIntersectionObserver: false,
  supportsResizeObserver: false
};
if (typeof navigator !== 'undefined') {
  var _userAgentData;
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  var ieEdge = /(?:Edge|Edg)\/(\d+)/.exec(navigator.userAgent);
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  var ieUpTo10 = /MSIE \d/.test(navigator.userAgent);
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  var ie11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent);

  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.mac = /Mac/.test(navigator.platform);
  result.windows =
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  ((_userAgentData = navigator.userAgentData) === null || _userAgentData === void 0 ? void 0 : _userAgentData.platform) === 'Windows' ||
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  /Windows|Win\d+/i.test(navigator.userAgent);
  var ie = result.ie = !!(ieUpTo10 || ie11up || ieEdge);
  result.ie_version = ieUpTo10 ?
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  document.documentMode || 6 : ie11up ? +ie11up[1] : ieEdge ? +ieEdge[1] : null;
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.gecko = !ie && /gecko\/\d/i.test(navigator.userAgent);
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.gecko_version = parseInt((navigator.userAgent.match(/Firefox\/(\d+)/) || [])[1], 10);

  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.chrome = !ie && /Chrome\//.test(navigator.userAgent);
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.chrome_version = parseInt((navigator.userAgent.match(/Chrome\/(\d+)/) || [])[1], 10);
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  result.android = /Android \d/.test(navigator.userAgent);
  result.ios =
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  !ie && /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);
  result.webkit = !ie && !!document.documentElement && 'WebkitAppearance' in document.documentElement.style;
  result.safari = Boolean(navigator.vendor && navigator.vendor.indexOf('Apple') > -1 && navigator.userAgent && navigator.userAgent.indexOf('CriOS') === -1 && navigator.userAgent.indexOf('FxiOS') === -1);
  result.safari_version = parseInt(
  // Ignored via go/ees005
  // eslint-disable-next-line require-unicode-regexp
  (navigator.userAgent.match(/Version\/([0-9\._]+).*Safari/) || [])[1], 10);
  result.supportsIntersectionObserver = typeof window !== 'undefined' && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window &&
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  'intersectionRatio' in window.IntersectionObserverEntry.prototype;
  result.supportsResizeObserver = typeof window !== 'undefined' && 'ResizeObserver' in window && 'ResizeObserverEntry' in window;
}

// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required
export { result as browser };
var getFirstMatch = function getFirstMatch(regexp, ua) {
  var match = ua.match(regexp);
  return match && match.length > 0 && match[1] || '';
};
var getSecondMatch = function getSecondMatch(regexp, ua) {
  var match = ua.match(regexp);
  return match && match.length > 0 && match[2] || '';
};

// Helper functions to safely access browser properties
var getSafeUserAgent = function getSafeUserAgent() {
  var _window$navigator;
  if (typeof window === 'undefined') {
    return undefined;
  }

  // Check for SSR user agent first
  if (process.env.REACT_SSR) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    var ssrUserAgent = window.__SSR_USER_AGENT__;
    if (ssrUserAgent) {
      return ssrUserAgent;
    }
  }

  // Fallback to navigator.userAgent with proper null checking
  return (_window$navigator = window.navigator) === null || _window$navigator === void 0 ? void 0 : _window$navigator.userAgent;
};
var getSafePlatform = function getSafePlatform() {
  var _window$navigator2;
  if (typeof window === 'undefined') {
    return undefined;
  }
  return (_window$navigator2 = window.navigator) === null || _window$navigator2 === void 0 ? void 0 : _window$navigator2.platform;
};
var hasIntersectionObserver = function hasIntersectionObserver() {
  return typeof window !== 'undefined' && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window &&
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  'intersectionRatio' in window.IntersectionObserverEntry.prototype;
};
var hasResizeObserver = function hasResizeObserver() {
  return typeof window !== 'undefined' && 'ResizeObserver' in window && 'ResizeObserverEntry' in window;
};

// New API to get the browser info on demand
export var getBrowserInfo = memorizeOne(function () {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  var result = {
    mac: false,
    windows: false,
    ie: false,
    ie_version: 0,
    gecko: false,
    gecko_version: 0,
    chrome: false,
    chrome_version: 0,
    android: false,
    ios: false,
    webkit: false,
    safari: false,
    safari_version: 0,
    supportsIntersectionObserver: false,
    supportsResizeObserver: false
  };
  var userAgent = getSafeUserAgent();
  var platform = getSafePlatform();
  if (userAgent) {
    // inspired from https://github.com/bowser-js/bowser/blob/master/src/parser-browsers.js
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    var _ieEdge = /\sedg\//i.test(userAgent);
    // eslint-disable-next-line require-unicode-regexp
    var ieEdge2 = /edg([ea]|ios)/i.test(userAgent);
    // eslint-disable-next-line require-unicode-regexp
    var internetExplorer = /msie|trident/i.test(userAgent);

    // Ideally we should use userAgent instead of platform, but we have lots of keymap tests failure when we change it
    // So leave it as is for now.
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.mac = platform && /Mac/.test(platform);

    // Previously relied on navigator.userAgentData?.platform and userAgent, now used only userAgent
    result.windows =
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    /Windows|Win\d+/i.test(userAgent);

    // used userAgent rather than relying on documentMode
    var _ie = _ieEdge || ieEdge2 || internetExplorer;
    result.ie = _ie;
    // inspired from https://github.com/bowser-js/bowser/blob/master/src/parser-browsers.js
    result.ie_version = _ieEdge ?
    // eslint-disable-next-line require-unicode-regexp
    parseInt(getFirstMatch(/\sedg\/(\d+(\.?_?\d+)+)/i, userAgent), 10) :
    // eslint-disable-next-line require-unicode-regexp
    ieEdge2 ?
    // eslint-disable-next-line require-unicode-regexp
    parseInt(getSecondMatch(/edg([ea]|ios)\/(\d+(\.?_?\d+)+)/i, userAgent), 10) :
    // eslint-disable-next-line require-unicode-regexp
    parseInt(getFirstMatch(/(?:msie |rv:)(\d+(\.?_?\d+)+)/i, userAgent), 10);

    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.gecko = !_ie && /gecko\/\d/i.test(userAgent);
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.gecko_version = parseInt((userAgent.match(/Firefox\/(\d+)/) || [])[1], 10);

    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.chrome = !_ie && /Chrome\//.test(userAgent);
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.chrome_version = parseInt((userAgent.match(/Chrome\/(\d+)/) || [])[1], 10);
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    result.android = /Android \d/.test(userAgent);
    result.ios =
    // Ignored via go/ees005
    // eslint-disable-next-line require-unicode-regexp
    !_ie && /AppleWebKit/.test(userAgent) && /Mobile\/\w+/.test(userAgent);

    // Previously relied on navigator.vendor, now used userAgent
    result.safari = !result.chrome && !result.ie && !result.gecko && !result.android && !userAgent.includes('CriOS') && !userAgent.includes('FxiOS') &&
    // eslint-disable-next-line require-unicode-regexp
    /safari|applewebkit/i.test(userAgent);
    result.safari_version = parseInt(
    // eslint-disable-next-line require-unicode-regexp
    (userAgent.match(/Version\/([0-9\._]+).*Safari/) || [])[1], 10);

    // Previously we relied on documentElement.style.WebkitAppearance, now changed to userAgent
    // eslint-disable-next-line require-unicode-regexp
    result.webkit = /(apple)?webkit/i.test(userAgent);
    result.supportsIntersectionObserver = hasIntersectionObserver();
    result.supportsResizeObserver = hasResizeObserver();
  }
  return result;
});