import { x as head, c as escape_html, g as derived, e as ensure_array_like, b as attr_class, a as attr } from './dev-BNpEUZI3.js';
import 'node:module';
import { B as Badge } from './Tabs-DzG6Bn9a.js';
import { B as Button, S as Spinner, E as EmptyState } from './ui-DR8zOSpC.js';
import { F as FilterBar, S as SortControl, O as ObserveModal, T as TierBadge, Q as QualityBar, P as Pagination } from './memory2-Cgc_4D5q.js';

//#region src/routes/brain/observations/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		const LIMIT = 50;
		let observations = [];
		let total = 0;
		let loading = true;
		let error = null;
		let filter = {
			tier: null,
			type: null,
			minQuality: void 0
		};
		let sortBy = "created_desc";
		let offset = 0;
		let searchText = "";
		let expandedId = null;
		let modalOpen = false;
		let toastMessage = null;
		function buildParams() {
			const p = new URLSearchParams();
			if (filter.tier) p.set("tier", filter.tier);
			if (filter.type) p.set("type", filter.type);
			if (filter.minQuality !== void 0) p.set("min_quality", String(filter.minQuality));
			return p;
		}
		async function load() {
			loading = true;
			error = null;
			try {
				const res = await fetch(`/api/memory/observations?${buildParams().toString()}`);
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				const body = await res.json();
				observations = body.observations;
				total = body.total;
				body.filtered;
			} catch (e) {
				error = e instanceof Error ? e.message : "Failed to load observations";
			} finally {
				loading = false;
			}
		}
		const displayed = derived(() => {
			let list = observations;
			if (searchText) {
				const q = searchText.toLowerCase();
				list = list.filter((o) => o.title.toLowerCase().includes(q) || (o.narrative ?? "").toLowerCase().includes(q));
			}
			if (sortBy === "quality_desc") list = [...list].sort((a, b) => (b.quality_score ?? -1) - (a.quality_score ?? -1));
			else if (sortBy === "citation_desc") list = [...list].sort((a, b) => b.citation_count - a.citation_count);
			return list;
		});
		const paged = derived(() => displayed().slice(offset, offset + LIMIT));
		function onFilterChange(next) {
			filter = next;
			offset = 0;
			load();
		}
		function onSortChange(next) {
			sortBy = next;
			offset = 0;
		}
		function onPageChange(next) {
			offset = next;
		}
		function onModalSuccess(id) {
			toastMessage = `Stored observation ${id}`;
			offset = 0;
			load();
			setTimeout(() => {
				toastMessage = null;
			}, 3e3);
		}
		function onModalError(msg) {
			toastMessage = `Save failed: ${msg}`;
			setTimeout(() => {
				toastMessage = null;
			}, 4e3);
		}
		let $$settled = true;
		let $$inner_renderer;
		function $$render_inner($$renderer) {
			head("8nabpv", $$renderer, ($$renderer) => {
				$$renderer.title(($$renderer) => {
					$$renderer.push(`<title>BRAIN Observations — CLEO Studio</title>`);
				});
			});
			$$renderer.push(`<section class="page svelte-8nabpv"><header class="page-head svelte-8nabpv"><div class="head-left svelte-8nabpv"><a class="back svelte-8nabpv" href="/brain/overview">← Overview</a> <h1 class="title svelte-8nabpv">Observations</h1> <span class="count svelte-8nabpv"><span class="count-n svelte-8nabpv">${escape_html(displayed().length)}</span> <span class="count-div svelte-8nabpv">/</span> <span class="count-total svelte-8nabpv">${escape_html(total)}</span> <span class="count-label svelte-8nabpv">shown</span></span></div> <div class="head-right svelte-8nabpv"><a class="canvas-pill svelte-8nabpv" href="/brain?scope=brain&amp;type=observation">Open in Canvas →</a> `);
			Button($$renderer, {
				variant: "primary",
				size: "sm",
				onclick: () => modalOpen = true,
				children: ($$renderer) => {
					$$renderer.push(`<!---->+ Observe`);
				}});
			$$renderer.push(`<!----></div></header> <div class="controls svelte-8nabpv">`);
			FilterBar($$renderer, {
				value: {
					...filter,
					q: searchText
				},
				tiers: [
					"short",
					"medium",
					"long"
				],
				types: [
					"episodic",
					"semantic",
					"procedural"
				],
				showQuality: true,
				showSearch: true,
				searchPlaceholder: "Search title or narrative…",
				onChange: (next) => {
					searchText = next.q ?? "";
					const { q, ...rest } = next;
					onFilterChange(rest);
				}
			});
			$$renderer.push(`<!----> `);
			SortControl($$renderer, {
				value: sortBy,
				onChange: onSortChange
			});
			$$renderer.push(`<!----></div> `);
			if (loading) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="state svelte-8nabpv">`);
				Spinner($$renderer, { size: "md" });
				$$renderer.push(`<!----> <span>Loading observations…</span></div>`);
			} else if (error) {
				$$renderer.push("<!--[1-->");
				{
					function action($$renderer) {
						Button($$renderer, {
							variant: "secondary",
							size: "sm",
							onclick: () => {
								load();
							},
							children: ($$renderer) => {
								$$renderer.push(`<!---->Retry`);
							}});
					}
					EmptyState($$renderer, {
						title: "Couldn't load observations",
						subtitle: error,
						variant: "warning",
						action});
				}
			} else if (paged().length === 0) {
				$$renderer.push("<!--[2-->");
				{
					function action($$renderer) {
						Button($$renderer, {
							variant: "primary",
							size: "sm",
							onclick: () => modalOpen = true,
							children: ($$renderer) => {
								$$renderer.push(`<!---->New observation`);
							}});
					}
					EmptyState($$renderer, {
						title: "No observations match these filters",
						subtitle: "Loosen filters, or record a fresh observation — the brain learns from every one.",
						action});
				}
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<ul class="list svelte-8nabpv"><!--[-->`);
				const each_array = ensure_array_like(paged());
				for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
					let obs = each_array[$$index];
					$$renderer.push(`<li${attr_class("row svelte-8nabpv", void 0, {
						"invalid": !!obs.invalid_at,
						"prune": obs.prune_candidate === 1
					})}><button class="row-head svelte-8nabpv"${attr("aria-expanded", expandedId === obs.id)}><span class="row-meta svelte-8nabpv"><code class="row-id svelte-8nabpv">${escape_html(obs.id)}</code> `);
					Badge($$renderer, {
						tone: "info",
						size: "sm",
						subtle: true,
						children: ($$renderer) => {
							$$renderer.push(`<!---->${escape_html(obs.type)}`);
						}});
					$$renderer.push(`<!----> `);
					TierBadge($$renderer, { tier: obs.memory_tier });
					$$renderer.push(`<!----> `);
					if (obs.memory_type) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "neutral",
							size: "sm",
							subtle: true,
							children: ($$renderer) => {
								$$renderer.push(`<!---->${escape_html(obs.memory_type)}`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (obs.verified === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "success",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->verified`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (obs.prune_candidate === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "warning",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->prune`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (obs.invalid_at) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "danger",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->invalidated`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (obs.citation_count > 0) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<span class="cites svelte-8nabpv">${escape_html(obs.citation_count)} citations</span>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> <span class="row-date svelte-8nabpv">${escape_html(obs.created_at.slice(0, 10))}</span></span> <span class="row-body svelte-8nabpv"><span class="row-text svelte-8nabpv">${escape_html(obs.title)}</span> `);
					QualityBar($$renderer, {
						score: obs.quality_score,
						width: 80
					});
					$$renderer.push(`<!----></span></button> `);
					if (expandedId === obs.id) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<div class="row-detail svelte-8nabpv">`);
						if (obs.subtitle) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<p class="subtitle svelte-8nabpv">${escape_html(obs.subtitle)}</p>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (obs.narrative) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-8nabpv"><span class="detail-label svelte-8nabpv">Narrative</span> <p class="detail-text svelte-8nabpv">${escape_html(obs.narrative)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <div class="detail-footer svelte-8nabpv"><code class="detail-id svelte-8nabpv">${escape_html(obs.id)}</code> `);
						if (obs.project) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="detail-meta svelte-8nabpv">Project · ${escape_html(obs.project)}</span>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (obs.source_confidence) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="detail-meta svelte-8nabpv">Confidence · ${escape_html(obs.source_confidence)}</span>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--></div></div>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></li>`);
				}
				$$renderer.push(`<!--]--></ul> `);
				Pagination($$renderer, {
					offset,
					limit: LIMIT,
					total: displayed().length,
					onChange: onPageChange
				});
				$$renderer.push(`<!---->`);
			}
			$$renderer.push(`<!--]--></section> `);
			ObserveModal($$renderer, {
				onSuccess: onModalSuccess,
				onError: onModalError,
				get open() {
					return modalOpen;
				},
				set open($$value) {
					modalOpen = $$value;
					$$settled = false;
				}
			});
			$$renderer.push(`<!----> `);
			if (toastMessage) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="toast svelte-8nabpv" role="status" aria-live="polite">${escape_html(toastMessage)}</div>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]-->`);
		}
		do {
			$$settled = true;
			$$inner_renderer = $$renderer.copy();
			$$render_inner($$inner_renderer);
		} while (!$$settled);
		$$renderer.subsume($$inner_renderer);
	});
}

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