"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _currentSiteCloudId = require("../../services/current-site-cloud-id");
var _personalization = require("../../services/personalization");
var NOT_ENABLED_RESULT = {
  connectedPct: undefined,
  isEnabled: false,
  isLoading: false
};

/**
 * Cache-first social proof hook.
 *
 * On mount:
 * 1. Reads localStorage synchronously via `getProviderPctMapSync`.
 *    - If data exists (warm cache): sets `providerPctMap` immediately, `isLoading = false`.
 *    - If no data (cold cache): leaves `providerPctMap` undefined, `isLoading = false`.
 * 2. Always kicks off an async fetch (fire-and-forget) via `getProviderPctMap` to populate
 *    localStorage for next page load. Does NOT update state with the async result.
 *
 * This means:
 * - First page visit (cold): no social proof rendered, no experiment exposure fired.
 *   Background fetch populates localStorage for next time.
 * - Second page visit (warm): social proof renders immediately from localStorage.
 *   Background refresh keeps localStorage fresh.
 *
 * Callers decide separately (e.g. via Statsig experiment) whether to surface the data.
 */
var useSocialProof = function useSocialProof(extensionKey) {
  var isKillswitchOn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var baseUriWithNoTrailingSlash = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var isEnabled = isKillswitchOn;
  var _useState = (0, _react.useState)(function () {
      if (!isEnabled) {
        return {
          cloudId: undefined,
          providerPctMap: null
        };
      }
      var cloudId = (0, _currentSiteCloudId.getCurrentSiteCloudIdSync)(baseUriWithNoTrailingSlash);
      return {
        cloudId: cloudId,
        providerPctMap: (0, _personalization.getProviderPctMapSync)(cloudId, _personalization.SOCIAL_PROOF_TRAIT_NAME)
      };
    }),
    _useState2 = (0, _slicedToArray2.default)(_useState, 1),
    snapshot = _useState2[0];

  // Fire-and-forget: warm caches for future mounts only. Never update this mount's treatment UI.
  (0, _react.useEffect)(function () {
    if (!isEnabled) {
      return;
    }
    void (0, _currentSiteCloudId.getCurrentSiteCloudId)(baseUriWithNoTrailingSlash).then(function (cloudId) {
      void (0, _personalization.getProviderPctMap)(cloudId, _personalization.SOCIAL_PROOF_TRAIT_NAME);
    });
  }, [baseUriWithNoTrailingSlash, isEnabled]);
  return (0, _react.useMemo)(function () {
    if (!isEnabled) {
      return NOT_ENABLED_RESULT;
    }
    return {
      connectedPct: extensionKey && snapshot.providerPctMap ? snapshot.providerPctMap[extensionKey] : undefined,
      isEnabled: Boolean(snapshot.cloudId && snapshot.providerPctMap),
      isLoading: false // sync read is instant; never in "loading" state
    };
  }, [extensionKey, isEnabled, snapshot]);
};
var _default = exports.default = useSocialProof;