import { x as head, c as escape_html, g as derived, e as ensure_array_like, b as attr_class, f as stringify, a as attr, y as bind_props } from './dev-BNpEUZI3.js';
import { H as HeroHeader } from './HeroHeader-v4Q9vaGr.js';
import { S as Sparkline } from './Sparkline-BJ-GuhRd.js';
import { S as StatBlock } from './StatBlock-DHJMmOCx.js';
import { B as Badge } from './Tabs-DzG6Bn9a.js';
import { C as ChipGroup, B as Button, b as Chip } from './ui-DR8zOSpC.js';

//#region src/lib/components/sessions/SessionTimeline.svelte
function SessionTimeline($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		/** Id of the expanded session, or null when all collapsed. */
		/** Toggle callback. */
		let { sessions, expandedId = null, onToggle } = $$props;
		function formatDuration(ms) {
			if (ms === null) return "—";
			const s = Math.floor(ms / 1e3);
			if (s < 60) return `${s}s`;
			const m = Math.floor(s / 60);
			if (m < 60) return `${m}m ${s % 60}s`;
			return `${Math.floor(m / 60)}h ${m % 60}m`;
		}
		function formatDate(iso) {
			try {
				return new Date(iso).toLocaleString("en-US", {
					month: "short",
					day: "numeric",
					hour: "2-digit",
					minute: "2-digit"
				});
			} catch {
				return iso;
			}
		}
		function statusTone(s) {
			if (s === "active") return "success";
			if (s === "ended") return "info";
			return "neutral";
		}
		/**
		* Build a visual activity series for the session. The tasks DB does
		* not persist token spend per hour, so we synthesise a gentle ramp
		* from `workedTasks` completion timestamps distributed across the
		* session duration. This is intentional: it gives the row visual
		* weight proportional to actual work without pretending to show
		* tokens.
		*/
		function synthActivity(session) {
			const buckets = new Array(12).fill(0);
			if (!session.endedAt && session.workedTasks.length === 0) return buckets;
			const startMs = new Date(session.startedAt).getTime();
			const endMs = new Date(session.endedAt ?? (/* @__PURE__ */ new Date()).toISOString()).getTime();
			const span = Math.max(1, endMs - startMs);
			for (const wt of session.workedTasks) try {
				const t = new Date(wt.setAt).getTime();
				const relative = Math.min(1, Math.max(0, (t - startMs) / span));
				const idx = Math.min(11, Math.floor(relative * buckets.length));
				const b = buckets[idx];
				if (typeof b === "number") buckets[idx] = b + 1;
			} catch {}
			return buckets;
		}
		function sessionHasTasks(s) {
			return s.completedTasks.length > 0 || s.workedTasks.length > 0 || s.currentTask !== null;
		}
		$$renderer.push(`<ol class="timeline svelte-yigptn" aria-label="Session timeline"><!--[-->`);
		const each_array = ensure_array_like(sessions);
		for (let $$index_2 = 0, $$length = each_array.length; $$index_2 < $$length; $$index_2++) {
			let sess = each_array[$$index_2];
			const expanded = expandedId === sess.id;
			$$renderer.push(`<li class="timeline-item svelte-yigptn"><div class="rail svelte-yigptn"><div${attr_class(`rail-dot rail-dot-${stringify(sess.status)}`, "svelte-yigptn")}></div> <div class="rail-line svelte-yigptn"></div></div> <article${attr_class("session svelte-yigptn", void 0, { "expanded": expanded })}><button type="button" class="session-head svelte-yigptn"${attr("aria-expanded", expanded)}><div class="head-top svelte-yigptn">`);
			Badge($$renderer, {
				tone: statusTone(sess.status),
				size: "sm",
				children: ($$renderer) => {
					$$renderer.push(`<!---->${escape_html(sess.status)}`);
				}});
			$$renderer.push(`<!----> <span class="head-name svelte-yigptn">${escape_html(sess.name ?? sess.id)}</span> `);
			if (sess.agent) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<code class="head-agent svelte-yigptn">${escape_html(sess.agent)}</code>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></div> <div class="head-meta svelte-yigptn"><time class="head-time svelte-yigptn">${escape_html(formatDate(sess.startedAt))}</time> `);
			if (sess.endedAt) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<span class="head-sep svelte-yigptn">→</span> <time class="head-time svelte-yigptn">${escape_html(formatDate(sess.endedAt))}</time> <span class="head-dur svelte-yigptn">${escape_html(formatDuration(sess.durationMs))}</span>`);
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<span class="head-dur head-dur-active svelte-yigptn">running</span>`);
			}
			$$renderer.push(`<!--]--></div> <div class="head-stats svelte-yigptn"><div class="stats-inline svelte-yigptn">`);
			if (sess.currentTask) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "accent",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->active: ${escape_html(sess.currentTask.id)}`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (sess.completedCount > 0) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "success",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->${escape_html(sess.completedCount)} done`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (sess.createdCount > 0) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "info",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->${escape_html(sess.createdCount)} new`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (sess.workedTasks.length > 0) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "warning",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->${escape_html(sess.workedTasks.length)} worked`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (!sessionHasTasks(sess)) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "neutral",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->no tasks`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></div> `);
			Sparkline($$renderer, {
				points: synthActivity(sess),
				width: 80,
				height: 20,
				tone: sess.status === "active" ? "success" : "accent",
				strokeWidth: 1,
				fill: false,
				ariaLabel: "Session activity"
			});
			$$renderer.push(`<!----></div></button> `);
			if (expanded) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="session-body svelte-yigptn">`);
				if (sess.currentTask) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<section class="expand-section svelte-yigptn"><h3 class="expand-label svelte-yigptn">Active task</h3> <a class="expand-row expand-row-active svelte-yigptn"${attr("href", `/tasks/${sess.currentTask.id}`)}><span class="row-id svelte-yigptn">${escape_html(sess.currentTask.id)}</span> <span class="row-title svelte-yigptn">${escape_html(sess.currentTask.title)}</span> `);
					Badge($$renderer, {
						tone: "accent",
						size: "sm",
						children: ($$renderer) => {
							$$renderer.push(`<!---->in progress`);
						}});
					$$renderer.push(`<!----></a></section>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (sess.workedTasks.length > 0) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<section class="expand-section svelte-yigptn"><h3 class="expand-label svelte-yigptn">Work history</h3> <div class="expand-list svelte-yigptn"><!--[-->`);
					const each_array_1 = ensure_array_like(sess.workedTasks);
					for (let $$index = 0, $$length = each_array_1.length; $$index < $$length; $$index++) {
						let t = each_array_1[$$index];
						$$renderer.push(`<a class="expand-row svelte-yigptn"${attr("href", `/tasks/${t.id}`)}><span class="row-id svelte-yigptn">${escape_html(t.id)}</span> <span class="row-title svelte-yigptn">${escape_html(t.title)}</span> <time class="row-time svelte-yigptn">${escape_html(formatDate(t.setAt))}</time> `);
						Badge($$renderer, {
							tone: t.clearedAt ? "success" : "info",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->${escape_html(t.clearedAt ? "done" : "active")}`);
							}});
						$$renderer.push(`<!----></a>`);
					}
					$$renderer.push(`<!--]--></div></section>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (sess.completedTasks.length > 0) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<section class="expand-section svelte-yigptn"><h3 class="expand-label svelte-yigptn">Completed</h3> <div class="expand-list svelte-yigptn"><!--[-->`);
					const each_array_2 = ensure_array_like(sess.completedTasks);
					for (let $$index_1 = 0, $$length = each_array_2.length; $$index_1 < $$length; $$index_1++) {
						let t = each_array_2[$$index_1];
						$$renderer.push(`<a class="expand-row svelte-yigptn"${attr("href", `/tasks/${t.id}`)}><span class="row-id svelte-yigptn">${escape_html(t.id)}</span> <span class="row-title svelte-yigptn">${escape_html(t.title)}</span> `);
						Badge($$renderer, {
							tone: "success",
							size: "sm",
							children: ($$renderer) => {
								$$renderer.push(`<!---->${escape_html(t.status)}`);
							}});
						$$renderer.push(`<!----></a>`);
					}
					$$renderer.push(`<!--]--></div></section>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--></div>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></article></li>`);
		}
		$$renderer.push(`<!--]--></ol>`);
		bind_props($$props, { expandedId });
	});
}
//#endregion
//#region src/routes/tasks/sessions/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		const sessions = derived(() => data.sessions ?? []);
		let statusFilter = "all";
		let hideEmpty = false;
		const filtered = derived(() => sessions().filter((s) => {
			if (statusFilter === "active" && s.status !== "active") return false;
			if (statusFilter === "ended" && s.status !== "ended") return false;
			if (hideEmpty && s.completedCount === 0 && s.createdCount === 0 && s.workedTasks.length === 0 && s.currentTask === null) return false;
			return true;
		}));
		let expandedId = null;
		const totalCompleted = derived(() => sessions().reduce((sum, s) => sum + s.completedCount, 0));
		const totalCreated = derived(() => sessions().reduce((sum, s) => sum + s.createdCount, 0));
		const activeSessions = derived(() => sessions().filter((s) => s.status === "active").length);
		const statusOptions = [
			{
				value: "all",
				label: "All"
			},
			{
				value: "active",
				label: "Active"
			},
			{
				value: "ended",
				label: "Ended"
			}
		];
		let $$settled = true;
		let $$inner_renderer;
		function $$render_inner($$renderer) {
			head("1x47y1z", $$renderer, ($$renderer) => {
				$$renderer.title(($$renderer) => {
					$$renderer.push(`<title>Sessions — CLEO Studio</title>`);
				});
			});
			$$renderer.push(`<div class="sessions-view svelte-1x47y1z">`);
			{
				function actions($$renderer) {
					Button($$renderer, {
						variant: "ghost",
						size: "sm",
						href: "/tasks",
						children: ($$renderer) => {
							$$renderer.push(`<!---->← Tasks`);
						}});
					$$renderer.push(`<!----> `);
					Button($$renderer, {
						variant: "ghost",
						size: "sm",
						href: "/tasks/pipeline",
						children: ($$renderer) => {
							$$renderer.push(`<!---->Pipeline`);
						}});
					$$renderer.push(`<!---->`);
				}
				HeroHeader($$renderer, {
					eyebrow: "TASK SESSIONS",
					title: "Sessions",
					subtitle: "Chronological record of every CLEO session — task completions, work history, duration.",
					liveIndicator: activeSessions() > 0,
					actions});
			}
			$$renderer.push(`<!----> <div class="summary-row svelte-1x47y1z">`);
			StatBlock($$renderer, {
				label: "Sessions",
				value: sessions().length,
				tone: "accent"
			});
			$$renderer.push(`<!----> `);
			StatBlock($$renderer, {
				label: "Active",
				value: activeSessions(),
				tone: activeSessions() > 0 ? "success" : "neutral",
				hint: "right now"
			});
			$$renderer.push(`<!----> `);
			StatBlock($$renderer, {
				label: "Completed",
				value: totalCompleted(),
				tone: "success"
			});
			$$renderer.push(`<!----> `);
			StatBlock($$renderer, {
				label: "Created",
				value: totalCreated(),
				tone: "info"
			});
			$$renderer.push(`<!----></div> <div class="filter-bar svelte-1x47y1z">`);
			ChipGroup($$renderer, {
				label: "Status",
				children: ($$renderer) => {
					$$renderer.push(`<!--[-->`);
					const each_array = ensure_array_like(statusOptions);
					for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
						let opt = each_array[$$index];
						Chip($$renderer, {
							mode: "toggle",
							active: statusFilter === opt.value,
							onclick: () => {
								statusFilter = opt.value;
							},
							children: ($$renderer) => {
								$$renderer.push(`<!---->${escape_html(opt.label)}`);
							},
							$$slots: { default: true }
						});
					}
					$$renderer.push(`<!--]-->`);
				}});
			$$renderer.push(`<!----> `);
			ChipGroup($$renderer, {
				label: "Display",
				children: ($$renderer) => {
					Chip($$renderer, {
						mode: "toggle",
						active: hideEmpty,
						onclick: () => {
							hideEmpty = !hideEmpty;
						},
						children: ($$renderer) => {
							$$renderer.push(`<!---->Hide empty`);
						},
						$$slots: { default: true }
					});
				}});
			$$renderer.push(`<!----> `);
			if (filtered().length !== sessions().length) {
				$$renderer.push("<!--[0-->");
				Badge($$renderer, {
					tone: "info",
					size: "sm",
					children: ($$renderer) => {
						$$renderer.push(`<!---->${escape_html(filtered().length)} / ${escape_html(sessions().length)}`);
					}});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></div> `);
			if (filtered().length === 0) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="empty-state svelte-1x47y1z">${escape_html(sessions().length === 0 ? "No sessions recorded in tasks.db." : "No sessions match the active filters.")}</div>`);
			} else {
				$$renderer.push("<!--[-1-->");
				SessionTimeline($$renderer, {
					sessions: filtered(),
					get expandedId() {
						return expandedId;
					},
					set expandedId($$value) {
						expandedId = $$value;
						$$settled = false;
					}
				});
			}
			$$renderer.push(`<!--]--></div>`);
		}
		do {
			$$settled = true;
			$$inner_renderer = $$renderer.copy();
			$$render_inner($$inner_renderer);
		} while (!$$settled);
		$$renderer.subsume($$inner_renderer);
	});
}

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