import { searchableSelectListTheme, theme } from "../theme.js";
import { modelRef } from "../tui-scoped-models.js";
import { SearchableSelectList } from "./searchable-select-list.js";
import { Text, isKeyRelease, matchesKey } from "@earendil-works/pi-tui";
//#region src/tui/components/scoped-models-selector.ts
function buildItems(catalog, enabledRefs) {
	return catalog.map((m) => {
		const ref = modelRef(m);
		return {
			value: ref,
			label: `${(enabledRefs === null || enabledRefs.size === 0 ? true : enabledRefs.has(ref)) ? "☑" : "☐"} ${m.name || m.id}`,
			description: m.provider,
			searchText: `${m.provider} ${m.id} ${m.name ?? ""}`
		};
	});
}
/** Toggle models included in Ctrl+P cycling (pi `/scoped-models` subset). */
var ScopedModelsSelector = class {
	catalog;
	enabledRefs;
	list;
	footer = new Text("", 1, 0);
	constructor(catalog, initialRefs, callbacks) {
		this.callbacks = callbacks;
		this.catalog = catalog;
		this.enabledRefs = initialRefs === null ? null : initialRefs.length === 0 ? /* @__PURE__ */ new Set() : new Set(initialRefs);
		this.list = new SearchableSelectList(buildItems(catalog, this.enabledRefs), 12, searchableSelectListTheme);
		this.list.onCancel = () => this.callbacks.onCancel();
		this.list.onSelect = () => this.saveAndClose();
		this.footer.setText(theme.dim("Space toggle · A all · C clear · Enter save · Esc cancel"));
	}
	refreshItems() {
		const selected = this.list.getSelectedItem()?.value;
		const items = buildItems(this.catalog, this.enabledRefs);
		this.list.items = items;
		this.list.filteredItems = items;
		if (selected) {
			const idx = items.findIndex((i) => i.value === selected);
			if (idx >= 0) this.list.setSelectedIndex(idx);
		}
	}
	saveAndClose() {
		if (this.enabledRefs === null) {
			this.callbacks.onSave(null);
			return;
		}
		const refs = [...this.enabledRefs];
		this.callbacks.onSave(refs.length === 0 || refs.length === this.catalog.length ? null : refs);
	}
	invalidate() {
		this.list.invalidate();
	}
	render(width) {
		return [
			theme.bold("Scoped models (Ctrl+P cycle)"),
			"",
			...this.list.render(width),
			"",
			...this.footer.render(width)
		];
	}
	handleInput(keyData) {
		if (isKeyRelease(keyData)) return;
		if (matchesKey(keyData, "enter")) {
			this.saveAndClose();
			return;
		}
		if (keyData === " ") {
			const item = this.list.getSelectedItem();
			if (!item) return;
			if (this.enabledRefs === null) this.enabledRefs = new Set(this.catalog.map(modelRef));
			if (this.enabledRefs.has(item.value)) this.enabledRefs.delete(item.value);
			else this.enabledRefs.add(item.value);
			this.refreshItems();
			this.callbacks.requestRender();
			return;
		}
		if (keyData === "a" || keyData === "A") {
			this.enabledRefs = null;
			this.refreshItems();
			this.callbacks.requestRender();
			return;
		}
		if (keyData === "c" || keyData === "C") {
			this.enabledRefs = /* @__PURE__ */ new Set();
			this.refreshItems();
			this.callbacks.requestRender();
			return;
		}
		this.list.handleInput(keyData);
		this.callbacks.requestRender();
	}
};
//#endregion
export { ScopedModelsSelector };

//# sourceMappingURL=scoped-models-selector.js.map