import { x as head, e as ensure_array_like, c as escape_html, a as attr, g as derived } from './dev-BNpEUZI3.js';
import 'node:module';
import { C as Card, B as Badge } from './Tabs-DzG6Bn9a.js';
import { I as Input, E as EmptyState, S as Spinner, B as Button } from './ui-DR8zOSpC.js';
import { T as TierBadge, Q as QualityBar } from './memory2-Cgc_4D5q.js';

//#region src/routes/brain/search/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		const RECENT_KEY = "cleo:brain:recent-searches";
		const MAX_RECENT = 6;
		let query = data.initialQuery;
		let hits = [];
		let loading = false;
		let error = null;
		let recentSearches = [];
		let debounceTimer = null;
		let abortController = null;
		const GROUPS = [
			"observations",
			"decisions",
			"patterns",
			"learnings"
		];
		const GROUP_LABELS = {
			observations: "Observations",
			decisions: "Decisions",
			patterns: "Patterns",
			learnings: "Learnings"
		};
		const TABLE_TONE = {
			observations: "info",
			decisions: "success",
			patterns: "accent",
			learnings: "warning"
		};
		function saveRecent(q) {
			try {
				recentSearches = [q, ...recentSearches.filter((r) => r !== q)].slice(0, MAX_RECENT);
				localStorage.setItem(RECENT_KEY, JSON.stringify(recentSearches));
			} catch {}
		}
		async function runSearch(q) {
			if (!q.trim()) {
				hits = [];
				error = null;
				loading = false;
				return;
			}
			abortController?.abort();
			abortController = new AbortController();
			loading = true;
			error = null;
			try {
				const res = await fetch(`/api/memory/find?q=${encodeURIComponent(q)}`, { signal: abortController.signal });
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				hits = (await res.json()).hits;
				saveRecent(q);
			} catch (e) {
				if (e instanceof DOMException && e.name === "AbortError") return;
				error = e instanceof Error ? e.message : "Search failed";
			} finally {
				loading = false;
			}
		}
		function onInput(e) {
			const v = e.target.value;
			query = v;
			if (debounceTimer) clearTimeout(debounceTimer);
			debounceTimer = setTimeout(() => {
				runSearch(v);
				try {
					const url = new URL(window.location.href);
					if (v.trim()) url.searchParams.set("q", v);
					else url.searchParams.delete("q");
					window.history.replaceState({}, "", url);
				} catch {}
			}, 250);
		}
		const grouped = derived(() => {
			const g = {
				observations: [],
				decisions: [],
				patterns: [],
				learnings: []
			};
			for (const h of hits) g[h.table].push(h);
			return g;
		});
		function hitRoute(h) {
			return `${`/brain/${h.table}`}#${encodeURIComponent(h.id)}`;
		}
		head("1nj6iq2", $$renderer, ($$renderer) => {
			$$renderer.title(($$renderer) => {
				$$renderer.push(`<title>BRAIN Search — CLEO Studio</title>`);
			});
		});
		$$renderer.push(`<section class="page svelte-1nj6iq2"><header class="page-head svelte-1nj6iq2"><div class="head-left"><a class="back svelte-1nj6iq2" href="/brain/overview">← Overview</a> <h1 class="title svelte-1nj6iq2">Search</h1> <span class="subtitle svelte-1nj6iq2">Cross-table memory retrieval — observations, decisions, patterns, learnings</span></div></header> <div class="search-shell svelte-1nj6iq2">`);
		Input($$renderer, {
			type: "search",
			value: query,
			label: "Memory search",
			placeholder: "Search across observations, decisions, patterns, learnings…",
			oninput: onInput
		});
		$$renderer.push(`<!----> `);
		if (loading) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<span class="live-indicator svelte-1nj6iq2">`);
			Spinner($$renderer, {
				size: "sm",
				label: ""
			});
			$$renderer.push(`<!----> <span>searching…</span></span>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--></div> `);
		if (!query.trim()) {
			$$renderer.push("<!--[0-->");
			{
				function header($$renderer) {
					$$renderer.push(`<div class="panel-head svelte-1nj6iq2"><h2 class="panel-title svelte-1nj6iq2">Recent searches</h2> `);
					if (recentSearches.length > 0) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<button type="button" class="clear-btn svelte-1nj6iq2">clear</button>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></div>`);
				}
				Card($$renderer, {
					header,
					children: ($$renderer) => {
						if (recentSearches.length === 0) {
							$$renderer.push("<!--[0-->");
							EmptyState($$renderer, {
								title: "Nothing yet",
								subtitle: "Type above to search memory. Your recent queries will appear here."
							});
						} else {
							$$renderer.push("<!--[-1-->");
							$$renderer.push(`<ul class="recent-list svelte-1nj6iq2"><!--[-->`);
							const each_array = ensure_array_like(recentSearches);
							for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
								let r = each_array[$$index];
								$$renderer.push(`<li><button type="button" class="recent-item svelte-1nj6iq2"><span class="recent-arrow svelte-1nj6iq2" aria-hidden="true">↗</span> <span>${escape_html(r)}</span></button></li>`);
							}
							$$renderer.push(`<!--]--></ul>`);
						}
						$$renderer.push(`<!--]-->`);
					}});
			}
		} else if (error) {
			$$renderer.push("<!--[1-->");
			{
				function action($$renderer) {
					Button($$renderer, {
						variant: "secondary",
						size: "sm",
						onclick: () => {
							runSearch(query);
						},
						children: ($$renderer) => {
							$$renderer.push(`<!---->Retry`);
						}});
				}
				EmptyState($$renderer, {
					title: "Search failed",
					subtitle: error,
					variant: "warning",
					action});
			}
		} else if (loading && hits.length === 0) {
			$$renderer.push("<!--[2-->");
			$$renderer.push(`<div class="state svelte-1nj6iq2">`);
			Spinner($$renderer, { size: "md" });
			$$renderer.push(`<!----> <span>Searching memory…</span></div>`);
		} else if (hits.length === 0) {
			$$renderer.push("<!--[3-->");
			EmptyState($$renderer, {
				title: "No matches",
				subtitle: `Nothing matched "${query}". Try a broader term or a synonym.`
			});
		} else {
			$$renderer.push("<!--[-1-->");
			$$renderer.push(`<div class="groups svelte-1nj6iq2"><!--[-->`);
			const each_array_1 = ensure_array_like(GROUPS);
			for (let $$index_2 = 0, $$length = each_array_1.length; $$index_2 < $$length; $$index_2++) {
				let g = each_array_1[$$index_2];
				const items = grouped()[g];
				if (items.length > 0) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<section class="group"><div class="group-head svelte-1nj6iq2"><h3 class="group-title svelte-1nj6iq2">${escape_html(GROUP_LABELS[g])}</h3> `);
					Badge($$renderer, {
						tone: TABLE_TONE[g],
						size: "sm",
						pill: true,
						children: ($$renderer) => {
							$$renderer.push(`<!---->${escape_html(items.length)}`);
						}});
					$$renderer.push(`<!----></div> <ul class="group-list svelte-1nj6iq2"><!--[-->`);
					const each_array_2 = ensure_array_like(items);
					for (let $$index_1 = 0, $$length = each_array_2.length; $$index_1 < $$length; $$index_1++) {
						let h = each_array_2[$$index_1];
						$$renderer.push(`<li><a class="hit svelte-1nj6iq2"${attr("href", hitRoute(h))}><div class="hit-head svelte-1nj6iq2"><code class="hit-id svelte-1nj6iq2">${escape_html(h.id)}</code> `);
						TierBadge($$renderer, { tier: h.tier });
						$$renderer.push(`<!----> `);
						if (h.verified === 1) {
							$$renderer.push("<!--[0-->");
							Badge($$renderer, {
								tone: "success",
								size: "sm",
								children: ($$renderer) => {
									$$renderer.push(`<!---->verified`);
								}});
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (h.citations > 0) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="hit-cites svelte-1nj6iq2">${escape_html(h.citations)} citations</span>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <span class="hit-date svelte-1nj6iq2">${escape_html(h.createdAt.slice(0, 10))}</span></div> <p class="hit-title svelte-1nj6iq2">${escape_html(h.title)}</p> `);
						if (h.preview) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<p class="hit-preview svelte-1nj6iq2">${escape_html(h.preview)}</p>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <div class="hit-foot svelte-1nj6iq2">`);
						QualityBar($$renderer, {
							score: h.quality,
							width: 80
						});
						$$renderer.push(`<!----></div></a></li>`);
					}
					$$renderer.push(`<!--]--></ul></section>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]-->`);
			}
			$$renderer.push(`<!--]--></div>`);
		}
		$$renderer.push(`<!--]--></section>`);
	});
}

export { _page as default };
//# sourceMappingURL=_page.svelte-UYiF7YBK.js.map
