import { authStorage, clientStorage } from "../helpers/storage.js";
import { debounce } from "@scalar/helpers/general/debounce";
//#region src/plugins/persistance-plugin.ts
/**
* Plugin to persist workspace state changes with debounced writes.
*/
var persistencePlugin = ({ debounceDelay = 500, maxWait = 1e4, prefix = "", persistAuth = false }) => {
	const { execute } = debounce({
		delay: debounceDelay,
		maxWait
	});
	const authPersistence = authStorage();
	const clientPersistence = clientStorage();
	/**
	* Resolves the prefix to use for local storage keys.
	* If 'prefix' is a string, returns it directly.
	* If 'prefix' is a function, calls and returns its value.
	*/
	const getPrefix = () => {
		if (typeof prefix === "string") return prefix;
		return prefix();
	};
	const getPersistAuth = () => {
		if (typeof persistAuth === "function") return persistAuth();
		return persistAuth;
	};
	return { hooks: { onWorkspaceStateChanges(event) {
		if (event.type === "meta") {
			const defaultClient = event.value["x-scalar-default-client"];
			if (defaultClient !== void 0) execute("x-scalar-default-client", () => clientPersistence.set(defaultClient));
			return;
		}
		if (getPersistAuth() && event.type === "auth") execute("auth", () => authPersistence.setAuth(getPrefix(), event.value));
	} } };
};
//#endregion
export { persistencePlugin };

//# sourceMappingURL=persistance-plugin.js.map