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

const devFrontPublicOrigin = resolveDevFrontPublicOrigin(
  import.meta.env.VITE_DEVFRONT_PUBLIC_URL,
  window.location.origin,
);

export const oidcConfig: AuthProviderProps = buildCommonOidcRuntimeConfig({
  authority: import.meta.env.VITE_OIDC_AUTHORITY || "https://sso.hmac.kr/oidc",
  clientId: import.meta.env.VITE_OIDC_CLIENT_ID || "devfront",
  origin: devFrontPublicOrigin,
  userStore: new WebStorageStateStore({ store: window.localStorage }),
});

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