import { SelectionModel } from "./SelectionModel.js";

//#region src/canvas/selection/SelectionController.ts
/**
* Reactive controller that bridges SelectionModel and Lit element lifecycle.
* Provides selection context to child elements.
*/
var SelectionController = class {
	constructor(host) {
		this.hitTestFn = null;
		this.host = host;
		this.selectionModel = new SelectionModel();
		this.selectionContext = this.createContextProxy();
		host.addController(this);
		this.selectionModel.addEventListener("selectionchange", () => {
			queueMicrotask(() => this.host.requestUpdate());
		});
	}
	hostConnected() {}
	hostDisconnected() {}
	/**
	* Set the hit test function for box selection.
	*/
	setHitTest(fn) {
		this.hitTestFn = fn;
	}
	/**
	* Get the underlying selection model.
	*/
	getModel() {
		return this.selectionModel;
	}
	/**
	* Create a proxy context that delegates to the selection model.
	*/
	createContextProxy() {
		const controller = this;
		return new Proxy({
			select: (id) => {
				controller.selectionModel.select(id);
			},
			selectMultiple: (ids) => {
				controller.selectionModel.selectMultiple(ids);
			},
			addToSelection: (id) => {
				controller.selectionModel.addToSelection(id);
			},
			deselect: (id) => {
				controller.selectionModel.deselect(id);
			},
			toggle: (id) => {
				controller.selectionModel.toggle(id);
			},
			clear: () => {
				controller.selectionModel.clear();
			},
			startBoxSelect: (x, y) => {
				controller.selectionModel.startBoxSelect(x, y);
				queueMicrotask(() => controller.host.requestUpdate());
			},
			updateBoxSelect: (x, y) => {
				controller.selectionModel.updateBoxSelect(x, y);
				queueMicrotask(() => controller.host.requestUpdate());
			},
			endBoxSelect: (hitTest, addToSelection) => {
				const fn = hitTest || controller.hitTestFn;
				if (fn) controller.selectionModel.endBoxSelect(fn, addToSelection ?? false);
			},
			createGroup: (ids) => {
				return controller.selectionModel.createGroup(ids);
			},
			ungroup: (groupId) => {
				controller.selectionModel.ungroup(groupId);
			},
			selectGroup: (groupId) => {
				controller.selectionModel.selectGroup(groupId);
			},
			getGroupId: (elementId) => {
				return controller.selectionModel.getGroupId(elementId);
			},
			getGroupElements: (groupId) => {
				return controller.selectionModel.getGroupElements(groupId);
			},
			addEventListener: (type, listener) => {
				controller.selectionModel.addEventListener(type, listener);
			},
			removeEventListener: (type, listener) => {
				controller.selectionModel.removeEventListener(type, listener);
			}
		}, { get(target, prop) {
			if (prop === "selectedIds") return controller.selectionModel.selectedIds;
			if (prop === "selectionMode") return controller.selectionModel.selectionMode;
			if (prop === "boxSelectBounds") return controller.selectionModel.boxSelectBounds;
			return target[prop];
		} });
	}
};

//#endregion
export { SelectionController };
//# sourceMappingURL=SelectionController.js.map