import { brandWorkOsOrgId } from './chunk-MQZE7SUI.js';
import './chunk-TVTSASST.js';
import { AuthProvider } from './chunk-BRJ3QZ4E.js';
import './chunk-I2KLQ2HA.js';
import { AuthKitProvider, useAuth } from '@workos-inc/authkit-react';
import { useMemo } from 'react';
import { jsx } from 'react/jsx-runtime';

function CoreAuthKitInner({ auth, children }) {
  return /* @__PURE__ */ jsx(
    AuthKitProvider,
    {
      clientId: auth.clientId,
      apiHostname: auth.apiHostname ?? "api.workos.com",
      redirectUri: auth.redirectUri,
      devMode: auth.devMode ?? false,
      children: /* @__PURE__ */ jsx(WorkOSAuthAdapter, { children })
    }
  );
}
var WORKOS_REFRESH_TOKEN_KEY = "workos:refresh-token";
function hasWorkOSRefreshToken() {
  try {
    return !!localStorage.getItem(WORKOS_REFRESH_TOKEN_KEY);
  } catch {
    return false;
  }
}
function WorkOSAuthAdapter({ children }) {
  const { user, isLoading, getAccessToken, organizationId, signIn } = useAuth();
  const adaptedUser = useMemo(() => user ? { id: user.id } : null, [user?.id]);
  const value = useMemo(
    () => ({
      user: adaptedUser,
      isLoading,
      getAccessToken,
      organizationId: brandWorkOsOrgId(organizationId ?? null),
      signIn,
      canRecover: hasWorkOSRefreshToken
    }),
    [adaptedUser, isLoading, getAccessToken, organizationId, signIn]
  );
  return /* @__PURE__ */ jsx(AuthProvider, { value, children });
}

export { CoreAuthKitInner };
