import {
  require_activeProject
} from "/build/_shared/chunk-FVHW7QHP.js";
import {
  require_clerk
} from "/build/_shared/chunk-VKSNXN2Q.js";
import {
  require_build
} from "/build/_shared/chunk-FWA2TGLX.js";
import {
  require_dist
} from "/build/_shared/chunk-ESA5OCAR.js";
import {
  require_jsx_dev_runtime
} from "/build/_shared/chunk-D5XSJF2M.js";
import {
  Await,
  init_esm2 as init_esm,
  useLoaderData,
  useNavigate
} from "/build/_shared/chunk-VJFCZKPM.js";
import {
  require_react
} from "/build/_shared/chunk-HWKMUXRS.js";
import {
  withAuth
} from "/build/_shared/chunk-RHQW4WCH.js";
import {
  require_node
} from "/build/_shared/chunk-MDLUSIM2.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/routes/app+/index.tsx
init_remix_hmr();
var import_remix = __toESM(require_dist(), 1);
var import_node = __toESM(require_node(), 1);
init_esm();
var import_react2 = __toESM(require_react(), 1);
var import_remix_themes = __toESM(require_build(), 1);
var import_activeProject = __toESM(require_activeProject(), 1);
var import_clerk = __toESM(require_clerk(), 1);
var import_jsx_dev_runtime = __toESM(require_jsx_dev_runtime(), 1);
if (!window.$RefreshReg$ || !window.$RefreshSig$ || !window.$RefreshRuntime$) {
  console.warn("remix:hmr: React Fast Refresh only works when the Remix compiler is running in development mode.");
} else {
  prevRefreshReg = window.$RefreshReg$;
  prevRefreshSig = window.$RefreshSig$;
  window.$RefreshReg$ = (type, id) => {
    window.$RefreshRuntime$.register(type, '"app/routes/app+/index.tsx"' + id);
  };
  window.$RefreshSig$ = window.$RefreshRuntime$.createSignatureFunctionForTransform;
}
var prevRefreshReg;
var prevRefreshSig;
var _s = $RefreshSig$();
var _s2 = $RefreshSig$();
var _s3 = $RefreshSig$();
if (import.meta) {
  import.meta.hot = createHotContext(
    //@ts-expect-error
    "app/routes/app+/index.tsx"
  );
  import.meta.hot.lastModified = "1715882071671.4744";
}
var getUserWithPublicMetadata = async (userId) => {
  const startTime = Date.now();
  const TIMEOUT = 1e4;
  const DELAY = 250;
  while (Date.now() - startTime < TIMEOUT) {
    const user = await import_clerk.clerkClient.users.getUser(userId);
    if (!user) {
      throw new Error("User not found! " + userId);
    }
    const {
      personalProjectId,
      personalEnvId
    } = user?.publicMetadata || {};
    if (personalProjectId && personalEnvId) {
      return user;
    }
    await new Promise((resolve) => setTimeout(resolve, DELAY));
  }
  throw new Error("Timeout reached without finding project!");
};
var loader = withAuth(async ({
  api,
  userId,
  request
}) => {
  const activeProject = await (0, import_activeProject.getActiveProject)(request);
  const allProjects = await api.project.projectControllerGetAllProjects();
  const userPromise = getUserWithPublicMetadata(userId);
  return (0, import_node.defer)({
    user: userPromise,
    allProjects,
    activeProject
  });
});
var LoadingLogo = () => {
  _s();
  const [theme] = (0, import_remix_themes.useTheme)();
  const isDarkMode = theme === import_remix_themes.Theme.DARK;
  return /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)("div", { className: "flex h-screen flex-col items-center justify-center gap-8", children: /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)("img", { src: isDarkMode ? "/DarkModeLogo.svg" : "/LightModeLogo.svg", alt: "Montelo Logo", className: "w-48" }, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 72,
    columnNumber: 7
  }, this) }, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 71,
    columnNumber: 10
  }, this);
};
_s(LoadingLogo, "tTTAODy8wohSLSpq8pa8WNTieJI=", false, function() {
  return [import_remix_themes.useTheme];
});
_c = LoadingLogo;
var redirectComponent = ({
  allProjects,
  activeProject,
  user,
  orgId
}) => {
  _s2();
  const navigate = useNavigate();
  (0, import_react2.useEffect)(() => {
    const {
      personalProjectId,
      personalEnvId,
      hasOnboarded
    } = user?.publicMetadata || {};
    let projectId = null, envId = null, redirectPath = Routes.app.project.env.dashboard;
    if (!orgId && personalProjectId && personalEnvId) {
      redirectPath = hasOnboarded ? Routes.app.project.env.dashboard : Routes.app.project.env.onboarding;
      projectId = personalProjectId;
      envId = personalEnvId;
    } else if (allProjects.length) {
      const activeProjectObj = activeProject ? allProjects.find((p) => p.id === activeProject) : null;
      if (activeProjectObj) {
        projectId = activeProjectObj.id;
        envId = activeProjectObj.environments[0].id;
      } else {
        const firstProject = allProjects[0];
        projectId = firstProject.id;
        envId = firstProject.environments[0].id;
      }
    }
    if (projectId && envId) {
      const path = redirectPath({
        projectId,
        envId
      });
      navigate(path, {
        unstable_viewTransition: true
      });
    }
  }, [user]);
  return /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)(LoadingLogo, {}, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 121,
    columnNumber: 10
  }, this);
};
_s2(redirectComponent, "0pNeyzXk/ByIxyERsdaIrG6js9s=", false, function() {
  return [useNavigate];
});
function RootRoute() {
  _s3();
  const auth = (0, import_remix.useAuth)();
  const {
    user,
    allProjects,
    activeProject
  } = useLoaderData();
  return /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)(import_react2.Suspense, { fallback: /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)(LoadingLogo, {}, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 134,
    columnNumber: 30
  }, this), children: /* @__PURE__ */ (0, import_jsx_dev_runtime.jsxDEV)(Await, { resolve: user, children: (user2) => redirectComponent({
    activeProject,
    allProjects,
    user: user2,
    orgId: auth.orgId
  }) }, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 135,
    columnNumber: 7
  }, this) }, void 0, false, {
    fileName: "app/routes/app+/index.tsx",
    lineNumber: 134,
    columnNumber: 10
  }, this);
}
_s3(RootRoute, "X24uiWYL6nmVmwotE6n/u+Oxq98=", false, function() {
  return [import_remix.useAuth, useLoaderData];
});
_c2 = RootRoute;
var _c;
var _c2;
$RefreshReg$(_c, "LoadingLogo");
$RefreshReg$(_c2, "RootRoute");
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;

export {
  RootRoute
};
//# sourceMappingURL=/build/_shared/chunk-B4HEWCA5.js.map
