import { getSafeTimers } from "../../../../../@vitest_utils@3.2.4/node_modules/@vitest/utils/dist/index.js";

//#region ../../node_modules/.pnpm/vitest@3.2.4_@edge-runtime+vm@3.2.0_@types+debug@4.1.12_@types+node@20.19.27_jiti@2.6.1_fc26da61d3f761277fbc021fa297f7ea/node_modules/vitest/dist/chunks/utils.XdZDrNZV.js
const NAME_WORKER_STATE = "__vitest_worker__";
function getWorkerState() {
	const workerState = globalThis[NAME_WORKER_STATE];
	if (!workerState) throw new Error("Vitest failed to access its internal state.\n\nOne of the following is possible:\n- \"vitest\" is imported directly without running \"vitest\" command\n- \"vitest\" is imported inside \"globalSetup\" (to fix this, use \"setupFiles\" instead, because \"globalSetup\" runs in a different context)\n- \"vitest\" is imported inside Vite / Vitest config file\n- Otherwise, it might be a Vitest bug. Please report it to https://github.com/vitest-dev/vitest/issues\n");
	return workerState;
}
function getCurrentEnvironment() {
	return getWorkerState()?.environment.name;
}
function isChildProcess() {
	return typeof process !== "undefined" && !!process.send;
}
function resetModules(modules, resetMocks = false) {
	const skipPaths = [
		/\/vitest\/dist\//,
		/\/vite-node\/dist\//,
		/vitest-virtual-\w+\/dist/,
		/@vitest\/dist/,
		...!resetMocks ? [/^mock:/] : []
	];
	modules.forEach((mod, path) => {
		if (skipPaths.some((re) => re.test(path))) return;
		modules.invalidateModule(mod);
	});
}
function waitNextTick() {
	const { setTimeout } = getSafeTimers();
	return new Promise((resolve) => setTimeout(resolve, 0));
}
async function waitForImportsToResolve() {
	await waitNextTick();
	const state = getWorkerState();
	const promises = [];
	let resolvingCount = 0;
	for (const mod of state.moduleCache.values()) {
		if (mod.promise && !mod.evaluated) promises.push(mod.promise);
		if (mod.resolving) resolvingCount++;
	}
	if (!promises.length && !resolvingCount) return;
	await Promise.allSettled(promises);
	await waitForImportsToResolve();
}

//#endregion
export { getCurrentEnvironment, getWorkerState, isChildProcess, resetModules, waitForImportsToResolve };