import fg from "fast-glob";
import { Cron } from "@geekmidas/constructs/crons";
import { Endpoint } from "@geekmidas/constructs/endpoints";
import { Function } from "@geekmidas/constructs/functions";
import { Subscriber } from "@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 Endpoint.isEndpoint(value) || Function.isFunction(value) || Cron.isCron(value) || Subscriber.isSubscriber(value);
}
/**
* Main sniffing function
*/
async function sniff() {
	const envVars = /* @__PURE__ */ new Set();
	let error = null;
	try {
		const files = await fg(routesPattern, {
			cwd: appPath,
			absolute: true
		});
		for (const file of files) try {
			const module = await import(file);
			for (const [, exportValue] of Object.entries(module)) 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.mjs.map