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';
import { JIRA_PRODUCTS } from '../use-rovo-chat/constants';
import useRovoConfig from '../use-rovo-config';
var ELIGIBLE_EXTENSION_KEYS = new Set(['slack-object-provider', 'google-object-provider', 'onedrive-object-provider', 'github-object-provider', 'ms-teams-object-provider', 'gitlab-object-provider', 'salesforce-object-provider']);
var NOT_ENABLED_RESULT = {
  isEnabled: false
};

/**
 * Returns whether the platform_sl_3p_auth_rovo_block_card_jira or platform_sl_3p_auth_rovo_block_card_confluence experiment
 * is enabled for the current user and link context.
 *
 * All eligibility criteria are consolidated here:
 * 1. Rovo chat must be enabled for the tenant.
 * 2. The consumer must have opted in via actionOptions.rovoChatAction.optIn.
 * 3. The link must support the RovoActions feature.
 * 4. The extension key must be one of the supported options.
 * 5. The experiment value must be true (via tmp-editor-statsig).
 *
 * The extension key is derived from the card store via the resolved URL,
 * so callers don't need to thread it as a prop.
 */
var useBlockCardRovoActionExperiment = function useBlockCardRovoActionExperiment(url, actionOptions) {
  var _actionOptions$rovoCh, _actionOptions$rovoCh2;
  var _useRovoConfig = useRovoConfig(),
    rovoConfig = _useRovoConfig.rovoOptions,
    product = _useRovoConfig.product;
  var isRovoChatEnabled = getIsRovoChatEnabled(rovoConfig);
  var cardState = useSmartCardState(url !== null && url !== void 0 ? url : '');
  var extensionKey = getExtensionKey(cardState.details);
  var isRovoChatActionOptedIn = (_actionOptions$rovoCh = actionOptions === null || actionOptions === 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(function () {
    if (!isRovoChatEnabled || !url || !isRovoChatActionOptedIn) {
      return NOT_ENABLED_RESULT;
    }
    if (extensionKey && !ELIGIBLE_EXTENSION_KEYS.has(extensionKey)) {
      return NOT_ENABLED_RESULT;
    }
    var isJiraEnabled = !!product && JIRA_PRODUCTS.includes(product) && fg('platform_sl_3p_auth_rovo_block_jira_kill_switch') && expValEquals('platform_sl_3p_auth_rovo_block_card_jira', 'isEnabled', true);
    var isConfluenceEnabled = !!product && product === 'CONFLUENCE' && fg('platform_sl_3p_auth_rovo_block_card_kill_switch') && expValEquals('platform_sl_3p_auth_rovo_block_card_confluence', 'isEnabled', true);
    return {
      isEnabled: isJiraEnabled || isConfluenceEnabled
    };
  }, [isRovoChatEnabled, extensionKey, url, isRovoChatActionOptedIn, product]);
};
export var isBlockCardRovoActionExperimentEnabled = function isBlockCardRovoActionExperimentEnabled(product) {
  var isJiraEnabled = !!product && JIRA_PRODUCTS.includes(product) && fg('platform_sl_3p_auth_rovo_block_jira_kill_switch') && expValEqualsNoExposure('platform_sl_3p_auth_rovo_block_card_jira', 'isEnabled', true);
  var isConfluenceEnabled = !!product && product === 'CONFLUENCE' && fg('platform_sl_3p_auth_rovo_block_card_kill_switch') && expValEqualsNoExposure('platform_sl_3p_auth_rovo_block_card_confluence', 'isEnabled', true);
  return isJiraEnabled || isConfluenceEnabled;
};
export var useBlockCardRovoActionExperimentNoExposure = function useBlockCardRovoActionExperimentNoExposure() {
  var _useRovoConfig2 = useRovoConfig(),
    product = _useRovoConfig2.product;
  return isBlockCardRovoActionExperimentEnabled(product);
};
export default useBlockCardRovoActionExperiment;