"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSmartLinkActions = useSmartLinkActions;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _uuid = _interopRequireDefault(require("uuid"));
var _linkProvider = require("@atlaskit/link-provider");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _useAnalyticsEvents2 = require("../../common/analytics/generated/use-analytics-events");
var _extractInvokeDownloadAction = require("../../extractors/action/extract-invoke-download-action");
var _extractInvokePreviewAction = require("../../extractors/action/extract-invoke-preview-action");
var _extractInvokeViewAction = require("../../extractors/action/extract-invoke-view-action");
var _messages = require("../../messages");
var _toAction = require("../../utils/actions/to-action");
var _useInvokeClientAction = _interopRequireDefault(require("../hooks/use-invoke-client-action"));
var _useResolve = _interopRequireDefault(require("../hooks/use-resolve"));
var _store = require("../store");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
function useSmartLinkActions(_ref) {
  var url = _ref.url,
    appearance = _ref.appearance,
    origin = _ref.origin,
    actionOptions = _ref.actionOptions,
    prefetch = _ref.prefetch;
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  var id = (0, _react.useMemo)(function () {
    return (0, _uuid.default)();
  }, []);
  var linkState = (0, _store.useSmartCardState)(url);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel;
  var invokeClientAction = (0, _useInvokeClientAction.default)({
    fireEvent: fireEvent
  });
  var resolve = (0, _useResolve.default)();
  if ((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && prefetch && !linkState.details) {
    resolve(url);
  }
  if (linkState.details && !(actionOptions !== null && actionOptions !== void 0 && actionOptions.hide)) {
    var actions = [];
    var invokeParam = {
      actionOptions: actionOptions,
      appearance: appearance,
      id: id,
      response: linkState.details
    };
    var downloadActionProps = (0, _extractInvokeDownloadAction.extractInvokeDownloadAction)(invokeParam);
    if (downloadActionProps) {
      actions.push((0, _toAction.toAction)(downloadActionProps, invokeClientAction, _messages.messages.download, 'download-content'));
    }
    var viewActionProps = (0, _extractInvokeViewAction.extractInvokeViewAction)(invokeParam);
    if (viewActionProps) {
      actions.push((0, _toAction.toAction)(viewActionProps, invokeClientAction, _messages.messages.view, 'view-content'));
    }
    var previewActionProps = (0, _extractInvokePreviewAction.extractInvokePreviewAction)(_objectSpread(_objectSpread({}, invokeParam), {}, {
      fireEvent: fireEvent,
      origin: origin,
      isPreviewPanelAvailable: isPreviewPanelAvailable,
      openPreviewPanel: openPreviewPanel
    }));
    if (previewActionProps) {
      actions.push((0, _toAction.toAction)(previewActionProps.invokeAction, invokeClientAction, _messages.messages.preview_improved, 'preview-content'));
    }
    return actions;
  }
  return [];
}