import { useMemo } from 'react';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { getIsRovoChatEnabled } from '../../../utils/rovo';
import { getExtensionKey } from '../../helpers';
import { useSmartCardState } from '../../store';
const EMBED_ROVO_ACTIONS_FOOTER_ELIGIBLE_EXTENSION_KEYS = new Set(['google-object-provider', 'onedrive-object-provider', 'github-object-provider', 'gitlab-object-provider']);
const isEligibleEmbedRovoActionsFooterExtensionKey = extensionKey => extensionKey !== undefined && EMBED_ROVO_ACTIONS_FOOTER_ELIGIBLE_EXTENSION_KEYS.has(extensionKey);
export const EMBED_ROVO_ACTIONS_FOOTER_EXPERIMENT_KEY = 'platform_sl_3p_auth_rovo_embed_footer_exp';
const NOT_ENABLED_RESULT = {
  isEnabled: false
};
export const isEmbedRovoActionsFooterExperimentEnabled = product => {
  return product === 'CONFLUENCE' && fg('platform_sl_3p_auth_rovo_embed_footer_kill_switch') && expValEquals(EMBED_ROVO_ACTIONS_FOOTER_EXPERIMENT_KEY, 'isEnabled', true);
};
const isEmbedRovoActionsFooterKillSwitchEnabled = product => {
  return product === 'CONFLUENCE' && fg('platform_sl_3p_auth_rovo_embed_footer_kill_switch');
};
const isEmbedRovoActionsFooterExperimentEnabledNoExposure = product => {
  return isEmbedRovoActionsFooterKillSwitchEnabled(product) && expValEqualsNoExposure(EMBED_ROVO_ACTIONS_FOOTER_EXPERIMENT_KEY, 'isEnabled', true);
};
export const getEmbedRovoActionsFooterExperimentMeta = ({
  extensionKey,
  isRovoChatActionOptedIn,
  isRovoChatEnabled,
  product
}) => {
  const isEligible = isEmbedRovoActionsFooterKillSwitchEnabled(product) && isRovoChatEnabled && isRovoChatActionOptedIn && isEligibleEmbedRovoActionsFooterExtensionKey(extensionKey);
  if (!isEligible) {
    return undefined;
  }
  return {
    [EMBED_ROVO_ACTIONS_FOOTER_EXPERIMENT_KEY]: {
      isEligible: true,
      isTreatment: isEmbedRovoActionsFooterExperimentEnabledNoExposure(product)
    }
  };
};
const useEmbedRovoActionsFooterExperiment = (url, actionOptions, rovoOptions, product) => {
  var _actionOptions$rovoCh, _actionOptions$rovoCh2;
  const isRovoChatEnabled = getIsRovoChatEnabled(rovoOptions);
  const cardState = useSmartCardState(url !== null && url !== void 0 ? url : '');
  const extensionKey = getExtensionKey(cardState.details);
  const isRovoChatActionOptedIn = (_actionOptions$rovoCh = actionOptions === null || actionOptions === void 0 ? void 0 : (_actionOptions$rovoCh2 = actionOptions.rovoChatAction) === null || _actionOptions$rovoCh2 === void 0 ? void 0 : _actionOptions$rovoCh2.optIn) !== null && _actionOptions$rovoCh !== void 0 ? _actionOptions$rovoCh : false;
  return useMemo(() => {
    if (!isRovoChatEnabled || !url || !isRovoChatActionOptedIn || !extensionKey) {
      return NOT_ENABLED_RESULT;
    }
    if (!isEligibleEmbedRovoActionsFooterExtensionKey(extensionKey)) {
      return NOT_ENABLED_RESULT;
    }
    return {
      isEnabled: isEmbedRovoActionsFooterExperimentEnabled(product)
    };
  }, [isRovoChatEnabled, extensionKey, url, isRovoChatActionOptedIn, product]);
};
export default useEmbedRovoActionsFooterExperiment;