import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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; }
import { useMemo } from 'react';

// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
import uuid from 'uuid';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { useAnalyticsEvents } from '../../common/analytics/generated/use-analytics-events';
import { extractInvokeDownloadAction } from '../../extractors/action/extract-invoke-download-action';
import { extractInvokePreviewAction } from '../../extractors/action/extract-invoke-preview-action';
import { extractInvokeViewAction } from '../../extractors/action/extract-invoke-view-action';
import { messages } from '../../messages';
import { toAction } from '../../utils/actions/to-action';
import useInvokeClientAction from '../hooks/use-invoke-client-action';
import useResolve from '../hooks/use-resolve';
import { useSmartLinkCrossProductUrlWrapperGated } from '../hooks/use-smart-link-cross-product-url-wrapper';
import { useSmartCardState as useLinkState } from '../store';
export 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 = useMemo(function () {
    return uuid();
  }, []);
  var linkState = useLinkState(url);
  var _useAnalyticsEvents = useAnalyticsEvents(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var _useSmartLinkContext = useSmartLinkContext(),
    isPreviewPanelAvailable = _useSmartLinkContext.isPreviewPanelAvailable,
    openPreviewPanel = _useSmartLinkContext.openPreviewPanel;
  var invokeClientAction = useInvokeClientAction({
    fireEvent: fireEvent
  });
  var resolve = useResolve();
  var appendCrossProductAnalyticsParams = useSmartLinkCrossProductUrlWrapperGated({
    details: linkState.details
  });
  if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && prefetch && !linkState.details) {
    resolve({
      url: 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 = extractInvokeDownloadAction(invokeParam);
    if (downloadActionProps) {
      actions.push(toAction(downloadActionProps, invokeClientAction, messages.download, 'download-content'));
    }
    var viewActionProps = fg('platform_smartlink_xpc_url_wrapping') ? extractInvokeViewAction(_objectSpread(_objectSpread({}, invokeParam), {}, {
      transformUrl: function transformUrl() {
        var destinationUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url;
        return appendCrossProductAnalyticsParams(destinationUrl);
      }
    })) : extractInvokeViewAction(invokeParam);
    if (viewActionProps) {
      actions.push(toAction(viewActionProps, invokeClientAction, messages.view, 'view-content'));
    }
    var previewActionProps = extractInvokePreviewAction(_objectSpread(_objectSpread({}, invokeParam), {}, {
      fireEvent: fireEvent,
      origin: origin,
      isPreviewPanelAvailable: isPreviewPanelAvailable,
      openPreviewPanel: openPreviewPanel
    }, fg('platform_smartlink_xpc_url_wrapping') ? {
      transformUrl: function transformUrl() {
        var destinationUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : url;
        return appendCrossProductAnalyticsParams(destinationUrl);
      }
    } : undefined));
    if (previewActionProps) {
      actions.push(toAction(previewActionProps.invokeAction, invokeClientAction, messages.preview_improved, 'preview-content'));
    }
    return actions;
  }
  return [];
}