"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 _personalization = require("../../services/personalization");
var NOT_ENABLED_RESULT = {
  connectedPct: undefined,
  isEnabled: false,
  isLoading: false
};

/**
 * Fetches provider usage percentage from the TAP Delivery personalization service when the
 * killswitch allows it. Callers decide separately (e.g. via Statsig experiment) whether to
 * surface that data in the UI.
 */
var useSocialProof = function useSocialProof(traitName, extensionKey) {
  var isKillswitchOn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var isEnabled = isKillswitchOn;
  var _useState = (0, _react.useState)(undefined),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    providerPctMap = _useState2[0],
    setProviderPctMap = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    isPersonalizationLoading = _useState4[0],
    setIsPersonalizationLoading = _useState4[1];
  (0, _react.useEffect)(function () {
    if (!isEnabled) {
      return;
    }
    var cancelled = false;
    setIsPersonalizationLoading(true);
    (0, _personalization.getProviderPctMap)(traitName).then(function (pctMap) {
      if (!cancelled) {
        setProviderPctMap(pctMap);
        setIsPersonalizationLoading(false);
      }
    });
    return function () {
      cancelled = true;
    };
  }, [isEnabled, traitName]);
  return (0, _react.useMemo)(function () {
    if (!isEnabled) {
      return NOT_ENABLED_RESULT;
    }
    return {
      connectedPct: extensionKey && providerPctMap ? providerPctMap[extensionKey] : undefined,
      isEnabled: isEnabled,
      isLoading: isPersonalizationLoading
    };
  }, [extensionKey, isEnabled, isPersonalizationLoading, providerPctMap]);
};
var _default = exports.default = useSocialProof;