---
const releasePageUrl = 'https://github.com/respawn-llc/kent/releases/latest';
---

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="robots" content="noindex" />
    <title>Downloading Kent Desktop | Kent</title>
  </head>
  <body>
    <main>
      <h1>Downloading Kent Desktop</h1>
      <p id="status">Resolving the latest desktop download...</p>
      <p>
        If the download does not start, open the
        <a href={releasePageUrl}>latest Kent release</a>.
      </p>
    </main>
    <script>
      const statusElement = document.getElementById('status');
      const releasePageUrl = 'https://github.com/respawn-llc/kent/releases/latest';
      const desktopAssets = [
        {
          name: 'macOS Apple Silicon',
          pattern: /^Kent_\d+\.\d+\.\d+_aarch64\.dmg$/,
          platform: 'macos-arm64',
        },
        {
          name: 'Linux x86_64 AppImage',
          pattern: /^Kent_\d+\.\d+\.\d+_amd64\.AppImage$/,
          platform: 'linux',
        },
      ];

      async function detectDesktopPlatform() {
        const platform = [
          navigator.userAgentData?.platform,
          navigator.platform,
          navigator.userAgent,
        ]
          .filter(Boolean)
          .join(' ')
          .toLowerCase();

        if (platform.includes('mac')) {
          const architecture = await detectArchitecture();
          return architecture === 'arm64' ? 'macos-arm64' : 'unsupported';
        }
        if (platform.includes('linux') || platform.includes('x11')) {
          return 'linux';
        }
        return 'unsupported';
      }

      async function detectArchitecture() {
        if (!navigator.userAgentData?.getHighEntropyValues) {
          return 'unknown';
        }

        const values = await navigator.userAgentData.getHighEntropyValues(['architecture']);
        const architecture = values.architecture?.toLowerCase();
        if (architecture === 'arm' || architecture === 'arm64' || architecture === 'aarch64') {
          return 'arm64';
        }
        if (architecture === 'x86' || architecture === 'x86_64' || architecture === 'amd64') {
          return 'x64';
        }
        return 'unknown';
      }

      async function redirectToLatestDesktopDownload() {
        const desktopPlatform = await detectDesktopPlatform();
        const assetRule = desktopAssets.find((candidate) => candidate.platform === desktopPlatform);
        if (!assetRule) {
          throw new Error(`Kent Desktop does not publish an automatic download for ${desktopPlatform}`);
        }

        const response = await fetch('https://api.github.com/repos/respawn-llc/kent/releases/latest', {
          headers: { Accept: 'application/vnd.github+json' },
        });
        if (!response.ok) {
          throw new Error(`GitHub release lookup failed with HTTP ${response.status}`);
        }

        const release = await response.json();
        const asset = release.assets?.find((candidate) => assetRule.pattern.test(candidate.name ?? ''));
        if (!asset?.browser_download_url) {
          throw new Error(`Latest Kent release does not contain ${assetRule.name}`);
        }

        window.location.replace(asset.browser_download_url);
      }

      redirectToLatestDesktopDownload().catch((error) => {
        console.error(error);
        if (statusElement) {
          statusElement.textContent = 'Could not start the download automatically.';
        }
        window.location.assign(releasePageUrl);
      });
    </script>
  </body>
</html>
