---
// Copyright © onWidget <https://github.com/onwidget>
// SPDX-License-Identifier: MIT
import { UI } from "astrowind:config";
---

<script is:inline define:vars={{ defaultTheme: UI.theme || 'system' }}>
  function applyTheme(theme) {
    document.documentElement.setAttribute('data-theme-transitioning', 'true');
    if (theme === 'dark') {
      document.documentElement.classList.add('dark');
      document.documentElement.dataset.theme = 'dark';
    } else {
      document.documentElement.classList.remove('dark');
      document.documentElement.dataset.theme = 'light';
    }
    const matches = document.querySelectorAll('[data-aw-toggle-color-scheme] > input');

    if (matches && matches.length) {
      matches.forEach((elem) => {
        elem.checked = theme !== 'dark';
      });
    }
    setTimeout(() => {
      document.documentElement.removeAttribute('data-theme-transitioning');
    }, 50);
  }

  if ((defaultTheme && defaultTheme.endsWith(':only')) || (!localStorage.theme && defaultTheme !== 'system')) {
    applyTheme(defaultTheme.replace(':only', ''));
  } else if (
    localStorage.theme === 'dark' ||
    (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)
  ) {
    applyTheme('dark');
  } else {
    applyTheme('light');
  }
</script>
