import { useState, useEffect } from "react";
// import toast from "react-hot-toast";
import {
  useGetGoogleAuthConfigQuery,
  useUpdateGoogleAuthConfigMutation,
} from "../../app/services/server";
export default function useGoogleAuthConfig() {
  const [changed, setChanged] = useState(false);
  const [clientId, setClientId] = useState("");
  const { data } = useGetGoogleAuthConfigQuery(undefined, {
    refetchOnMountOrArgChange: true,
  });
  const [
    updateGoogleAuthConfig,
    { isSuccess },
  ] = useUpdateGoogleAuthConfigMutation();
  useEffect(() => {
    if (data) {
      setClientId(data.client_id);
    }
  }, [data]);

  useEffect(() => {
    setChanged(isSuccess ? false : data?.client_id !== clientId);
  }, [data, clientId, isSuccess]);

  const updateServerClientId = async () => {
    if (!clientId) return;
    await updateGoogleAuthConfig({ client_id: clientId });
  };

  return {
    config: data,
    changed,
    clientId,
    updateClientId: setClientId,
    updateServerClientId,
    updateGoogleAuthConfig,
    isSuccess,
  };
}
