import { searchableSelectListTheme, theme } from "./theme.js";
import { SearchableSelectList } from "./components/searchable-select-list.js";
import { ScopedModelsSelector } from "./components/scoped-models-selector.js";
import { SettingsSelector } from "./components/settings-selector.js";
import { SessionSelector } from "./components/session-selector.js";
//#region src/tui/tui-picker-overlay.ts
function resumeSession(svc, sessionKey) {
	svc.setSessionKey(sessionKey);
	svc.clearChatForSessionSwitch();
	svc.chatLog.addSystem(`Session: ${sessionKey}`);
	svc.refreshSessionInfo().then(() => svc.loadSessionHistory()).then(() => {
		svc.updateHeader();
		svc.tui.requestRender();
	});
	svc.tui.requestRender();
}
/** Ctrl+L — pick model, sends `/switch provider/id`. */
async function openModelPickerOverlay(svc) {
	const models = await svc.client.listModels();
	svc.setModelChoices(models);
	if (models.length === 0) {
		svc.chatLog.addSystem("No models available from gateway.");
		svc.tui.requestRender();
		return;
	}
	const items = models.map((m) => ({
		value: `${m.provider}/${m.id}`,
		label: m.name || m.id,
		description: m.provider,
		searchText: `${m.provider} ${m.id} ${m.name ?? ""}`
	}));
	const list = new SearchableSelectList(items, Math.min(10, items.length), searchableSelectListTheme);
	list.onSelect = (item) => {
		svc.closeOverlay();
		svc.tui.setFocus(svc.editor);
		svc.sendMessage(`/switch ${item.value}`);
		svc.tui.requestRender();
	};
	list.onCancel = () => {
		svc.closeOverlay();
		svc.tui.setFocus(svc.editor);
		svc.tui.requestRender();
	};
	svc.openOverlay(list);
	svc.chatLog.addSystem(theme.dim("Select model (↑/↓ · type to filter · Esc)"));
	svc.tui.requestRender();
}
/** Ctrl+Shift+P — session picker with rename/delete. */
async function openSessionPickerOverlay(svc) {
	const sessions = await svc.client.listSessions();
	if (sessions.length === 0) {
		svc.chatLog.addSystem("No sessions listed.");
		svc.tui.requestRender();
		return;
	}
	const selector = new SessionSelector(sessions, {
		onResume: (sessionKey) => {
			svc.closeOverlay();
			svc.tui.setFocus(svc.editor);
			resumeSession(svc, sessionKey);
		},
		onRename: async (sessionKey, name) => {
			return (await svc.client.renameSession(sessionKey, name)).ok ? { ok: true } : {
				ok: false,
				error: "Rename failed"
			};
		},
		onDelete: async (sessionKey) => {
			if (sessionKey === svc.state.currentSessionKey) return {
				ok: false,
				error: "Switch away before deleting the active session"
			};
			return (await svc.client.deleteSession(sessionKey)).ok ? { ok: true } : {
				ok: false,
				error: "Delete failed"
			};
		},
		onCancel: () => {
			svc.closeOverlay();
			svc.tui.setFocus(svc.editor);
			svc.tui.requestRender();
		},
		requestRender: () => svc.tui.requestRender()
	});
	svc.openOverlay(selector);
	svc.chatLog.addSystem(theme.dim("Session picker"));
	svc.tui.requestRender();
}
/** `/scoped-models` — limit Ctrl+P model cycle set. */
async function openScopedModelsOverlay(svc) {
	const catalog = await svc.client.listModels();
	svc.setModelChoices(catalog);
	if (catalog.length === 0) {
		svc.chatLog.addSystem("No models available.");
		svc.tui.requestRender();
		return;
	}
	const selector = new ScopedModelsSelector(catalog, svc.getScopedModelRefs(), {
		onSave: (refs) => {
			svc.setScopedModelRefs(refs);
			svc.refreshCycleModels();
			svc.closeOverlay();
			svc.tui.setFocus(svc.editor);
			const count = refs === null ? catalog.length : refs.length;
			svc.chatLog.addSystem(theme.dim(refs === null ? `Ctrl+P cycles all ${catalog.length} models` : `Ctrl+P cycles ${count} scoped model${count === 1 ? "" : "s"}`));
			svc.tui.requestRender();
		},
		onCancel: () => {
			svc.closeOverlay();
			svc.tui.setFocus(svc.editor);
			svc.tui.requestRender();
		},
		requestRender: () => svc.tui.requestRender()
	});
	svc.openOverlay(selector);
	svc.chatLog.addSystem(theme.dim("Scoped models for Ctrl+P"));
	svc.tui.requestRender();
}
/** `/settings` — TUI preferences overlay. */
function openSettingsOverlay(svc) {
	const selector = new SettingsSelector(svc.getTuiSettings(), {
		onChange: (settings) => svc.applyTuiSettings(settings),
		onThemePreview: (themeId) => svc.previewTheme(themeId),
		onReloadKeybindings: () => svc.reloadKeybindings(),
		onCancel: () => {
			svc.previewTheme(svc.getTuiSettings().theme);
			svc.closeOverlay();
			svc.tui.setFocus(svc.editor);
			svc.tui.requestRender();
		}
	});
	svc.openOverlay(selector);
	svc.chatLog.addSystem(theme.dim("Settings (↑/↓ · Enter toggle · Esc close)"));
	svc.tui.requestRender();
}
//#endregion
export { openModelPickerOverlay, openScopedModelsOverlay, openSessionPickerOverlay, openSettingsOverlay };

//# sourceMappingURL=tui-picker-overlay.js.map