import {
  Api,
  Configuration,
  require_environment,
  require_node
} from "/build/_shared/chunk-MDLUSIM2.js";
import {
  require_ssr
} from "/build/_shared/chunk-EB6A6RVQ.js";
import {
  Routes
} from "/build/_shared/chunk-5222SY3A.js";
import {
  createHotContext,
  init_remix_hmr
} from "/build/_shared/chunk-4FTV3JJX.js";
import {
  __toESM
} from "/build/_shared/chunk-RODUX5XG.js";

// app/auth/withAuth.ts
init_remix_hmr();
var import_ssr = __toESM(require_ssr(), 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 = "1715882071644.6074";
}
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 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-RHQW4WCH.js.map
