import { E as head, r as escape_html, p as ensure_array_like, d as attr, e as attr_class, o as derived } from './dev-B6xUe35c.js';
import 'node:module';
import { g as goto } from './client-BVhtM1mG.js';
import { p as page } from './state2-boKye0ZJ.js';
import { N as Navbar, c as chat, E as EndpointList, S as SessionList, e as endpointsService, I as IconButton } from './Navbar-y0AyDpcy.js';
import { S as Spinner } from './Spinner-Bk6e83RX.js';
import { C as ChatMessage, T as ToolStrip } from './ChatMessage-BEFlmHur.js';
import './internal-DQFuMtuQ.js';
import './utils-BSRjJDrZ.js';
import './chunk-CLZ62Ad-.js';
import './theme-state.svelte-BsNoJAjT.js';

//#region src/lib/components/chat/NewChatButton.svelte
function plus($$renderer) {
	$$renderer.push(`<svg aria-hidden="true" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14"></path><path d="M5 12h14"></path></svg>`);
}
function NewChatButton($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		const onChat = derived(() => (page.url?.pathname ?? "") === "/chat" || (page.url?.pathname ?? "").startsWith("/chat/"));
		let starting = false;
		async function newChat() {
			if (starting) return;
			starting = true;
			try {
				if (onChat()) await chat.startNewSession();
				else await goto("/chat?new=1");
			} finally {
				starting = false;
			}
		}
		IconButton($$renderer, {
			icon: plus,
			ariaLabel: "Start a new chat",
			title: "New chat",
			disabled: starting,
			onclick: newChat
		});
	});
}
//#endregion
//#region src/lib/components/chat/ChatInput.svelte
function ChatInput($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { sending, questionPending = false} = $$props;
		let inputText = "";
		const inputDisabled = derived(() => sending && !questionPending);
		const actionLabel = derived(() => questionPending ? "Answer" : "Send");
		$$renderer.push(`<div class="chat-input-row svelte-j7h4bp"><div class="input-area svelte-j7h4bp">`);
		NewChatButton($$renderer);
		$$renderer.push(`<!----> <textarea id="chat-input"${attr("placeholder", questionPending ? "Answer the assistant… (Enter to send, Shift+Enter for newline)" : "Send a message… (Enter to send, Shift+Enter for newline)")} rows="1"${attr("disabled", inputDisabled(), true)} aria-label="Message input" class="svelte-j7h4bp">`);
		const $$body = escape_html(inputText);
		if ($$body) $$renderer.push(`${$$body}`);
		$$renderer.push(`</textarea> <button class="btn btn-primary send-btn svelte-j7h4bp" type="button"${attr("disabled", inputDisabled() || !inputText.trim(), true)}${attr("aria-label", questionPending ? "Send answer" : "Send message")}>`);
		if (sending && !questionPending) {
			$$renderer.push("<!--[0-->");
			Spinner($$renderer, {});
			$$renderer.push(`<!----> <span>Sending...</span>`);
		} else {
			$$renderer.push("<!--[-1-->");
			$$renderer.push(`<svg aria-hidden="true" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg> <span>${escape_html(actionLabel())}</span>`);
		}
		$$renderer.push(`<!--]--></button></div></div>`);
	});
}
//#endregion
//#region src/routes/chat/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		const entriesLoading = derived(() => chat.entriesLoading);
		const sessionsLoading = derived(() => chat.byEndpoint.get(chat.activeEndpointId)?.sessionsLoading ?? false);
		const activeEndpointLabel = derived(() => endpointsService.active?.label ?? "Assistant");
		const activeSessionLabel = derived(() => {
			return (chat.byEndpoint.get(chat.activeEndpointId)?.sessions.find((item) => item.id === chat.activeSessionId))?.title?.trim() || "New session";
		});
		function liveStatusText() {
			if (chat.pendingPermission) return "Assistant paused for approval";
			if (chat.pendingQuestion) return "Assistant is waiting for your answer";
			if (chat.pendingAssistantText) return "Assistant is responding";
			return "Assistant is typing";
		}
		function clamp(text, max = 160) {
			return text.length > max ? `${text.slice(0, max - 1)}…` : text;
		}
		head("23dtxz", $$renderer, ($$renderer) => {
			$$renderer.title(($$renderer) => {
				$$renderer.push(`<title>Chat — OpenPalm</title>`);
			});
		});
		Navbar($$renderer);
		$$renderer.push(`<!----> <div class="chat-shell svelte-23dtxz"><div class="chat-layout svelte-23dtxz"><section class="messages-area svelte-23dtxz" aria-label="Chat history"><header class="chat-intro svelte-23dtxz" aria-label="Chat overview"><div class="chat-intro-copy svelte-23dtxz"><div class="chat-intro-eyebrow svelte-23dtxz">OpenPalm chat</div> <h1 class="svelte-23dtxz">Talk to your assistant</h1> <p class="svelte-23dtxz">You're chatting with <strong class="svelte-23dtxz">${escape_html(activeEndpointLabel())}</strong> in <strong class="svelte-23dtxz">${escape_html(activeSessionLabel())}</strong>.
            Ask for help in plain language and review the live output below.</p></div></header> `);
		if (sessionsLoading() || entriesLoading()) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="session-loading svelte-23dtxz" aria-live="polite">`);
			Spinner($$renderer, {});
			$$renderer.push(`<!----> <span class="svelte-23dtxz">Loading messages…</span></div>`);
		} else if (chat.entries.length === 0) {
			$$renderer.push("<!--[1-->");
			$$renderer.push(`<div class="empty-state svelte-23dtxz"><p class="svelte-23dtxz">No messages yet. Send something to begin.</p></div>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> <!--[-->`);
		const each_array = ensure_array_like(chat.entries);
		for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
			let entry = each_array[$$index];
			ChatMessage($$renderer, { entry });
		}
		$$renderer.push(`<!--]--> `);
		if (chat.sending) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="typing-message svelte-23dtxz" aria-live="polite"${attr("aria-label", liveStatusText())}><div class="typing-bubble svelte-23dtxz">`);
			if (chat.pendingAssistantText) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<p class="typing-text typing-text-streaming svelte-23dtxz">${escape_html(chat.pendingAssistantText)}</p>`);
			} else {
				$$renderer.push("<!--[-1-->");
				$$renderer.push(`<div class="typing-state-row svelte-23dtxz"><span class="typing-text svelte-23dtxz">${escape_html(liveStatusText())}</span> `);
				if (!chat.pendingPermission && !chat.pendingQuestion) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<span class="typing-indicator svelte-23dtxz" aria-hidden="true"><span class="typing-dot svelte-23dtxz"></span> <span class="typing-dot svelte-23dtxz"></span> <span class="typing-dot svelte-23dtxz"></span></span>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--></div>`);
			}
			$$renderer.push(`<!--]--> `);
			if (chat.pendingToolStates.length > 0) {
				$$renderer.push("<!--[0-->");
				ToolStrip($$renderer, {
					items: chat.pendingToolStates,
					muted: !!chat.pendingPermission || !!chat.pendingQuestion,
					bordered: true,
					ariaLabel: "Assistant tool activity"
				});
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (chat.pendingPermission) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="live-card permission-card svelte-23dtxz" role="group" aria-label="Permission request"><div class="live-card-header svelte-23dtxz"><span class="live-card-kicker svelte-23dtxz">Permission request</span> <span class="live-card-title svelte-23dtxz">${escape_html(chat.pendingPermission.permission)}</span></div> `);
				if (chat.pendingPermission.detail) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<p class="live-card-message svelte-23dtxz">${escape_html(clamp(chat.pendingPermission.detail))}</p>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (chat.pendingPermission.patterns.length > 0) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<div class="svelte-23dtxz"><span class="live-card-label svelte-23dtxz">Requested now</span> <code class="live-card-code svelte-23dtxz">${escape_html(chat.pendingPermission.patterns.join(", "))}</code></div>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (chat.pendingPermission.always.length > 0) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<div class="svelte-23dtxz"><span class="live-card-label svelte-23dtxz">Saved if always allowed</span> <code class="live-card-code svelte-23dtxz">${escape_html(chat.pendingPermission.always.join(", "))}</code></div>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (chat.pendingPermission.message) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<p class="live-card-message svelte-23dtxz">${escape_html(chat.pendingPermission.message)}</p>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> <div class="live-card-actions svelte-23dtxz"><button class="btn btn-primary btn-sm svelte-23dtxz" type="button"${attr("disabled", chat.pendingPermission.status === "submitting" || chat.pendingPermission.status === "resolved", true)}>${escape_html("Allow this once")}</button> <button class="btn btn-secondary btn-sm svelte-23dtxz" type="button"${attr("disabled", chat.pendingPermission.status === "submitting" || chat.pendingPermission.status === "resolved", true)}>${escape_html("Always allow matches")}</button> <button class="btn btn-danger btn-sm svelte-23dtxz" type="button"${attr("disabled", chat.pendingPermission.status === "submitting" || chat.pendingPermission.status === "resolved", true)}>${escape_html("Deny request")}</button></div></div>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--> `);
			if (chat.pendingQuestion) {
				$$renderer.push("<!--[0-->");
				$$renderer.push(`<div class="live-card question-card svelte-23dtxz" role="group" aria-label="Assistant question"><div class="live-card-header svelte-23dtxz"><span class="live-card-kicker svelte-23dtxz">Assistant question</span> `);
				if (chat.pendingQuestion.questions.length === 1 && chat.pendingQuestion.questions[0]?.header) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<span class="live-card-title svelte-23dtxz">${escape_html(chat.pendingQuestion.questions[0].header)}</span>`);
				} else if (chat.pendingQuestion.questions.length > 1) {
					$$renderer.push("<!--[1-->");
					$$renderer.push(`<span class="live-card-title svelte-23dtxz">${escape_html(chat.pendingQuestion.questions.length)} answers required</span>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--></div> `);
				if (chat.pendingQuestion.questions.length === 1 && chat.pendingQuestion.questions[0]) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<p class="live-card-question svelte-23dtxz">${escape_html(chat.pendingQuestion.questions[0].question)}</p> `);
					if (chat.pendingQuestion.questions[0].options.length > 0) {
						$$renderer.push("<!--[0-->");
						$$renderer.push(`<div class="question-options svelte-23dtxz"><!--[-->`);
						const each_array_1 = ensure_array_like(chat.pendingQuestion.questions[0].options);
						for (let index = 0, $$length = each_array_1.length; index < $$length; index++) {
							let option = each_array_1[index];
							$$renderer.push(`<button class="btn btn-secondary btn-sm question-option svelte-23dtxz" type="button"${attr("disabled", chat.pendingQuestion.status === "submitting" || chat.pendingQuestion.status === "answered" || chat.pendingQuestion.status === "rejected", true)}><span class="svelte-23dtxz">${escape_html(option.label)}</span> `);
							if (option.description) {
								$$renderer.push("<!--[0-->");
								$$renderer.push(`<span class="question-option-description svelte-23dtxz">${escape_html(option.description)}</span>`);
							} else $$renderer.push("<!--[-1-->");
							$$renderer.push(`<!--]--></button>`);
						}
						$$renderer.push(`<!--]--></div>`);
					} else $$renderer.push("<!--[-1-->");
					$$renderer.push(`<!--]--> <p class="live-card-hint svelte-23dtxz">Use the composer below to send a custom answer.</p>`);
				} else {
					$$renderer.push("<!--[-1-->");
					$$renderer.push(`<div class="multi-question-list svelte-23dtxz"><!--[-->`);
					const each_array_2 = ensure_array_like(chat.pendingQuestion.questions);
					for (let index = 0, $$length = each_array_2.length; index < $$length; index++) {
						let question = each_array_2[index];
						$$renderer.push(`<div class="multi-question-item svelte-23dtxz">`);
						if (question.header) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<span class="live-card-label svelte-23dtxz">${escape_html(question.header)}</span>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <p class="live-card-question svelte-23dtxz">${escape_html(question.question)}</p> `);
						if (question.options.length > 0) {
							$$renderer.push("<!--[0-->");
							$$renderer.push(`<div class="question-options svelte-23dtxz"><!--[-->`);
							const each_array_3 = ensure_array_like(question.options);
							for (let optionIndex = 0, $$length = each_array_3.length; optionIndex < $$length; optionIndex++) {
								let option = each_array_3[optionIndex];
								$$renderer.push(`<button${attr_class("btn btn-secondary btn-sm question-option svelte-23dtxz", void 0, { "selected-option": chat.pendingQuestion.answers[index] === option.label })} type="button"${attr("disabled", chat.pendingQuestion.status === "submitting" || chat.pendingQuestion.status === "answered" || chat.pendingQuestion.status === "rejected", true)}><span class="svelte-23dtxz">${escape_html(option.label)}</span> `);
								if (option.description) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<span class="question-option-description svelte-23dtxz">${escape_html(option.description)}</span>`);
								} else $$renderer.push("<!--[-1-->");
								$$renderer.push(`<!--]--></button>`);
							}
							$$renderer.push(`<!--]--></div>`);
						} else $$renderer.push("<!--[-1-->");
						$$renderer.push(`<!--]--> <input class="question-input svelte-23dtxz" type="text"${attr("value", chat.pendingQuestion.answers[index])} placeholder="Type an answer" aria-required="true"${attr("aria-invalid", chat.pendingQuestion.status === "error" ? !chat.pendingQuestion.answers[index] : void 0)}${attr("disabled", chat.pendingQuestion.status === "submitting" || chat.pendingQuestion.status === "answered" || chat.pendingQuestion.status === "rejected", true)}/></div>`);
					}
					$$renderer.push(`<!--]--></div>`);
				}
				$$renderer.push(`<!--]--> `);
				if (chat.pendingQuestion.answers.some((answer) => answer)) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<code class="live-card-code svelte-23dtxz">${escape_html(chat.pendingQuestion.answers.filter(Boolean).join(" | "))}</code>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> `);
				if (chat.pendingQuestion.message) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<p class="live-card-message svelte-23dtxz">${escape_html(chat.pendingQuestion.message)}</p>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> <div class="live-card-actions svelte-23dtxz">`);
				if (chat.pendingQuestion.questions.length > 1) {
					$$renderer.push("<!--[0-->");
					$$renderer.push(`<button class="btn btn-primary btn-sm svelte-23dtxz" type="button"${attr("disabled", chat.pendingQuestion.status === "submitting" || chat.pendingQuestion.status === "answered" || chat.pendingQuestion.status === "rejected", true)}>Submit answers</button>`);
				} else $$renderer.push("<!--[-1-->");
				$$renderer.push(`<!--]--> <button class="btn btn-secondary btn-sm svelte-23dtxz" type="button"${attr("disabled", chat.pendingQuestion.status === "submitting" || chat.pendingQuestion.status === "answered" || chat.pendingQuestion.status === "rejected", true)}>Can't answer</button></div></div>`);
			} else $$renderer.push("<!--[-1-->");
			$$renderer.push(`<!--]--></div> <span class="typing-meta svelte-23dtxz">Assistant</span></div>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> <div aria-hidden="true" class="svelte-23dtxz"></div></section> `);
		if (chat.error) {
			$$renderer.push("<!--[0-->");
			$$renderer.push(`<div class="chat-error-banner svelte-23dtxz" role="alert"><span class="svelte-23dtxz">${escape_html(chat.error)}</span> <button class="reconnect-btn svelte-23dtxz" type="button">Reconnect</button> <button class="dismiss-btn svelte-23dtxz" type="button" aria-label="Dismiss error">×</button></div>`);
		} else $$renderer.push("<!--[-1-->");
		$$renderer.push(`<!--]--> `);
		ChatInput($$renderer, {
			sending: chat.sending,
			questionPending: !!chat.pendingQuestion && chat.pendingQuestion.questions.length === 1});
		$$renderer.push(`<!----></div> <aside class="chat-side svelte-23dtxz" aria-label="Assistant and sessions"><section class="panel side-panel svelte-23dtxz"><div class="panel-header side-panel-header svelte-23dtxz"><div class="svelte-23dtxz"><div class="chat-side-eyebrow svelte-23dtxz">Connection</div> <h2 class="svelte-23dtxz">Assistant</h2></div></div> <div class="panel-body side-panel-body svelte-23dtxz">`);
		EndpointList($$renderer, {});
		$$renderer.push(`<!----></div></section> <section class="panel side-panel side-sessions svelte-23dtxz"><div class="panel-header side-panel-header svelte-23dtxz"><div class="svelte-23dtxz"><div class="chat-side-eyebrow svelte-23dtxz">History</div> <h2 class="svelte-23dtxz">Sessions</h2></div></div> <div class="panel-body side-panel-body side-panel-body--sessions svelte-23dtxz">`);
		SessionList($$renderer, {});
		$$renderer.push(`<!----></div></section></aside></div>`);
	});
}

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