const require_chunk = require('../chunk-CUT6urMc.cjs');
const fast_glob = require_chunk.__toESM(require("fast-glob"));
const __geekmidas_constructs_crons = require_chunk.__toESM(require("@geekmidas/constructs/crons"));
const __geekmidas_constructs_endpoints = require_chunk.__toESM(require("@geekmidas/constructs/endpoints"));
const __geekmidas_constructs_functions = require_chunk.__toESM(require("@geekmidas/constructs/functions"));
const __geekmidas_constructs_subscribers = require_chunk.__toESM(require("@geekmidas/constructs/subscribers"));

//#region src/deploy/sniffer-routes-worker.ts
const appPathArg = process.argv[2];
const routesPatternArg = process.argv[3];
if (!appPathArg || !routesPatternArg) {
	console.log(JSON.stringify({
		envVars: [],
		error: "App path and routes pattern are required"
	}));
	process.exit(1);
}
const appPath = appPathArg;
const routesPattern = routesPatternArg;
/**
* Check if a value is a gkm construct.
*/
function isConstruct(value) {
	return __geekmidas_constructs_endpoints.Endpoint.isEndpoint(value) || __geekmidas_constructs_functions.Function.isFunction(value) || __geekmidas_constructs_crons.Cron.isCron(value) || __geekmidas_constructs_subscribers.Subscriber.isSubscriber(value);
}
/**
* Main sniffing function
*/
async function sniff() {
	const envVars = /* @__PURE__ */ new Set();
	let error = null;
	try {
		const files = await (0, fast_glob.default)(routesPattern, {
			cwd: appPath,
			absolute: true
		});
		for (const file of files) try {
			const module$1 = await import(file);
			for (const [, exportValue] of Object.entries(module$1)) if (isConstruct(exportValue)) try {
				const constructEnvVars = await exportValue.getEnvironment();
				constructEnvVars.forEach((v) => envVars.add(v));
			} catch {}
		} catch (e) {
			const msg = e instanceof Error ? e.message : String(e);
			console.error(`[sniffer] Failed to import ${file}: ${msg}`);
		}
	} catch (e) {
		error = e instanceof Error ? e.message : String(e);
	}
	console.log(JSON.stringify({
		envVars: Array.from(envVars).sort(),
		error
	}));
}
process.on("unhandledRejection", () => {});
sniff().catch((e) => {
	console.log(JSON.stringify({
		envVars: [],
		error: e.message || String(e)
	}));
	process.exit(1);
});

//#endregion
//# sourceMappingURL=sniffer-routes-worker.cjs.map