//#region src/DelayedLoadingState.ts
var DelayedLoadingState = class {
	constructor(delayMs = 250, onStateChange) {
		this.operations = /* @__PURE__ */ new Map();
		this.isCurrentlyLoading = false;
		this.currentMessage = "";
		this.loadingDelayMs = delayMs;
		this.onStateChange = onStateChange;
	}
	/**
	* Start a delayed loading operation
	*/
	startLoading(operationId, message, options = {}) {
		const isBackground = options.background || false;
		const existingOp = this.operations.get(operationId);
		if (existingOp?.timeout) clearTimeout(existingOp.timeout);
		if (isBackground) {
			this.operations.set(operationId, {
				id: operationId,
				message,
				startTime: Date.now(),
				isBackground: true
			});
			return;
		}
		const timeout = setTimeout(() => {
			const operation = this.operations.get(operationId);
			if (operation && !operation.isBackground) this.triggerLoadingState();
		}, this.loadingDelayMs);
		this.operations.set(operationId, {
			id: operationId,
			message,
			startTime: Date.now(),
			timeout,
			isBackground: false
		});
	}
	/**
	* Clear a loading operation
	*/
	clearLoading(operationId) {
		const operation = this.operations.get(operationId);
		if (!operation) return;
		if (operation.timeout) clearTimeout(operation.timeout);
		this.operations.delete(operationId);
		this.updateLoadingState();
	}
	/**
	* Clear all loading operations
	*/
	clearAllLoading() {
		for (const operation of this.operations.values()) if (operation.timeout) clearTimeout(operation.timeout);
		this.operations.clear();
		this.updateLoadingState();
	}
	/**
	* Get current loading state
	*/
	get isLoading() {
		return this.isCurrentlyLoading;
	}
	/**
	* Get current loading message
	*/
	get message() {
		return this.currentMessage;
	}
	/**
	* Check if any non-background operations are active
	*/
	hasActiveOperations() {
		for (const operation of this.operations.values()) if (!operation.isBackground) return true;
		return false;
	}
	/**
	* Get the message for the most recent non-background operation
	*/
	getCurrentMessage() {
		let latestTime = 0;
		let latestMessage = "";
		for (const operation of this.operations.values()) if (!operation.isBackground && operation.startTime > latestTime) {
			latestTime = operation.startTime;
			latestMessage = operation.message;
		}
		return latestMessage;
	}
	/**
	* Trigger loading state (called by timeout)
	*/
	triggerLoadingState() {
		if (this.hasActiveOperations() && !this.isCurrentlyLoading) {
			this.isCurrentlyLoading = true;
			this.currentMessage = this.getCurrentMessage();
			this.onStateChange?.(true, this.currentMessage);
		}
	}
	/**
	* Update loading state based on current operations
	*/
	updateLoadingState() {
		const shouldBeLoading = this.hasActiveOperations();
		if (shouldBeLoading !== this.isCurrentlyLoading) {
			this.isCurrentlyLoading = shouldBeLoading;
			if (shouldBeLoading) {
				this.currentMessage = this.getCurrentMessage();
				this.onStateChange?.(true, this.currentMessage);
			} else {
				this.currentMessage = "";
				this.onStateChange?.(false, "");
			}
		}
	}
};

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