import { x as head, c as escape_html, e as ensure_array_like, b as attr_class, a as attr } 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, T as TierBadge, Q as QualityBar, P as Pagination, b as PatternModal } from './memory2-Cgc_4D5q.js';
import 'node:module';

//#region src/routes/brain/patterns/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		const LIMIT = 50;
		const initial = data.initial;
		let patterns = initial?.patterns ?? [];
		let total = initial?.total ?? 0;
		let filteredCount = initial?.filtered ?? 0;
		let loading = false;
		let error = null;
		let filter = {
			tier: null,
			type: null,
			status: null,
			minQuality: void 0
		};
		let sortBy = "created_desc";
		let offset = 0;
		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.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/patterns?${buildParams().toString()}`);
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				const body = await res.json();
				patterns = body.patterns;
				total = body.total;
				filteredCount = body.filtered;
			} catch (e) {
				error = e instanceof Error ? e.message : "Failed to load patterns";
			} 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 pattern ${id}`;
			offset = 0;
			load();
			setTimeout(() => {
				toastMessage = null;
			}, 3e3);
		}
		function onModalError(msg) {
			toastMessage = `Save failed: ${msg}`;
			setTimeout(() => {
				toastMessage = null;
			}, 4e3);
		}
		const IMPACT_TONE = {
			high: "danger",
			medium: "warning",
			low: "neutral"
		};
		const TYPE_TONE = {
			success: "success",
			failure: "danger",
			optimization: "accent",
			workflow: "info",
			blocker: "danger"
		};
		let $$settled = true;
		let $$inner_renderer;
		function $$render_inner($$renderer) {
			head("cwsxhn", $$renderer, ($$renderer) => {
				$$renderer.title(($$renderer) => {
					$$renderer.push(`<title>BRAIN Patterns — CLEO Studio</title>`);
				});
			});
			$$renderer.push(`<section class="page svelte-cwsxhn"><header class="page-head svelte-cwsxhn"><div class="head-left svelte-cwsxhn"><a class="back svelte-cwsxhn" href="/brain/overview">← Overview</a> <h1 class="title svelte-cwsxhn">Patterns</h1> <span class="count svelte-cwsxhn"><span class="count-n svelte-cwsxhn">${escape_html(filteredCount)}</span> <span class="count-div svelte-cwsxhn">/</span> <span class="count-total svelte-cwsxhn">${escape_html(total)}</span> <span class="count-label svelte-cwsxhn">shown</span></span></div> <div class="head-right">`);
			Button($$renderer, {
				variant: "primary",
				size: "sm",
				onclick: () => modalOpen = true,
				children: ($$renderer) => {
					$$renderer.push(`<!---->+ Store pattern`);
				}});
			$$renderer.push(`<!----></div></header> <div class="controls svelte-cwsxhn">`);
			FilterBar($$renderer, {
				value: filter,
				tiers: [
					"short",
					"medium",
					"long"
				],
				types: [
					"workflow",
					"blocker",
					"success",
					"failure",
					"optimization"
				],
				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-cwsxhn">`);
				Spinner($$renderer, { size: "md" });
				$$renderer.push(`<!----> <span>Loading patterns…</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 patterns",
						subtitle: error,
						variant: "warning",
						action});
				}
			} else if (patterns.length === 0) {
				$$renderer.push("<!--[2-->");
				{
					function action($$renderer) {
						Button($$renderer, {
							variant: "primary",
							size: "sm",
							onclick: () => modalOpen = true,
							children: ($$renderer) => {
								$$renderer.push(`<!---->Store a pattern`);
							}});
					}
					EmptyState($$renderer, {
						title: "No patterns match these filters",
						subtitle: "Loosen filters, or store the first pattern in this project.",
						action});
				}
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<ul class="list svelte-cwsxhn"><!--[-->`);
				const each_array = ensure_array_like(patterns);
				for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
					let p = each_array[$$index];
					$$renderer.push(`<li${attr_class("row svelte-cwsxhn", void 0, {
						"invalid": !!p.invalid_at,
						"prune": p.prune_candidate === 1
					})}><button class="row-head svelte-cwsxhn"${attr("aria-expanded", expanded === p.id)}><span class="row-meta svelte-cwsxhn"><code class="row-id svelte-cwsxhn">${escape_html(p.id)}</code> `);
					Badge($$renderer, {
						tone: TYPE_TONE[p.type] ?? "neutral",
						size: "sm",
						subtle: true,
						children: ($$renderer) => {
							$$renderer.push(`<!---->${escape_html(p.type)}`);
						}});
					$$renderer.push(`<!----> `);
					if (p.impact) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: IMPACT_TONE[p.impact] ?? "neutral",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->${escape_html(p.impact)} impact`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					TierBadge($$renderer, { tier: p.memory_tier });
					$$renderer.push(`<!----> `);
					if (p.verified === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "success",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->verified`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (p.prune_candidate === 1) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "warning",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->prune`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> `);
					if (p.invalid_at) {
						$$renderer.push("<!--[0-->");
						Badge($$renderer, {
							tone: "danger",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->invalidated`);
							}});
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> <span class="freq svelte-cwsxhn">${escape_html(p.frequency)}× seen</span> `);
					if (p.citation_count > 0) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<span class="cites svelte-cwsxhn">${escape_html(p.citation_count)} citations</span>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--></span> <span class="row-body svelte-cwsxhn"><span class="row-text svelte-cwsxhn">${escape_html(p.pattern)}</span> `);
					QualityBar($$renderer, {
						score: p.quality_score,
						width: 80
					});
					$$renderer.push(`<!----></span></button> `);
					if (expanded === p.id) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<div class="row-detail svelte-cwsxhn">`);
						if (p.context) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-cwsxhn"><span class="detail-label svelte-cwsxhn">Context</span> <p class="detail-text svelte-cwsxhn">${escape_html(p.context)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (p.anti_pattern) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-cwsxhn"><span class="detail-label svelte-cwsxhn">Anti-pattern</span> <p class="detail-text svelte-cwsxhn">${escape_html(p.anti_pattern)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> `);
						if (p.mitigation) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="detail-section svelte-cwsxhn"><span class="detail-label svelte-cwsxhn">Mitigation</span> <p class="detail-text svelte-cwsxhn">${escape_html(p.mitigation)}</p></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <div class="detail-footer svelte-cwsxhn"><span class="detail-ts svelte-cwsxhn">Extracted ${escape_html(p.extracted_at.slice(0, 10))}</span> `);
						if (typeof p.success_rate === "number") {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="detail-meta svelte-cwsxhn">Success rate ${escape_html((p.success_rate * 100).toFixed(0))}%</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,
					onChange: onPageChange
				});
				$$renderer.push(`<!---->`);
			}
			$$renderer.push(`<!--]--></section> `);
			PatternModal($$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-cwsxhn" 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-CophUar3.js.map
