import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 var SOCIAL_PROOF_3P_UNAUTH_BLOCK_EXPERIMENT_KEY = 'social_proof_3p_unauth_block_exp';
export var INLINE_SOCIAL_PROOF_EXPERIMENT_KEY = 'platform_sl_3p_preauth_social_proof_inline_cta';
export var SOCIAL_PROOF_TIER_THRESHOLD = 30;
export var getSocialProofTier = function getSocialProofTier(connectedPct) {
  if (connectedPct === undefined) {
    return undefined;
  }
  return connectedPct >= SOCIAL_PROOF_TIER_THRESHOLD ? 'not-low' : 'low';
};
var getSocialProofExperimentMetadata = function getSocialProofExperimentMetadata(_ref) {
  var extensionKey = _ref.extensionKey,
    _ref$baseUriWithNoTra = _ref.baseUriWithNoTrailingSlash,
    baseUriWithNoTrailingSlash = _ref$baseUriWithNoTra === void 0 ? '' : _ref$baseUriWithNoTra;
  if (!extensionKey) {
    return {
      isEligible: false
    };
  }
  var cloudId = getCurrentSiteCloudIdSync(baseUriWithNoTrailingSlash);
  var providerPctMap = getProviderPctMapSync(cloudId, SOCIAL_PROOF_TRAIT_NAME);
  var tier = getSocialProofTier(providerPctMap === null || providerPctMap === void 0 ? void 0 : providerPctMap[extensionKey]);
  return _objectSpread({
    isEligible: tier !== undefined
  }, tier ? {
    tier: tier
  } : {});
};
export var getSocialProofExperimentMeta = function getSocialProofExperimentMeta(params) {
  return _defineProperty({}, SOCIAL_PROOF_3P_UNAUTH_BLOCK_EXPERIMENT_KEY, getSocialProofExperimentMetadata(params));
};
export var getInlineSocialProofExperimentMeta = function getInlineSocialProofExperimentMeta(params) {
  return _defineProperty({}, 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').
 */
var useSocialProofExperiment = function useSocialProofExperiment(extensionKey) {
  var baseUriWithNoTrailingSlash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var _useSocialProof = useSocialProof(extensionKey, true, baseUriWithNoTrailingSlash),
    connectedPct = _useSocialProof.connectedPct,
    isEnabled = _useSocialProof.isEnabled,
    isLoading = _useSocialProof.isLoading;
  return useMemo(function () {
    var _getSocialProofTier;
    var hasSocialProofData = connectedPct !== undefined;

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