// Code generated by templ - DO NOT EDIT.

// templ: version: v0.2.778
package components

//lint:file-ignore SA4006 This context is only used if a nested component is present.

import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"

func SearchJS() templ.Component {
	return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
		templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
		if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
			return templ_7745c5c3_CtxErr
		}
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
		if !templ_7745c5c3_IsBuffer {
			defer func() {
				templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
				if templ_7745c5c3_Err == nil {
					templ_7745c5c3_Err = templ_7745c5c3_BufErr
				}
			}()
		}
		ctx = templ.InitializeContext(ctx)
		templ_7745c5c3_Var1 := templ.GetChildren(ctx)
		if templ_7745c5c3_Var1 == nil {
			templ_7745c5c3_Var1 = templ.NopComponent
		}
		ctx = templ.ClearChildren(ctx)
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script>\n    const input = document.getElementById('stock-search-input');\n    const dropdown = document.getElementById('search-dropdown');\n    let debounceTimeout;\n    let selectedIndex = -1;\n\n    input.addEventListener('input', function () {\n      selectedIndex = -1;\n      const query = input.value.trim();\n      clearTimeout(debounceTimeout);\n      if (query.length < 2) {\n        dropdown.classList.remove('show');\n        dropdown.innerHTML = '';\n        return;\n      }\n      debounceTimeout = setTimeout(() => {\n        fetch('/api/stocksearch?q=' + encodeURIComponent(query))\n          .then(res => res.json())\n          .then(data => {\n            if (data.quotes && data.quotes.length > 0) {\n              dropdown.innerHTML = data.quotes.map(item =>\n                `<a class=\"dropdown-item-search\" href=\"/details?stock=${encodeURIComponent(item.symbol)}\">${item.longname || item.shortname || item.symbol} (${item.symbol})</a>`\n              ).join('');\n              dropdown.classList.add('show');\n            } else {\n              dropdown.innerHTML = '<div class=\"dropdown-item-search\">Keine Ergebnisse</div>';\n              dropdown.classList.add('show');\n            }\n          });\n      }, 300);\n    });\n\n    input.addEventListener('keydown', function (e) {\n      const items = Array.from(dropdown.querySelectorAll('.dropdown-item-search'));\n      if (!dropdown.classList.contains('show') || items.length === 0) return;\n\n      if (e.key === 'ArrowDown') {\n        e.preventDefault();\n        selectedIndex = (selectedIndex + 1) % items.length;\n        updateDropdownSelection(items);\n      } else if (e.key === 'ArrowUp') {\n        e.preventDefault();\n        selectedIndex = (selectedIndex - 1 + items.length) % items.length;\n        updateDropdownSelection(items);\n      } else if (e.key === 'Enter') {\n        if (selectedIndex >= 0 && selectedIndex < items.length) {\n          e.preventDefault();\n          window.location.href = items[selectedIndex].getAttribute('href');\n        }\n      }\n    });\n\n    function updateDropdownSelection(items) {\n      items.forEach((item, idx) => {\n        if (idx === selectedIndex) {\n          item.classList.add('active');\n          item.scrollIntoView({ block: 'nearest' });\n        } else {\n          item.classList.remove('active');\n        }\n      });\n    }\n\n    document.addEventListener('click', function (e) {\n      if (!input.contains(e.target) && !dropdown.contains(e.target)) {\n        dropdown.classList.remove('show');\n      }\n    });\n\n    document.getElementById('stock-search-form').addEventListener('submit', function(e) {\n      e.preventDefault();\n      const items = Array.from(dropdown.querySelectorAll('.dropdown-item-search'));\n      // Wenn ein Eintrag ausgewählt ist, nimm diesen\n      if (selectedIndex >= 0 && selectedIndex < items.length) {\n        window.location.href = items[selectedIndex].getAttribute('href');\n        return;\n      }\n      // Sonst nimm das erste Ergebnis, falls vorhanden\n      if (items.length > 0) {\n        window.location.href = items[0].getAttribute('href');\n        return;\n      }\n      // Sonst nichts tun\n    });\n  </script>")
		if templ_7745c5c3_Err != nil {
			return templ_7745c5c3_Err
		}
		return templ_7745c5c3_Err
	})
}

var _ = templruntime.GeneratedTemplate
