---
import PageTitle from "@/components/PageTitle.astro";
import Layout from "@/layouts/Layout.astro";
import Search from "astro-pagefind/components/Search";
---

<Layout title="Search | fiatcode" description="Search posts on fiatcode.dev">
  <PageTitle title="Search" />
  <Search
    id="search"
    className="pagefind-ui"
    uiOptions={{
      showImages: false,
      translations: {
        placeholder: "Search posts...",
      },
    }}
  />
</Layout>

<style is:global>
  .pagefind-ui {
    --pagefind-ui-scale: 0.9;
    --pagefind-ui-primary: #a3a3a3; /* neutral-400 */
    --pagefind-ui-text: #d4d4d8; /* neutral-300 */
    --pagefind-ui-background: #171717; /* neutral-900 */
    --pagefind-ui-border: #404040; /* neutral-700 */
    --pagefind-ui-tag: #262626; /* neutral-800 */
    --pagefind-ui-border-width: 1px;
    --pagefind-ui-border-radius: 0rem;
    --pagefind-ui-font: "JetBrains Mono", monospace;
  }

  .pagefind-ui__search-input {
    background: #262626; /* neutral-800 */
    color: #d4d4d8; /* neutral-300 */
    border: 1px solid #404040; /* neutral-700 */
  }

  .pagefind-ui__search-input::placeholder {
    color: #737373; /* neutral-500 */
  }

  .pagefind-ui__result {
    border-bottom: 1px solid #404040 !important; /* neutral-700 */
    padding: 1rem 0 !important;
    background: transparent !important;
    border-top: none !important;
    border-left: none !important;
    border-right: none !important;
    margin-bottom: 0 !important;
    position: relative !important;
  }

  .pagefind-ui__result:last-child {
    border-bottom: none !important;
  }

  .pagefind-ui__result-inner {
    padding: 0 !important;
    margin-top: 0 !important;
    align-items: flex-start !important;
  }

  .pagefind-ui__result-link {
    color: #d4d4d8; /* neutral-300 */
    text-decoration: none !important;
    font-weight: 500;
  }

  .pagefind-ui__result-link:hover {
    text-decoration: underline !important;
    color: #fafafa; /* neutral-50 */
  }

  .pagefind-ui__result-link::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
  }

  .pagefind-ui__result-title {
    color: #fafafa; /* neutral-50 */
  }

  .pagefind-ui__result-excerpt {
    color: #a3a3a3; /* neutral-400 */
    margin-top: 0.5rem;
  }

  .pagefind-ui__message {
    color: #a3a3a3; /* neutral-400 */
    padding: 1rem 0;
  }

  /* Remove default image styling if present, though showImages: false handles most */
  .pagefind-ui__result-thumb {
    display: none;
  }
</style>

<script>
  // Re-trigger search when navigating back to the page
  function retriggerSearch() {
    const input = document.querySelector<HTMLInputElement>(
      ".pagefind-ui__search-input",
    );
    if (input && input.value) {
      input.dispatchEvent(new Event("input", { bubbles: true }));
    }
  }

  // Handle browser back/forward navigation
  window.addEventListener("pageshow", (event) => {
    if (event.persisted) {
      // Page was loaded from bfcache
      setTimeout(retriggerSearch, 100);
    }
  });

  // Also trigger on initial load if there's a value
  document.addEventListener("DOMContentLoaded", () => {
    setTimeout(retriggerSearch, 100);
  });
</script>
