import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useMemo, useState } from 'react';
import { getProviderPctMap } from '../../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 = useState(undefined),
    _useState2 = _slicedToArray(_useState, 2),
    providerPctMap = _useState2[0],
    setProviderPctMap = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isPersonalizationLoading = _useState4[0],
    setIsPersonalizationLoading = _useState4[1];
  useEffect(function () {
    if (!isEnabled) {
      return;
    }
    var cancelled = false;
    setIsPersonalizationLoading(true);
    getProviderPctMap(traitName).then(function (pctMap) {
      if (!cancelled) {
        setProviderPctMap(pctMap);
        setIsPersonalizationLoading(false);
      }
    });
    return function () {
      cancelled = true;
    };
  }, [isEnabled, traitName]);
  return useMemo(function () {
    if (!isEnabled) {
      return NOT_ENABLED_RESULT;
    }
    return {
      connectedPct: extensionKey && providerPctMap ? providerPctMap[extensionKey] : undefined,
      isEnabled: isEnabled,
      isLoading: isPersonalizationLoading
    };
  }, [extensionKey, isEnabled, isPersonalizationLoading, providerPctMap]);
};
export default useSocialProof;