import { patchGlobalConsole } from './chunk-C5R5IRTK.js';
import { patchGlobalResponse } from './chunk-CWWKO23R.js';
import { patchGlobalServerResponse } from './chunk-KU6Z4QI2.js';
import { execSyncVarlock, VarlockExecError } from './chunk-BUYR7R2U.js';
import { generateEncryptionKeyHex, encryptEnvBlobSync } from './chunk-IQ226IPZ.js';
import { initVarlockEnv } from './chunk-PRASPAE4.js';

// src/auto-load.ts
try {
  const { stdout } = execSyncVarlock("load --format json-full --compact", {
    fullResult: true,
    // Pass the directory of this module so that in monorepos the binary search
    // starts from inside the varlock package (e.g. apps/web/node_modules/varlock)
    // rather than from process.cwd(), which may be an unrelated workspace root.
    callerDir: import.meta.dirname ?? new URL(".", import.meta.url).pathname
  });
  const parsed = JSON.parse(stdout);
  globalThis.__varlockLoadedEnv = parsed;
  let encryptionKey = process.env._VARLOCK_ENV_KEY;
  if (parsed.settings?.encryptInjectedEnv && !encryptionKey) {
    encryptionKey = generateEncryptionKeyHex();
    process.env._VARLOCK_ENV_KEY = encryptionKey;
  }
  if (encryptionKey) {
    process.env.__VARLOCK_ENV = encryptEnvBlobSync(stdout, encryptionKey);
  } else {
    process.env.__VARLOCK_ENV = stdout;
  }
} catch (err) {
  if (err instanceof VarlockExecError && err.stderr) {
    process.stderr.write(err.stderr);
  } else {
    console.error(err);
  }
  process.exit(err.exitCode ?? 1);
}
initVarlockEnv();
patchGlobalConsole();
patchGlobalServerResponse();
patchGlobalResponse();
//# sourceMappingURL=chunk-VXEKFJP7.js.map
//# sourceMappingURL=chunk-VXEKFJP7.js.map