"use client";

import { useEffect, useState } from "react";
import {
  readStashedSocialProfile,
  takeSocialPrefill,
  type SocialUserType,
} from "@/lib/auth/socialSignup";

/**
 * The provider profile stashed before a Google / LinkedIn redirect.
 *
 * `sessionStorage` cannot be read during the first render: these are Client
 * Components that are still server-rendered, and reading it in a lazy
 * `useState` initialiser would either throw on the server or hydrate against a
 * different value than the server produced. Unlike the viewport width this is
 * also not a *store* — `useSyncExternalStore` is the wrong tool, because
 * `clearSocialSignupState()` deletes the entry once the social-auth call
 * succeeds and `getSnapshot` would then report `null` back into a screen that
 * is mid-submit with the profile it read.
 *
 * So a mount effect with a `setState` in it is the right shape, and the
 * `react-hooks/set-state-in-effect` report on it is accepted — **once**, here,
 * rather than in each of the three screens that used to carry this exact
 * `useState(null)` + read + `JSON.parse` + `setState` sequence
 * (`useCompleteDetails`, `useVolunteerMandate`, `JoinUs`), each separately
 * reported by the rule.
 *
 * `null` means "not read yet" *and* "nothing stashed"; no caller distinguishes
 * them, because both mean the same thing — carry on with an empty form.
 */
export function useStashedSocialProfile<T>(): T | null {
  const [profile, setProfile] = useState<T | null>(null);

  useEffect(() => {
    setProfile(readStashedSocialProfile<T>());
  }, []);

  return profile;
}

/**
 * The answers the visitor had already typed before clicking Google / LinkedIn,
 * read once and dropped.
 *
 * Consuming is the whole point — `takeSocialPrefill` removes the entry, so an
 * abandoned sign-up cannot repopulate an unrelated one later in the same tab —
 * which rules out both a lazy `useState` initialiser (it would run during the
 * server render, where `sessionStorage` does not exist, and StrictMode's second
 * render would find the stash already gone) and `useSyncExternalStore` (whose
 * `getSnapshot` is called repeatedly and must not have side effects).
 *
 * So, again, a mount effect, and the `react-hooks/set-state-in-effect` report
 * on it is accepted once here instead of in `useCompleteDetails` and
 * `useVolunteerMandate`, which carried the identical pair.
 *
 * `skip` is for the modal rendering of the mandate form, which is handed its
 * profile as a prop and must not consume a stash meant for the page.
 */
export function useConsumedSocialPrefill(
  userType: SocialUserType,
  skip = false
): Record<string, unknown> | null {
  const [prefill, setPrefill] = useState<Record<string, unknown> | null>(null);

  useEffect(() => {
    if (skip) return;
    /* `if (parked)`, not an unconditional write: the read *consumes* the
       stash, so StrictMode's second pass finds nothing and must not clear
       what the first pass picked up. */
    const parked = takeSocialPrefill(userType);
    if (parked) setPrefill(parked);
  }, [userType, skip]);

  return prefill;
}
