import { useMemo } from 'react';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import useSocialProof from '../use-social-proof';
const TIER_THRESHOLD = 30;

/**
 * Returns enrollment and treatment state for the social proof unauth block card experiment.
 *
 * Delegates data-fetching to `useSocialProof` (Sasha's shared service layer),
 * which handles cloudId resolution, TAP trait fetch, localStorage caching,
 * request deduping, and sync reads.
 *
 * This hook adds the experiment/cohort layer on top:
 * - Only fires exposure (via editorExperiment) when data has loaded.
 * - Derives `tier` and `isTreatment` for rendering decisions.
 *
 * Expected to be called only inside the fg-enabled branch (via componentWithFG).
 *
 * @param extensionKey - The extensionKey of the current 3P provider (e.g. 'google-object-provider').
 */
const useSocialProofExperiment = (extensionKey, baseUriWithNoTrailingSlash = '') => {
  const {
    connectedPct,
    isEnabled,
    isLoading
  } = useSocialProof(extensionKey, true, baseUriWithNoTrailingSlash);
  return useMemo(() => {
    const hasSocialProofData = connectedPct !== undefined;

    // Only fire exposure when data has loaded (prevents exposure inflation)
    const isTreatment = isEnabled && !isLoading && hasSocialProofData ? editorExperiment('social_proof_3p_unauth_block_exp', true) : false;
    const tier = connectedPct !== undefined && connectedPct >= TIER_THRESHOLD ? 'not-low' : 'low';
    return {
      isTreatment,
      tier,
      connectedPct,
      isLoading
    };
  }, [isEnabled, isLoading, connectedPct]);
};
export default useSocialProofExperiment;