import { x as head, e as ensure_array_like, g as derived, c as escape_html, a as attr } from './dev-BNpEUZI3.js';
import { C as Card, B as Badge } from './Tabs-DzG6Bn9a.js';

//#region src/routes/settings/vault/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		/** Group connections by provider for display (stable provider-asc order). */
		const groupedConnections = derived(() => {
			const groups = /* @__PURE__ */ new Map();
			for (const conn of data.connections) {
				const list = groups.get(conn.provider);
				if (list) list.push(conn);
				else groups.set(conn.provider, [conn]);
			}
			return Array.from(groups.entries()).sort(([a], [b]) => a.localeCompare(b)).map(([provider, list]) => ({
				provider,
				connections: list.slice().sort((a, b) => a.label.localeCompare(b.label))
			}));
		});
		/** Grants keyed by `provider:label` so they render under their connection. */
		const grantsByConnection = derived(() => {
			const map = /* @__PURE__ */ new Map();
			for (const g of data.grants) {
				if (g.provider === null || g.label === null) continue;
				const key = `${g.provider}:${g.label}`;
				const list = map.get(key);
				if (list) list.push(g);
				else map.set(key, [g]);
			}
			return map;
		});
		/** Grants whose connection was deleted out-of-band (orphans). */
		const orphanGrants = derived(() => data.grants.filter((g) => g.provider === null || g.label === null));
		function statusTone(status) {
			if (status === "active") return "success";
			if (status === "expired") return "warning";
			if (status === "revoked") return "danger";
			return "neutral";
		}
		function formatExpiry(expiresAt) {
			if (expiresAt === null) return "never";
			const ms = Date.parse(expiresAt) - Date.now();
			if (!Number.isFinite(ms)) return expiresAt;
			if (ms < 0) return "expired";
			const hours = Math.round(ms / 36e5);
			if (hours < 48) return `${hours}h`;
			return `${Math.round(hours / 24)}d`;
		}
		head("1p03kmz", $$renderer, ($$renderer) => {
			$$renderer.title(($$renderer) => {
				$$renderer.push(`<title>Service Vault — CLEO Studio</title>`);
			});
			$$renderer.push(`<meta name="robots" content="noindex"/>`);
		});
		$$renderer.push(`<section class="vault-page svelte-1p03kmz"><header class="page-header svelte-1p03kmz"><div><span class="eyebrow svelte-1p03kmz">SERVICE VAULT</span> <h1 class="svelte-1p03kmz">Connected services</h1> <p class="muted svelte-1p03kmz">Read-only view of the global service vault — connections + per-agent grants. Secrets never
        leave core; this dashboard shows identity, scopes, and status only. Manage connections via <code class="svelte-1p03kmz">cleo service connect</code> / <code class="svelte-1p03kmz">cleo service revoke</code>.</p></div></header> `);
		if (data.error) {
			$$renderer.push("<!--[0-->");
			Card($$renderer, {
				padding: "cozy",
				children: ($$renderer) => {
					$$renderer.push(`<div class="error svelte-1p03kmz" role="alert">Failed to load the vault: ${escape_html(data.error)}</div>`);
				}});
		} else if (data.connections.length === 0) {
			$$renderer.push("<!--[1-->");
			Card($$renderer, {
				padding: "cozy",
				children: ($$renderer) => {
					$$renderer.push(`<p class="muted svelte-1p03kmz">No service connections yet. Connect one with <code class="svelte-1p03kmz">cleo service connect &lt;provider></code> from the CLI.</p>`);
				}});
		} else {
			$$renderer.push("<!--[-1-->");
			$$renderer.push(`<div class="groups svelte-1p03kmz"><!--[-->`);
			const each_array = ensure_array_like(groupedConnections());
			for (let $$index_3 = 0, $$length = each_array.length; $$index_3 < $$length; $$index_3++) {
				let group = each_array[$$index_3];
				{
					function header($$renderer) {
						$$renderer.push(`<h2 class="group-title svelte-1p03kmz">${escape_html(group.provider)} <span class="muted svelte-1p03kmz">· ${escape_html(group.connections.length)}
                ${escape_html(group.connections.length === 1 ? "connection" : "connections")}</span></h2>`);
					}
					Card($$renderer, {
						padding: "cozy",
						header,
						children: ($$renderer) => {
							$$renderer.push(`<table class="conns svelte-1p03kmz"><thead><tr><th scope="col" class="svelte-1p03kmz">Label</th><th scope="col" class="svelte-1p03kmz">Status</th><th scope="col" class="svelte-1p03kmz">Scopes</th><th scope="col" class="svelte-1p03kmz">Expiry</th><th scope="col" class="svelte-1p03kmz">Credential</th><th scope="col" class="svelte-1p03kmz">Agent grants</th></tr></thead><tbody><!--[-->`);
							const each_array_1 = ensure_array_like(group.connections);
							for (let $$index_2 = 0, $$length = each_array_1.length; $$index_2 < $$length; $$index_2++) {
								let conn = each_array_1[$$index_2];
								const grants = grantsByConnection().get(`${conn.provider}:${conn.label}`) ?? [];
								$$renderer.push(`<tr><td class="svelte-1p03kmz"><code class="svelte-1p03kmz">${escape_html(conn.label)}</code></td><td class="svelte-1p03kmz">`);
								Badge($$renderer, {
									tone: statusTone(conn.status),
									size: "sm",
									children: ($$renderer) => {
										$$renderer.push(`<!---->${escape_html(conn.status)}`);
									}});
								$$renderer.push(`<!----></td><td class="scopes svelte-1p03kmz">`);
								if (conn.scopes.length === 0) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<span class="muted svelte-1p03kmz">—</span>`);
								} else {
									$$renderer.push("<!--[-1-->");
									$$renderer.push(`<!--[-->`);
									const each_array_2 = ensure_array_like(conn.scopes);
									for (let $$index = 0, $$length = each_array_2.length; $$index < $$length; $$index++) {
										let scope = each_array_2[$$index];
										Badge($$renderer, {
											tone: "neutral",
											size: "sm",
											children: ($$renderer) => {
												$$renderer.push(`<!---->${escape_html(scope)}`);
											}});
									}
									$$renderer.push(`<!--]-->`);
								}
								$$renderer.push(`<!--]--></td><td class="svelte-1p03kmz">${escape_html(formatExpiry(conn.expiresAt))}</td><td class="svelte-1p03kmz">`);
								if (conn.hasCredentials) {
									$$renderer.push("<!--[0-->");
									Badge($$renderer, {
										tone: "info",
										size: "sm",
										children: ($$renderer) => {
											$$renderer.push(`<!---->stored`);
										}});
								} else {
									$$renderer.push("<!--[-1-->");
									$$renderer.push(`<span class="muted svelte-1p03kmz">none</span>`);
								}
								$$renderer.push(`<!--]--></td><td class="grants svelte-1p03kmz">`);
								if (grants.length === 0) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<span class="muted svelte-1p03kmz">no grants</span>`);
								} else {
									$$renderer.push("<!--[-1-->");
									$$renderer.push(`<!--[-->`);
									const each_array_3 = ensure_array_like(grants);
									for (let $$index_1 = 0, $$length = each_array_3.length; $$index_1 < $$length; $$index_1++) {
										let grant = each_array_3[$$index_1];
										$$renderer.push(`<span class="grant svelte-1p03kmz"${attr("title", `policy: ${grant.mode}`)}><code class="svelte-1p03kmz">${escape_html(grant.agentId)}</code> `);
										Badge($$renderer, {
											tone: grant.mode === "block" ? "danger" : "success",
											size: "sm",
											children: ($$renderer) => {
												$$renderer.push(`<!---->${escape_html(grant.mode)}`);
											}});
										$$renderer.push(`<!----> `);
										if (grant.manualApproval) {
											$$renderer.push("<!--[0-->");
											Badge($$renderer, {
												tone: "warning",
												size: "sm",
												children: ($$renderer) => {
													$$renderer.push(`<!---->manual`);
												}});
										} else $$renderer.push("<!--[-1-->");
										$$renderer.push(`<!--]--></span>`);
									}
									$$renderer.push(`<!--]-->`);
								}
								$$renderer.push(`<!--]--></td></tr>`);
							}
							$$renderer.push(`<!--]--></tbody></table>`);
						}});
				}
			}
			$$renderer.push(`<!--]--></div> `);
			if (orphanGrants().length > 0) {
				$$renderer.push("<!--[0-->");
				{
					function header($$renderer) {
						$$renderer.push(`<h2 class="group-title svelte-1p03kmz">Orphan grants <span class="muted svelte-1p03kmz">· ${escape_html(orphanGrants().length)} (connection deleted)</span></h2>`);
					}
					Card($$renderer, {
						padding: "cozy",
						header,
						children: ($$renderer) => {
							$$renderer.push(`<ul class="orphans svelte-1p03kmz"><!--[-->`);
							const each_array_4 = ensure_array_like(orphanGrants());
							for (let i = 0, $$length = each_array_4.length; i < $$length; i++) {
								let grant = each_array_4[i];
								$$renderer.push(`<li><code class="svelte-1p03kmz">${escape_html(grant.agentId)}</code> → connection #${escape_html(grant.provider === null ? "(deleted)" : `${grant.provider}:${grant.label}`)} `);
								Badge($$renderer, {
									tone: grant.mode === "block" ? "danger" : "neutral",
									size: "sm",
									children: ($$renderer) => {
										$$renderer.push(`<!---->${escape_html(grant.mode)}`);
									}});
								$$renderer.push(`<!----></li>`);
							}
							$$renderer.push(`<!--]--></ul>`);
						}});
				}
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]-->`);
		}
		$$renderer.push(`<!--]--></section>`);
	});
}

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