import { extractSmartLinkUrl } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { ActionName, CardAction } from '../../../constants';
import { getDefinitionId, getExtensionKey, getResourceType } from '../../../state/helpers';
import { canShowAction } from '../../../utils/actions/can-show-action';
import { getIsRovoChatEnabled } from '../../../utils/rovo';
// For block card experiment (NAVX-4814)
const 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']);
const extractRovoChatAction = ({
  actionOptions,
  appearance,
  id,
  isEmbedRovoActionsFooterExperimentEnabled,
  product,
  response,
  rovoConfig
}) => {
  var _actionOptions$rovoCh;
  if (!canShowAction(CardAction.RovoChatAction, actionOptions)) {
    return;
  }
  const isRovoChatEnabled = getIsRovoChatEnabled(rovoConfig === null || rovoConfig === void 0 ? void 0 : rovoConfig.rovoOptions);
  if (!isRovoChatEnabled) {
    return;
  }
  const extensionKey = getExtensionKey(response);
  const isInlineExperimentEnabled = fg('platform_sl_3p_auth_inline_tailored_cta_killswitch') && expValEqualsNoExposure('platform_sl_3p_auth_inline_tailored_cta', 'isEnabled', true);
  const is3PBlockPostAuthActionsEnabled = extensionKey !== undefined && ELIGIBLE_EXTENSION_KEYS.has(extensionKey) && (rovoConfig === null || rovoConfig === void 0 ? void 0 : rovoConfig.product) === 'CONFLUENCE';
  const is3PEmbedPostAuthActionsEnabled = isEmbedRovoActionsFooterExperimentEnabled === true;
  const isSupportedFeature = is3PBlockPostAuthActionsEnabled || isInlineExperimentEnabled || is3PEmbedPostAuthActionsEnabled;
  const isOptIn = (actionOptions === null || actionOptions === void 0 ? void 0 : (_actionOptions$rovoCh = actionOptions.rovoChatAction) === null || _actionOptions$rovoCh === void 0 ? void 0 : _actionOptions$rovoCh.optIn) === true;
  const url = extractSmartLinkUrl(response);
  return isSupportedFeature && isOptIn ? {
    invokeAction: {
      actionSubjectId: 'rovoChatPrompt',
      actionType: ActionName.RovoChatAction,
      definitionId: getDefinitionId(response),
      display: appearance,
      extensionKey,
      id,
      resourceType: getResourceType(response)
    },
    product,
    url
  } : undefined;
};
export default extractRovoChatAction;