"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _rovo = require("../../../utils/rovo");
var _helpers = require("../../helpers");
var _store = require("../../store");
var _useRovoConfig2 = _interopRequireDefault(require("../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']);

/**
 * Returns whether the rovo actions are 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.
 *
 * 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 useBlockCardRovoAction = function useBlockCardRovoAction(url, actionOptions) {
  var _actionOptions$rovoCh, _actionOptions$rovoCh2;
  var _useRovoConfig = (0, _useRovoConfig2.default)(),
    rovoConfig = _useRovoConfig.rovoOptions,
    product = _useRovoConfig.product;
  var isRovoChatEnabled = (0, _rovo.getIsRovoChatEnabled)(rovoConfig);
  var cardState = (0, _store.useSmartCardState)(url !== null && url !== void 0 ? url : '');
  var extensionKey = (0, _helpers.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 (0, _react.useMemo)(function () {
    if (!isRovoChatEnabled || !url || !isRovoChatActionOptedIn) {
      return false;
    }
    if (extensionKey && !ELIGIBLE_EXTENSION_KEYS.has(extensionKey)) {
      return false;
    }
    return !!product && product === 'CONFLUENCE';
  }, [isRovoChatEnabled, extensionKey, url, isRovoChatActionOptedIn, product]);
};
var _default = exports.default = useBlockCardRovoAction;