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

const adminPublicOrigin = resolveAdminPublicOrigin(
  import.meta.env.VITE_ADMIN_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 || "adminfront",
  origin: adminPublicOrigin,
  userStore: new WebStorageStateStore({ store: window.localStorage }),
});

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