import { ProtectedRoute, AppearanceContext, AppShellError, useAppearance } from '../../chunk-CPIQ5GXG.js';
export { AccessGuard } from '../../chunk-CPIQ5GXG.js';
import '../../chunk-NZ2F5RQ4.js';
import '../../chunk-OJJK27GC.js';
import '../../chunk-ZTWA5H77.js';
import '../../chunk-AUDNF2Q7.js';
import '../../chunk-6M6OLGQY.js';
import '../../chunk-MKH2KOAO.js';
import '../../chunk-7Q5THR43.js';
import '../../chunk-SJHM4WDG.js';
import '../../chunk-GMXGDO3I.js';
import '../../chunk-S3XR4II4.js';
import { useInitialization } from '../../chunk-6DO4PE3O.js';
import '../../chunk-DD3CCMCZ.js';
import '../../chunk-M7WWRZ5Z.js';
import '../../chunk-E6NSKXYN.js';
import '../../chunk-2IFYDILW.js';
import '../../chunk-Q7DJKLEN.js';
import '../../chunk-HENXLGVD.js';
import '../../chunk-I3CFGE3N.js';
import '../../chunk-RNP5R5I3.js';
export { useUserProfile } from '../../chunk-W2SFTXMT.js';
import '../../chunk-XOPLS4S6.js';
import '../../chunk-FVOMKZ7S.js';
import '../../chunk-KRWALB24.js';
import '../../chunk-2FTX4WO2.js';
import '../../chunk-MQZE7SUI.js';
import '../../chunk-TVTSASST.js';
import '../../chunk-BRJ3QZ4E.js';
import '../../chunk-I2KLQ2HA.js';
import { useContext, useState, useRef, useEffect, useCallback } from 'react';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';

function FullScreenLoader({ hiding, onHidden }) {
  const { background, loader } = useAppearance();
  return /* @__PURE__ */ jsxs(
    "div",
    {
      style: {
        position: "fixed",
        inset: 0,
        overflow: "hidden",
        background: "var(--color-background)",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        zIndex: 1e4,
        opacity: hiding ? 0 : 1,
        transition: "opacity 300ms ease-out",
        pointerEvents: hiding ? "none" : "auto"
      },
      onTransitionEnd: hiding ? onHidden : void 0,
      children: [
        background,
        /* @__PURE__ */ jsx("div", { style: { zIndex: 1 }, children: loader })
      ]
    }
  );
}
function AnimatedLoader({ showLoader }) {
  const [loaderVisible, setLoaderVisible] = useState(showLoader);
  const [loaderHiding, setLoaderHiding] = useState(false);
  const prevShowLoader = useRef(showLoader);
  useEffect(() => {
    if (prevShowLoader.current && !showLoader) {
      setLoaderHiding(true);
    } else if (showLoader && !prevShowLoader.current) {
      setLoaderVisible(true);
      setLoaderHiding(false);
    }
    prevShowLoader.current = showLoader;
  }, [showLoader]);
  const handleLoaderHidden = useCallback(() => {
    setLoaderVisible(false);
    setLoaderHiding(false);
  }, []);
  if (!loaderVisible) return null;
  return /* @__PURE__ */ jsx(FullScreenLoader, { hiding: loaderHiding, onHidden: handleLoaderHidden });
}
function ProtectedRoute2({
  children,
  redirectTo = "/login",
  fallback,
  errorFallback,
  waitForOrganization = false
}) {
  const { isInitializing, userReady, allReady, error } = useInitialization();
  const orgSettled = allReady || error?.layer === "organization";
  const showLoader = !error && (isInitializing || !userReady || waitForOrganization && !orgSettled);
  const resolvedErrorFallback = errorFallback ?? ((err, retry) => /* @__PURE__ */ jsx(AppShellError, { message: err.message, retry }));
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    fallback ? showLoader ? fallback : null : /* @__PURE__ */ jsx(AnimatedLoader, { showLoader }),
    /* @__PURE__ */ jsx(
      ProtectedRoute,
      {
        redirectTo,
        waitForOrganization,
        errorFallback: resolvedErrorFallback,
        children
      }
    )
  ] });
}
function AuthRedirectLoadingScreen({
  backgroundColor = "#f3f4f6",
  loader: loaderOverride,
  testId = "auth-redirect-loading"
}) {
  const appearance = useContext(AppearanceContext);
  return /* @__PURE__ */ jsxs(
    "div",
    {
      "data-testid": testId,
      style: {
        height: "100vh",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        backgroundColor
      },
      children: [
        loaderOverride ?? appearance?.loader ?? /* @__PURE__ */ jsx(
          "div",
          {
            "aria-label": "Loading",
            role: "status",
            style: {
              width: 32,
              height: 32,
              border: "3px solid #d1d5db",
              borderTopColor: "#6b7280",
              borderRadius: "50%",
              animation: "elevasis-auth-redirect-spin 0.8s linear infinite"
            }
          }
        ),
        /* @__PURE__ */ jsx("style", { children: "@keyframes elevasis-auth-redirect-spin { to { transform: rotate(360deg); } }" })
      ]
    }
  );
}

export { AuthRedirectLoadingScreen, ProtectedRoute2 as ProtectedRoute };
