import { ReactNode } from "react";
import { createUserManagerContext } from "react-oidc-js";
import { AUTHORIZATION } from "../../../constants";
import { queryClient } from "../../../hooks/queryClient";
import { getDeviceId } from "../../../modules";

const {
  Provider,
  handleAccessTokenExpired,
  getUser,
  removeUser,
  getUserWaitRefresh,
  axiosMiddle,
} = createUserManagerContext({
  onRefresh: async (user) => {
    const { postAuthV1PublicAuthConnectToken } = await import("../../services");
    const res = await postAuthV1PublicAuthConnectToken(
      {
        refreshToken: user?.refresh_token,
        grantType: "RefreshToken",
        clientSecret: __CLIENT_SECRET__,
        clientId: __CLIENT_ID__,
      },
      { deviceId: getDeviceId(), platform: "web" },
      {
        headers: {
          authorization: AUTHORIZATION,
        },
      },
    );

    setTimeout(async function () {
      await queryClient.resetQueries();
    }, 0);

    return {
      access_token: res.accessToken || "",
      refresh_token: res.refreshToken || "",
      expires_in: res.expiresIn,
      token_type: res.tokenType || "",
    };
  },
  authority: "web.hitobit.com",
  client_id: __CLIENT_ID__,
});

const UserManagerProvider = ({ children }: { children: ReactNode }) => {
  return <Provider>{children}</Provider>;
};

export {
  handleAccessTokenExpired,
  getUser,
  removeUser,
  getUserWaitRefresh,
  axiosMiddle,
};

export default UserManagerProvider;
