//#region src/elements/TargetController.ts
const EF_TARGETABLE = Symbol("EF_TARGETABLE");
var TargetRegistry = class {
	constructor() {
		this.idMap = /* @__PURE__ */ new Map();
		this.callbacks = /* @__PURE__ */ new Map();
	}
	subscribe(id, callback) {
		this.callbacks.set(id, this.callbacks.get(id) ?? /* @__PURE__ */ new Set());
		this.callbacks.get(id)?.add(callback);
	}
	unsubscribe(id, callback) {
		if (id === null) return;
		this.callbacks.get(id)?.delete(callback);
		if (this.callbacks.get(id)?.size === 0) this.callbacks.delete(id);
	}
	get(id) {
		return this.idMap.get(id);
	}
	register(id, target) {
		this.idMap.set(id, target);
		for (const callback of this.callbacks.get(id) ?? []) callback(target);
	}
	unregister(id, target) {
		if (this.idMap.get(id) !== target) return;
		for (const callback of this.callbacks.get(id) ?? []) callback(void 0);
		this.idMap.delete(id);
		this.callbacks.delete(id);
	}
};
const documentRegistries = /* @__PURE__ */ new WeakMap();
const getRegistry = (root) => {
	let registry = documentRegistries.get(root);
	if (!registry) {
		registry = new TargetRegistry();
		documentRegistries.set(root, registry);
	}
	return registry;
};
const EFTargetable = (superClass) => {
	class TargetableElement extends superClass {
		#registry = null;
		static get observedAttributes() {
			const parentAttributes = superClass.observedAttributes || [];
			return [...new Set([...parentAttributes, "id"])];
		}
		updateRegistry(oldValue, newValue) {
			if (!this.#registry) return;
			if (oldValue === newValue) return;
			if (oldValue) this.#registry.unregister(oldValue, this);
			if (newValue) this.#registry.register(newValue, this);
		}
		connectedCallback() {
			super.connectedCallback();
			this.#registry = getRegistry(this.getRootNode());
			const initialId = this.getAttribute("id");
			if (initialId) this.updateRegistry("", initialId);
		}
		attributeChangedCallback(name, old, value) {
			super.attributeChangedCallback(name, old, value);
			if (name === "id") this.updateRegistry(old ?? "", value ?? "");
		}
		disconnectedCallback() {
			if (this.#registry) {
				this.updateRegistry(this.id, "");
				this.#registry = null;
			}
			super.disconnectedCallback();
		}
	}
	Object.defineProperty(TargetableElement.prototype, EF_TARGETABLE, { value: true });
	return TargetableElement;
};
var TargetController = class {
	constructor(host) {
		this.currentTargetString = null;
		this.registryCallback = (target) => {
			this.host.targetElement = target ?? null;
		};
		this.host = host;
		this.host.addController(this);
		this.currentTargetString = this.host.target;
		if (this.currentTargetString) this.registry.subscribe(this.currentTargetString, this.registryCallback);
	}
	updateTarget() {
		if (!this.host.target) {
			if (this.host.targetElement !== null) {
				this.host.targetElement = null;
				this.host.requestUpdate("targetElement");
			}
			return;
		}
		let newTarget = this.registry.get(this.host.target);
		if (!newTarget) {
			const container = this.host.closest("ef-timegroup, ef-configuration") || this.host.getRootNode();
			if (container && "querySelector" in container) newTarget = container.querySelector(`#${CSS.escape(this.host.target)}`);
		}
		if (!newTarget) newTarget = document.getElementById(this.host.target);
		if (this.host.targetElement !== newTarget) {
			this.host.targetElement = newTarget ?? null;
			this.host.requestUpdate("targetElement");
		}
	}
	get registry() {
		return getRegistry(this.host.getRootNode());
	}
	hostDisconnected() {}
	hostConnected() {
		this.updateTarget();
	}
	hostUpdate() {
		if (this.currentTargetString !== this.host.target) {
			this.registry.unsubscribe(this.currentTargetString, this.registryCallback);
			this.registry.subscribe(this.host.target, this.registryCallback);
			this.updateTarget();
			this.currentTargetString = this.host.target;
		}
	}
};

//#endregion
export { EFTargetable, TargetController };
//# sourceMappingURL=TargetController.js.map