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, D as DecisionModal, C as ConfidenceBadge, T as TierBadge, Q as QualityBar, P as Pagination } from './memory2-Cgc_4D5q.js';

//#region src/routes/brain/decisions/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		const LIMIT = 50;
		let decisions = [];
		let total = 0;
		let loading = true;
		let error = null;
		let filter = {
			tier: null,
			confidence: null,
			minQuality: void 0
		};
		let sortBy = "created_desc";
		let offset = 0;
		let searchText = "";
		let expandedId = null;
		let modalOpen = false;
		let toastMessage = null;
		async function load() {
			loading = true;
			error = null;
			try {
				const res = await fetch("/api/memory/decisions");
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				const body = await res.json();
				decisions = body.decisions;
				total = body.total;
			} catch (e) {
				error = e instanceof Error ? e.message : "Failed to load decisions";
			} finally {
				loading = false;
			}
		}
		const displayed = derived(() => {
			let list = decisions.slice();
			if (filter.tier) list = list.filter((d) => d.memory_tier === filter.tier);
			if (filter.confidence) list = list.filter((d) => d.confidence?.toLowerCase() === filter.confidence);
			if (filter.minQuality !== void 0) {
				const m = filter.minQuality;
				list = list.filter((d) => d.quality_score === null || d.quality_score >= m);
			}
			if (searchText) {
				const q = searchText.toLowerCase();
				list = list.filter((d) => d.decision.toLowerCase().includes(q) || d.rationale.toLowerCase().includes(q));
			}
			if (sortBy === "quality_desc") list.sort((a, b) => (b.quality_score ?? -1) - (a.quality_score ?? -1));
			else if (sortBy === "citation_desc") list.sort((a, b) => b.created_at.localeCompare(a.created_at));
			else list.sort((a, b) => b.created_at.localeCompare(a.created_at));
			return list;
		});
		const paged = derived(() => displayed().slice(offset, offset + LIMIT));
		function onFilterChange(next) {
			filter = next;
			offset = 0;
		}
		function onSortChange(next) {
			sortBy = next;
			offset = 0;
		}
		function onPageChange(next) {
			offset = next;
		}
		function onModalSuccess(id) {
			toastMessage = `Stored decision ${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("c3xmlv", $$renderer, ($$renderer) => {
				$$renderer.title(($$renderer) => {
					$$renderer.push(`<title>BRAIN Decisions — CLEO Studio</title>`);
				});
			});
			$$renderer.push(`<section class="page svelte-c3xmlv"><header class="page-head svelte-c3xmlv"><div class="head-left svelte-c3xmlv"><a class="back svelte-c3xmlv" href="/brain/overview">← Overview</a> <h1 class="title svelte-c3xmlv">Decisions</h1> <span class="count svelte-c3xmlv"><span class="count-n svelte-c3xmlv">${escape_html(displayed().length)}</span> <span class="count-div svelte-c3xmlv">/</span> <span class="count-total svelte-c3xmlv">${escape_html(total)}</span> <span class="count-label svelte-c3xmlv">shown</span></span></div> <div class="head-right svelte-c3xmlv"><a class="canvas-pill svelte-c3xmlv" href="/brain?scope=brain&amp;type=decision">Open in Canvas →</a> `);
			Button($$renderer, {
				variant: "primary",
				size: "sm",
				onclick: () => modalOpen = true,
				children: ($$renderer) => {
					$$renderer.push(`<!---->+ Store decision`);
				}});
			$$renderer.push(`<!----></div></header> <div class="controls svelte-c3xmlv">`);
			FilterBar($$renderer, {
				value: {
					...filter,
					q: searchText
				},
				tiers: [
					"short",
					"medium",
					"long"
				],
				confidences: [
					"high",
					"medium",
					"low"
				],
				showQuality: true,
				showSearch: true,
				searchPlaceholder: "Search decisions…",
				onChange: (next) => {
					searchText = next.q ?? "";
					const { q, ...rest } = next;
					onFilterChange(rest);
				}
			});
			$$renderer.push(`<!----> `);
			SortControl($$renderer, {
				value: sortBy,
				onChange: onSortChange,
				allowCitation: false
			});
			$$renderer.push(`<!----></div> `);
			if (loading) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="state svelte-c3xmlv">`);
				Spinner($$renderer, { size: "md" });
				$$renderer.push(`<!----> <span>Loading decisions…</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 decisions",
						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 decision`);
							}});
					}
					EmptyState($$renderer, {
						title: "No decisions match these filters",
						subtitle: "Loosen the filter or capture the first decision of the project.",
						action});
				}
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<ol class="timeline svelte-c3xmlv"><!--[-->`);
				const each_array = ensure_array_like(paged());
				for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
					let dec = each_array[$$index];
					$$renderer.push(`<li${attr_class("tl-item svelte-c3xmlv", void 0, {
						"expanded": expandedId === dec.id,
						"invalid": !!dec.invalid_at
					})}><span class="tl-connector svelte-c3xmlv" aria-hidden="true"><span class="tl-dot svelte-c3xmlv"${attr("data-tier", dec.memory_tier ?? "short")}></span> <span class="tl-line svelte-c3xmlv"></span></span> <div class="tl-content svelte-c3xmlv"><button class="tl-head svelte-c3xmlv"${attr("aria-expanded", expandedId === dec.id)}><span class="tl-meta svelte-c3xmlv"><code class="tl-id svelte-c3xmlv">${escape_html(dec.id)}</code> `);
					Badge($$renderer, {
						tone: "neutral",
						size: "sm",
						subtle: true,
						children: ($$renderer) => {
							$$renderer.push(`<!---->${escape_html(dec.type)}`);
						}});
					$$renderer.push(`<!----> `);
					ConfidenceBadge($$renderer, { confidence: dec.confidence });
					$$renderer.push(`<!----> `);
					TierBadge($$renderer, { tier: dec.memory_tier });
					$$renderer.push(`<!----> `);
					if (dec.verified === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "success",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->verified`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (dec.prune_candidate === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "warning",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->prune`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (dec.invalid_at) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "danger",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->invalidated`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> <span class="tl-date svelte-c3xmlv">${escape_html(dec.created_at.slice(0, 10))}</span></span> <p class="tl-statement svelte-c3xmlv">${escape_html(dec.decision)}</p> <div class="tl-body-end svelte-c3xmlv">`);
					QualityBar($$renderer, {
						score: dec.quality_score,
						width: 80
					});
					$$renderer.push(`<!----></div></button> `);
					if (expandedId === dec.id) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<div class="tl-detail svelte-c3xmlv"><div class="detail-section svelte-c3xmlv"><span class="detail-label svelte-c3xmlv">Rationale</span> <p class="detail-text svelte-c3xmlv">${escape_html(dec.rationale)}</p></div> `);
						if (dec.outcome) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-c3xmlv"><span class="detail-label svelte-c3xmlv">Outcome</span> <p class="detail-text svelte-c3xmlv">${escape_html(dec.outcome)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <div class="detail-footer svelte-c3xmlv">`);
						if (dec.context_task_id) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<a class="ctx-chip svelte-c3xmlv"${attr("href", `/tasks#${dec.context_task_id}`)}>Task · <code class="svelte-c3xmlv">${escape_html(dec.context_task_id)}</code></a>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (dec.context_epic_id) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<a class="ctx-chip svelte-c3xmlv"${attr("href", `/tasks#${dec.context_epic_id}`)}>Epic · <code class="svelte-c3xmlv">${escape_html(dec.context_epic_id)}</code></a>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (dec.context_phase) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="detail-meta svelte-c3xmlv">Phase · ${escape_html(dec.context_phase)}</span>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--></div></div>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></div></li>`);
				}
				$$renderer.push(`<!--]--></ol> `);
				Pagination($$renderer, {
					offset,
					limit: LIMIT,
					total: displayed().length,
					onChange: onPageChange
				});
				$$renderer.push(`<!---->`);
			}
			$$renderer.push(`<!--]--></section> `);
			DecisionModal($$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-c3xmlv" 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-al08ovKm.js.map
