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

//#region src/routes/brain/learnings/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		const LIMIT = 50;
		const initial = data.initial;
		let learnings = initial?.learnings ?? [];
		let total = initial?.total ?? 0;
		let filteredCount = initial?.filtered ?? 0;
		let loading = false;
		let error = null;
		let filter = {
			tier: null,
			confidence: null,
			minQuality: void 0
		};
		let sortBy = "created_desc";
		let offset = 0;
		let actionableOnly = false;
		let expanded = null;
		let modalOpen = false;
		let toastMessage = null;
		function buildParams() {
			const p = new URLSearchParams();
			p.set("offset", String(offset));
			p.set("limit", String(LIMIT));
			p.set("sort", sortBy);
			if (filter.minQuality !== void 0) p.set("min_quality", String(filter.minQuality));
			if (filter.confidence === "high") p.set("min_confidence", "0.7");
			else if (filter.confidence === "medium") p.set("min_confidence", "0.4");
			else if (filter.confidence === "low") p.set("min_confidence", "0");
			return p;
		}
		async function load() {
			loading = true;
			error = null;
			try {
				const res = await fetch(`/api/memory/learnings?${buildParams().toString()}`);
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				const body = await res.json();
				learnings = body.learnings;
				total = body.total;
				filteredCount = body.filtered;
			} catch (e) {
				error = e instanceof Error ? e.message : "Failed to load learnings";
			} finally {
				loading = false;
			}
		}
		function onFilterChange(next) {
			filter = next;
			offset = 0;
			load();
		}
		function onSortChange(next) {
			sortBy = next;
			offset = 0;
			load();
		}
		function onPageChange(next) {
			offset = next;
			load();
		}
		function onModalSuccess(id) {
			toastMessage = `Stored learning ${id}`;
			offset = 0;
			load();
			setTimeout(() => {
				toastMessage = null;
			}, 3e3);
		}
		function onModalError(msg) {
			toastMessage = `Save failed: ${msg}`;
			setTimeout(() => {
				toastMessage = null;
			}, 4e3);
		}
		function parseTags(raw) {
			if (!raw) return [];
			try {
				const parsed = JSON.parse(raw);
				if (Array.isArray(parsed)) return parsed.filter((x) => typeof x === "string");
			} catch {}
			return [];
		}
		let $$settled = true;
		let $$inner_renderer;
		function $$render_inner($$renderer) {
			head("49ufcn", $$renderer, ($$renderer) => {
				$$renderer.title(($$renderer) => {
					$$renderer.push(`<title>BRAIN Learnings — CLEO Studio</title>`);
				});
			});
			$$renderer.push(`<section class="page svelte-49ufcn"><header class="page-head svelte-49ufcn"><div class="head-left svelte-49ufcn"><a class="back svelte-49ufcn" href="/brain/overview">← Overview</a> <h1 class="title svelte-49ufcn">Learnings</h1> <span class="count svelte-49ufcn"><span class="count-n svelte-49ufcn">${escape_html(filteredCount)}</span> <span class="count-div svelte-49ufcn">/</span> <span class="count-total svelte-49ufcn">${escape_html(total)}</span> <span class="count-label svelte-49ufcn">shown</span></span></div> <div class="head-right svelte-49ufcn"><button type="button"${attr_class("toggle-btn svelte-49ufcn", void 0, { "on": actionableOnly })}${attr("aria-pressed", actionableOnly)}>${escape_html("All learnings")}</button> `);
			Button($$renderer, {
				variant: "primary",
				size: "sm",
				onclick: () => modalOpen = true,
				children: ($$renderer) => {
					$$renderer.push(`<!---->+ Store learning`);
				}});
			$$renderer.push(`<!----></div></header> <div class="controls svelte-49ufcn">`);
			FilterBar($$renderer, {
				value: filter,
				tiers: [
					"short",
					"medium",
					"long"
				],
				confidences: [
					"high",
					"medium",
					"low"
				],
				showQuality: true,
				onChange: onFilterChange
			});
			$$renderer.push(`<!----> `);
			SortControl($$renderer, {
				value: sortBy,
				onChange: onSortChange
			});
			$$renderer.push(`<!----></div> `);
			if (loading) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="state svelte-49ufcn">`);
				Spinner($$renderer, { size: "md" });
				$$renderer.push(`<!----> <span>Loading learnings…</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 learnings",
						subtitle: error,
						variant: "warning",
						action});
				}
			} else if (learnings.length === 0) {
				$$renderer.push("<!--[2-->");
				{
					function action($$renderer) {
						Button($$renderer, {
							variant: "primary",
							size: "sm",
							onclick: () => modalOpen = true,
							children: ($$renderer) => {
								$$renderer.push(`<!---->Store a learning`);
							}});
					}
					EmptyState($$renderer, {
						title: "No learnings captured",
						subtitle: "Capture an insight — a future session will recall it automatically.",
						action});
				}
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<ul class="list svelte-49ufcn"><!--[-->`);
				const each_array = ensure_array_like(learnings);
				for (let $$index_1 = 0, $$length = each_array.length; $$index_1 < $$length; $$index_1++) {
					let l = each_array[$$index_1];
					const tags = parseTags(l.applicable_types);
					$$renderer.push(`<li${attr_class("row svelte-49ufcn", void 0, {
						"invalid": !!l.invalid_at,
						"prune": l.prune_candidate === 1
					})}><button class="row-head svelte-49ufcn"${attr("aria-expanded", expanded === l.id)}><span class="row-meta svelte-49ufcn"><code class="row-id svelte-49ufcn">${escape_html(l.id)}</code> `);
					ConfidenceBadge($$renderer, {
						confidence: l.confidence !== null && l.confidence >= .7 ? "high" : l.confidence !== null && l.confidence >= .4 ? "medium" : l.confidence !== null ? "low" : "unknown",
						value: l.confidence
					});
					$$renderer.push(`<!----> `);
					TierBadge($$renderer, { tier: l.memory_tier });
					$$renderer.push(`<!----> `);
					if (l.actionable === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "accent",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->actionable`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (l.verified === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "success",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->verified`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (l.prune_candidate === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "warning",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->prune`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (l.invalid_at) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "danger",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->invalidated`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (l.citation_count > 0) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<span class="cites svelte-49ufcn">${escape_html(l.citation_count)} citations</span>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></span> <span class="row-body svelte-49ufcn"><span class="row-text svelte-49ufcn">${escape_html(l.insight)}</span> `);
					QualityBar($$renderer, {
						score: l.quality_score,
						width: 80
					});
					$$renderer.push(`<!----></span></button> `);
					if (expanded === l.id) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<div class="row-detail svelte-49ufcn">`);
						if (l.source) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-49ufcn"><span class="detail-label svelte-49ufcn">Source</span> <p class="detail-text svelte-49ufcn">${escape_html(l.source)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (l.application) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-49ufcn"><span class="detail-label svelte-49ufcn">Application</span> <p class="detail-text svelte-49ufcn">${escape_html(l.application)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (tags.length > 0) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-49ufcn"><span class="detail-label svelte-49ufcn">Applicable to</span> <div class="tag-row svelte-49ufcn"><!--[-->`);
							const each_array_1 = ensure_array_like(tags);
							for (let $$index = 0, $$length = each_array_1.length; $$index < $$length; $$index++) {
								let t = each_array_1[$$index];
								Badge($$renderer, {
									tone: "neutral",
									size: "sm",
									subtle: true,
									children: ($$renderer) => {
										$$renderer.push(`<!---->${escape_html(t)}`);
									}});
							}
							$$renderer.push(`<!--]--></div></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <div class="detail-footer svelte-49ufcn"><span class="detail-ts svelte-49ufcn">Stored ${escape_html(l.created_at.slice(0, 10))}</span></div></div>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></li>`);
				}
				$$renderer.push(`<!--]--></ul> `);
				Pagination($$renderer, {
					offset,
					limit: LIMIT,
					total,
					onChange: onPageChange
				});
				$$renderer.push(`<!---->`);
			}
			$$renderer.push(`<!--]--></section> `);
			LearningModal($$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-49ufcn" 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-BXyoMME1.js.map
