<footer>
  <div style="display:flex">
    {{- range $index, $key := .Site.Params.Social -}}
    {{- if hasPrefix $key.icon "simple:" -}}
    <a class="soc" href="{{ $key.url }}" rel="me" title="{{ $key.name }}"><i class="si si-{{ substr $key.icon 7 }}"></i></a>
    {{- else -}}
    <a class="soc" href="{{ $key.url }}" rel="me" title="{{ $key.name }}">{{ template "feathericon" (dict "Icon" $key.icon "UseCDN" .Site.Params.useCDN) }}</a>
    {{- end -}}
    <a class="border"></a>
    {{- end -}}
  </div>
  <div class="footer-info">
    {{ dateFormat "2006" now }} {{ with .Site.Copyright }} {{ . }} | {{ end }}
      {{ range .Site.Menus.footer }}
		    <a href="{{ .URL }}">{{ .Name }}</a>
		  {{ end }}
  </div>
</footer>

<script>
  window.addEventListener('DOMContentLoaded', (event) => {
      const searchState = window.codexSearch || { initialized: false, instance: null };
      const searchElement = document.getElementById('search');
      const toggleButton = document.getElementById('toggle-search');

      const waitForSearchInput = () => new Promise((resolve) => {
        const findInput = () => {
          const input = searchElement.querySelector('input');
          if (input) {
            resolve(input);
            return;
          }

          window.requestAnimationFrame(findInput);
        };

        findInput();
      });

      const ensureSearch = async () => {
        if (!searchElement) {
          return null;
        }

        if (searchState.initialized) {
          return searchElement.querySelector('input');
        }

        if (searchState.initializing) {
          return waitForSearchInput();
        }

        if (typeof window.PagefindUI !== 'function') {
          searchElement.innerHTML = '<p>Search is currently unavailable.</p>';
          searchState.initialized = true;
          return null;
        }

        searchState.initializing = true;
        searchState.instance = new PagefindUI({ element: "#search", showSubResults: false });
        searchState.initialized = true;
        searchState.initializing = false;
        return waitForSearchInput();
      };

      if (toggleButton && searchElement) {
        toggleButton.addEventListener('click', async () => {
          const isHidden = searchElement.hasAttribute('hidden');

          if (isHidden) {
            searchElement.removeAttribute('hidden');
            toggleButton.setAttribute('aria-expanded', 'true');
            const input = await ensureSearch();
            if (input) {
              input.focus();
            }
            return;
          }

          searchElement.setAttribute('hidden', '');
          toggleButton.setAttribute('aria-expanded', 'false');
        });
      }
  });
</script>
