import { UserManager, WebStorageStateStore } from "oidc-client-ts";
import type { AuthProviderProps } from "react-oidc-context";
import {
  buildCommonOidcRuntimeConfig,
  buildCommonUserManagerSettings,
} from "../../../common/core/auth";
import { resolveOrgFrontPublicOrigin } from "./authConfig";

const orgFrontPublicOrigin = resolveOrgFrontPublicOrigin(
  import.meta.env.VITE_ORGFRONT_PUBLIC_URL,
  window.location.origin,
);

export const oidcConfig: AuthProviderProps = buildCommonOidcRuntimeConfig({
  authority:
    import.meta.env.VITE_OIDC_AUTHORITY || "http://localhost:5000/oidc",
  clientId: import.meta.env.VITE_OIDC_CLIENT_ID || "orgfront",
  origin: orgFrontPublicOrigin,
  userStore: new WebStorageStateStore({ store: window.localStorage }),
});

export const userManager = new UserManager(
  buildCommonUserManagerSettings(oidcConfig),
);
