function isAppleArm() {
    /*
    based on: https://stackoverflow.com/questions/65146751/detecting-apple-silicon-mac-in-javascript

    iMac Intel
    -> AMD Radeon Pro 570 OpenGL Engine
    -> AMD Radeon HD 6770M OpenGL Engine

    MacBook M1
    -> Safari: Apple GPU
    -> Firefox: Apple M1
    */
    
    var w = document.createElement('canvas').getContext('webgl')
    var d = w.getExtension('WEBGL_debug_renderer_info')
    var g = d && w.getParameter(d.UNMASKED_RENDERER_WEBGL) || ''

    if (g.match(/Apple GPU/) || g.match(/Apple M1/)) {
      return true
    } else {
      return false
    }
}

function openOverlayWhileDownloading() {
  const modal = new bootstrap.Modal('#overlay-while-downloading')
  modal.show()
}

function closeOverlayWhileDownloading() {
  const modal = new bootstrap.Modal('#overlay-while-downloading')
  modal.hide()
}

async function openCheckoutOverlay(productId, language, message, oldSerial, couponCode) {
  let displayModeTheme = 'light'
  if (window.matchMedia) {
    if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
      displayModeTheme = 'dark'
      displayModeTheme = 'light'
    }
  }

  if (productId === 600045 || productId === 600082 || productId === 518944 || productId === 641751 || productId === 642254) { /* MAMP PRO 5 || MAMP PRO Update 4 -> 5 || MAMP Cloud || NAMO 2 || NAMO 2 Update */
    /*if (message === '') {
      if (language === 'de') {
        message = '25% Rabatt -> Gutscheincode "MAMP-Friday"'
      } else {
        message = 'Get 25% -> coupon code "MAMP-Friday"'
      }
    }*/

    Paddle.Checkout.open({
      displayModeTheme: displayModeTheme,
      product: productId,
      passthrough: {"site-language":language},
      message: message/*,
      coupon: 'MAMP-Friday'*/
    })
  } else {
    const url = '/js/mamppro-generate-pay-link.ajax.php'

    const data = {
      product_id: productId,
      old_serial: oldSerial,
      coupon_code: couponCode,
      language: language
    }

    fetch(url, {
      method: 'POST',
      cache: 'no-cache',
      credentials: 'same-origin',
      redirect: 'follow',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    }).then(res => res.json())
    .then(json => {
      if (json.pay_link.length !== 0) {
        Paddle.Checkout.open({
          displayModeTheme: displayModeTheme,
          override: json.pay_link
        })
      } else {
        showDialog(((language === 'de') ? 'Fehler' : 'Error'), json.error)
      }
    })
  }
}

function isMac() {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return userAgent.includes('macintosh') || userAgent.includes('mac os');
}

function isWindows() {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return userAgent.includes('windows');
}

/* ---------- DIALOG ---------- */
function showDialog(headline, text) {
  const target = document.getElementById('dialog')

  if (typeof target.showModal === 'function') {
    target.classList.remove('d-none')
    target.querySelector('h1').textContent = headline
    target.querySelector('p').textContent = text
    target.showModal()
  } else {
    alert(headline+"\n\n"+text)
  }
}

function closeDialog() {
  const target = document.getElementById('dialog')

  if (typeof target.close === 'function') {
    target.close()
  }
}

const dialogFooterBotton = document.querySelector('#dialog footer button')
if (dialogFooterBotton !== null) {
  dialogFooterBotton.addEventListener('click', () => {
    closeDialog()
  })
}
/* ---------- /DIALOG ---------- */