import { useSiteByWebflowIdQuery } from "../generated/graphql";
import { useWebflowInfo } from "./useWebflowInfo";

interface UseCurrentSiteIdResults {
  loading: boolean;
  error?: string;
  data: {
    webflowId?: string;
    nocodelyticsId?: string;
  };
}

export const useCurrentSiteInfo = (): UseCurrentSiteIdResults => {
  const { webflowSiteLoading, webflowId, webflowError } = useWebflowInfo();

  const siteByWebflowIdQueryResults = useSiteByWebflowIdQuery({
    variables: {
      webflowId,
    },
    skip: !webflowId,
  });
  const loading = webflowSiteLoading || siteByWebflowIdQueryResults.loading;
  const error = webflowError || siteByWebflowIdQueryResults.error?.message;
  return {
    loading,
    error,
    data: {
      webflowId,
      nocodelyticsId: siteByWebflowIdQueryResults.data?.siteByWebflowId.id,
    },
  };
};
