import { useMemo } from 'react';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { getCurrentSiteCloudIdSync } from '../../services/current-site-cloud-id';
import { getProviderPctMapSync, SOCIAL_PROOF_TRAIT_NAME } from '../../services/personalization';
import useSocialProof from '../use-social-proof';
export const SOCIAL_PROOF_3P_UNAUTH_BLOCK_EXPERIMENT_KEY = 'social_proof_3p_unauth_block_exp';
export const INLINE_SOCIAL_PROOF_EXPERIMENT_KEY = 'platform_sl_3p_preauth_social_proof_inline_cta';
export const SOCIAL_PROOF_TIER_THRESHOLD = 30;
export const getSocialProofTier = connectedPct => {
  if (connectedPct === undefined) {
    return undefined;
  }
  return connectedPct >= SOCIAL_PROOF_TIER_THRESHOLD ? 'not-low' : 'low';
};
const getSocialProofExperimentMetadata = ({
  extensionKey,
  baseUriWithNoTrailingSlash = ''
}) => {
  if (!extensionKey) {
    return {
      isEligible: false
    };
  }
  const cloudId = getCurrentSiteCloudIdSync(baseUriWithNoTrailingSlash);
  const providerPctMap = getProviderPctMapSync(cloudId, SOCIAL_PROOF_TRAIT_NAME);
  const tier = getSocialProofTier(providerPctMap === null || providerPctMap === void 0 ? void 0 : providerPctMap[extensionKey]);
  return {
    isEligible: tier !== undefined,
    ...(tier ? {
      tier
    } : {})
  };
};
export const getSocialProofExperimentMeta = params => ({
  [SOCIAL_PROOF_3P_UNAUTH_BLOCK_EXPERIMENT_KEY]: getSocialProofExperimentMetadata(params)
});
export const getInlineSocialProofExperimentMeta = params => ({
  [INLINE_SOCIAL_PROOF_EXPERIMENT_KEY]: getSocialProofExperimentMetadata(params)
});

/**
 * 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(() => {
    var _getSocialProofTier;
    const hasSocialProofData = connectedPct !== undefined;

    // Only fire exposure when data has loaded (prevents exposure inflation)
    const isTreatment = isEnabled && !isLoading && hasSocialProofData ? expValEquals(SOCIAL_PROOF_3P_UNAUTH_BLOCK_EXPERIMENT_KEY, 'isEnabled', true) : false;
    const tier = (_getSocialProofTier = getSocialProofTier(connectedPct)) !== null && _getSocialProofTier !== void 0 ? _getSocialProofTier : 'low';
    return {
      isTreatment,
      tier,
      connectedPct,
      isLoading
    };
  }, [isEnabled, isLoading, connectedPct]);
};
export default useSocialProofExperiment;