const navigator = window.navigator;
const userAgent = navigator.userAgent;

const normalizedUserAgent = userAgent.toLowerCase();

const isIos =
  /ip(ad|hone|od)/.test(normalizedUserAgent) ||
  (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
const isAndroid = /android/.test(normalizedUserAgent);

export const isSafari = () => /^((?!chrome|android).)*safari/i.test(userAgent);
export const isFirefox = () => userAgent.indexOf('Firefox') !== -1;
export const isWebview =
  (isAndroid && /; wv\)/.test(normalizedUserAgent)) || isIos;

export const getClientOs = () => {
  // userAgentData is the new API, however currently is NOT supported in Safari and Firefox
  // @ts-ignore - TS2304: Cannot find name 'userAgentData'
  const agent = navigator.userAgentData?.platform || userAgent;

  if (isAndroid) {
    return 'Android';
  }
  if (isIos) {
    return 'iOS';
  }
  if (agent.indexOf('Windows') !== -1) {
    return 'Windows';
  }
  if (/(mac|macos)/i.test(agent)) {
    return 'MacOS';
  }
  if (agent.indexOf('Linux') !== -1) {
    return 'Linux';
  }
};
