import { computed, onBeforeUnmount, toValue, watch } from "vue";
//#region src/v2/features/app/hooks/use-document-watcher.ts
/**
* Default timeout before the first poll attempt (in milliseconds).
*/
var DEFAULT_INITIAL_TIMEOUT = 5 * 1e3;
/**
* Maximum timeout cap for exponential backoff (in milliseconds).
*/
var MAX_TIMEOUT = 60 * 1e3;
/**
* Creates a timer manager for handling polling timeouts.
*
* @returns Object with methods to manage the polling timer.
*/
var createTimerManager = () => {
	let timer = null;
	/**
	* Clears the current timer if it exists.
	*/
	const clear = () => {
		if (timer) {
			clearTimeout(timer);
			timer = null;
		}
	};
	/**
	* Schedules the next poll after the specified timeout.
	*
	* @param callback - The function to call when the timeout expires.
	* @param timeout - The delay in milliseconds before calling the callback.
	*/
	const schedule = (callback, timeout) => {
		clear();
		timer = setTimeout(callback, timeout);
	};
	return {
		clear,
		schedule
	};
};
/**
* Creates a timeout manager that handles exponential backoff on failures.
*
* @param initialTimeout - The initial timeout value in milliseconds.
* @returns Object with methods to manage timeout values.
*/
var createTimeoutManager = (initialTimeout) => {
	let currentTimeout = initialTimeout;
	/**
	* Resets the timeout to its initial value.
	*/
	const reset = () => {
		currentTimeout = initialTimeout;
	};
	/**
	* Doubles the timeout with exponential backoff, capped at MAX_TIMEOUT.
	*/
	const backoff = () => {
		currentTimeout = Math.min(currentTimeout * 2, MAX_TIMEOUT);
	};
	/**
	* Gets the current timeout value.
	*/
	const get = () => currentTimeout;
	return {
		reset,
		backoff,
		get
	};
};
/**
* Resolves conflicts by preferring remote changes.
*
* @param conflicts - Array of conflict tuples from the rebase result.
* Each tuple contains two arrays: [remote changes, local changes].
* @returns Array of remote changes to apply.
*/
var resolveConflicts = (conflicts) => {
	return conflicts.flatMap(([remote]) => remote);
};
/**
* Watches the specified document in the workspace store and periodically rebases it with its remote source.
*
* This utility sets up a watcher on the given document. If the document specifies an 'x-scalar-original-source-url'
* and watch mode is enabled, this hook polls the remote source and calls `store.rebaseDocument`.
* If rebase conflicts are detected, it applies automatic conflict resolution by preferring remote changes.
* Uses exponential backoff on failures to avoid overwhelming the server.
*
* @param params - Configuration object for the document watcher.
* @param params.documentName - The name/key of the document to watch and rebase.
* @param params.store - The WorkspaceStore instance.
* @param params.initialTimeout - Initial delay before the first poll attempt (default: 5000ms).
*
* @example
* ```ts
* import { useDocumentWatcher } from '@/hooks/use-document-watcher'
* import { useScalarWorkspaceStore } from '@scalar/workspace-store'
*
* const store = useScalarWorkspaceStore()
* useDocumentWatcher({ documentName: 'myApi', store })
* ```
*/
var useDocumentWatcher = ({ documentName, store, initialTimeout = DEFAULT_INITIAL_TIMEOUT }) => {
	const document = computed(() => {
		const storeValue = toValue(store);
		const documentNameValue = toValue(documentName);
		if (!storeValue || !documentNameValue) return null;
		return storeValue.workspace.documents[documentNameValue];
	});
	const timerManager = createTimerManager();
	const timeoutManager = createTimeoutManager(initialTimeout);
	/**
	* Handles a successful poll result (with or without changes).
	*/
	const handleSuccess = () => {
		timeoutManager.reset();
		timerManager.schedule(poll, timeoutManager.get());
	};
	/**
	* Handles a failed poll result with exponential backoff.
	*/
	const handleFailure = () => {
		timeoutManager.backoff();
		timerManager.schedule(poll, timeoutManager.get());
	};
	/**
	* Polls the remote document source and attempts to rebase.
	*/
	const poll = async () => {
		const storeValue = toValue(store);
		const documentNameValue = toValue(documentName);
		const sourceUrl = document.value?.["x-scalar-original-source-url"];
		if (!storeValue || !documentNameValue || !sourceUrl) return;
		const result = await storeValue.rebaseDocument({
			name: documentNameValue,
			url: sourceUrl
		});
		if (result?.ok) {
			await result.applyChanges({ resolvedConflicts: resolveConflicts(result.conflicts) });
			handleSuccess();
		} else if (result?.ok === false && result.type === "NO_CHANGES_DETECTED") handleSuccess();
		else handleFailure();
	};
	/**
	* Watches for changes to the document's source URL and watch mode.
	* Starts or stops polling based on these values.
	*/
	watch([() => document.value?.["x-scalar-original-source-url"], () => document.value?.["x-scalar-watch-mode"]], ([sourceUrl, watchMode = false]) => {
		if (!toValue(store)) {
			timerManager.clear();
			return;
		}
		timerManager.clear();
		if (!sourceUrl || !watchMode) {
			timeoutManager.reset();
			return;
		}
		timerManager.schedule(poll, timeoutManager.get());
	}, { immediate: true });
	onBeforeUnmount(() => {
		timerManager.clear();
	});
};
//#endregion
export { useDocumentWatcher };

//# sourceMappingURL=use-document-watcher.js.map