import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
//#region src/extensions/slots.ts
init_logger();
const log = createLogger("Extension:Slots");
/**
* Manages exclusive slots for extensions.
* Ensures only one plugin can claim a specific slot at a time.
*/
var SlotRegistry = class {
	slots = /* @__PURE__ */ new Map();
	/**
	* Claim a slot for a plugin
	* @returns true if claim succeeded, false if slot already taken
	*/
	claim(key, pluginId, instance) {
		const existing = this.slots.get(key);
		if (existing && existing.pluginId !== pluginId) {
			log.warn(`Slot "${key}" already claimed by "${existing.pluginId}", rejecting claim from "${pluginId}"`);
			return false;
		}
		this.slots.set(key, {
			pluginId,
			instance,
			claimedAt: /* @__PURE__ */ new Date()
		});
		log.info(`Slot "${key}" claimed by "${pluginId}"`);
		return true;
	}
	/**
	* Release a slot
	* @returns true if released, false if not owned by the plugin
	*/
	release(key, pluginId) {
		const existing = this.slots.get(key);
		if (!existing || existing.pluginId !== pluginId) return false;
		this.slots.delete(key);
		log.info(`Slot "${key}" released by "${pluginId}"`);
		return true;
	}
	/**
	* Get the instance in a slot
	*/
	get(key) {
		return this.slots.get(key)?.instance;
	}
	/**
	* Check if a slot is claimed
	*/
	isClaimed(key) {
		return this.slots.has(key);
	}
	/**
	* Get which plugin claimed a slot
	*/
	getClaimant(key) {
		return this.slots.get(key)?.pluginId;
	}
	/**
	* Get all claimed slots
	*/
	getAllClaims() {
		return new Map(this.slots);
	}
	/**
	* Clear all slots (useful for testing or reset)
	*/
	clear() {
		this.slots.clear();
		log.info("All slots cleared");
	}
	/**
	* Force release a slot (admin operation)
	*/
	forceRelease(key) {
		if (this.slots.has(key)) {
			const claimant = this.slots.get(key).pluginId;
			this.slots.delete(key);
			log.warn(`Slot "${key}" force-released (was claimed by "${claimant}")`);
			return true;
		}
		return false;
	}
};
const registeredSlotTypes = /* @__PURE__ */ new Set();
/**
* Register a new slot type (for extensibility)
*/
function registerSlotType(key) {
	if (!registeredSlotTypes.has(key)) {
		registeredSlotTypes.add(key);
		log.debug(`Registered slot type: ${key}`);
	}
}
/**
* Get all registered slot types
*/
function getRegisteredSlotTypes() {
	return Array.from(registeredSlotTypes);
}
registerSlotType("memory");
registerSlotType("tts");
registerSlotType("imageGeneration");
registerSlotType("webSearch");
let globalSlotRegistry = null;
function getSlotRegistry() {
	if (!globalSlotRegistry) globalSlotRegistry = new SlotRegistry();
	return globalSlotRegistry;
}
function setSlotRegistry(registry) {
	globalSlotRegistry = registry;
}
//#endregion
export { SlotRegistry, getRegisteredSlotTypes, getSlotRegistry, registerSlotType, setSlotRegistry };

//# sourceMappingURL=slots.js.map