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 { 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 { useSmartCardState as useLinkState } from '../store';
export function useSmartLinkActions({
  url,
  appearance,
  origin,
  actionOptions,
  prefetch
}) {
  // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
  const id = useMemo(() => uuid(), []);
  const linkState = useLinkState(url);
  const {
    fireEvent
  } = useAnalyticsEvents();
  const {
    isPreviewPanelAvailable,
    openPreviewPanel
  } = useSmartLinkContext();
  const invokeClientAction = useInvokeClientAction({
    fireEvent
  });
  const resolve = useResolve();
  if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && prefetch && !linkState.details) {
    resolve(url);
  }
  if (linkState.details && !(actionOptions !== null && actionOptions !== void 0 && actionOptions.hide)) {
    const actions = [];
    const invokeParam = {
      actionOptions,
      appearance,
      id,
      response: linkState.details
    };
    const downloadActionProps = extractInvokeDownloadAction(invokeParam);
    if (downloadActionProps) {
      actions.push(toAction(downloadActionProps, invokeClientAction, messages.download, 'download-content'));
    }
    const viewActionProps = extractInvokeViewAction(invokeParam);
    if (viewActionProps) {
      actions.push(toAction(viewActionProps, invokeClientAction, messages.view, 'view-content'));
    }
    const previewActionProps = extractInvokePreviewAction({
      ...invokeParam,
      fireEvent,
      origin,
      isPreviewPanelAvailable,
      openPreviewPanel
    });
    if (previewActionProps) {
      actions.push(toAction(previewActionProps.invokeAction, invokeClientAction, messages.preview_improved, 'preview-content'));
    }
    return actions;
  }
  return [];
}