import {
  Api,
  require_dist,
  require_environment
} from "/build/_shared/chunk-KQKP3VNY.js";
import {
  require_node
} from "/build/_shared/chunk-G7CHZRZX.js";
import {
  require_ssr
} from "/build/_shared/chunk-TLLKLDIT.js";
import {
  Routes
} from "/build/_shared/chunk-N27UEDHN.js";
import {
  createHotContext,
  init_remix_hmr
} from "/build/_shared/chunk-G7KTOMTD.js";
import {
  __toESM
} from "/build/_shared/chunk-PNG5AS42.js";

// app/auth/withAuth.ts
init_remix_hmr();
var import_ssr = __toESM(require_ssr(), 1);
var import_browser_client = __toESM(require_dist(), 1);
var import_node = __toESM(require_node(), 1);
var import_environment = __toESM(require_environment(), 1);
if (import.meta) {
  import.meta.hot = createHotContext(
    //@ts-expect-error
    "app/auth/withAuth.ts"
  );
  import.meta.hot.lastModified = "1711558601917.4905";
}
var withAuth = (func) => {
  return async (args) => {
    const auth = await (0, import_ssr.getAuth)(args);
    const token = await auth.getToken();
    if (!token || !auth.userId) {
      return (0, import_node.redirect)(Routes.auth.login);
    }
    const { userId, orgId } = auth;
    if (!orgId && !userId) {
      console.warn("No orgId or userId found in session.");
      return (0, import_node.redirect)(Routes.app.root);
    }
    const configuration = new import_browser_client.Configuration({
      basePath: import_environment.env.SERVER_BASE_URL,
      accessToken: token,
      headers: {
        connection: "keep-alive",
        "x-montelo-project-id": args.params.projectId || ""
      }
    });
    const api = new Api(configuration);
    return func({ ...args, api, userId: auth.userId, orgId });
  };
};

export {
  withAuth
};
//# sourceMappingURL=/build/_shared/chunk-NHF4CNMF.js.map
