/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useCallback, useState } from 'react';
import { SmartLinkStatus } from '../../../../../constants';
import { useMouseDownEvent } from '../../../../../state/analytics/useLinkClicked';
import { useFlexibleCardContext } from '../../../../../state/flexible-ui-context';
import { Title } from '../../elements';
import ActionGroup from '../action-group';
import TitleBlockErroredView from './errored';
import TitleBlockResolvedView from './resolved';
import TitleBlockResolvingView from './resolving';
const actionStyles = null;
const getTitleBlockViewComponent = status => {
  switch (status) {
    case SmartLinkStatus.Pending:
    case SmartLinkStatus.Resolving:
      return TitleBlockResolvingView;
    case SmartLinkStatus.Resolved:
      return TitleBlockResolvedView;
    case SmartLinkStatus.Unauthorized:
    case SmartLinkStatus.Forbidden:
    case SmartLinkStatus.NotFound:
    case SmartLinkStatus.Errored:
    case SmartLinkStatus.Fallback:
    default:
      return TitleBlockErroredView;
  }
};

/**
 * Represents a TitleBlock, which is the foundation of Flexible UI.
 * This contains an icon, the link, and any associated metadata and actions in one block.
 * The TitleBlock will also render differently given the state of the smart link.
 * This can be found in the corresponding Resolving, Resolved and Errored views.
 * @public
 * @param {TitleBlockProps} TitleBlockProps
 * @see Block
 * @see TitleBlockResolvingViewNew
 * @see TitleBlockResolvedViewNew
 * @see TitleBlockErroredViewNew
 */
const TitleBlock = ({
  actions = [],
  anchorTarget,
  hideTitleTooltip,
  maxLines,
  onActionMenuOpenChange,
  showActionOnHover,
  testId = 'smart-block-title',
  text,
  icon,
  hideRetry,
  metadataPosition,
  hideIcon = false,
  className,
  anchorRef,
  CompetitorPrompt,
  url,
  hideIconLoadingSkeleton,
  ...props
}) => {
  var _props$size;
  const cardContext = useFlexibleCardContext();
  const {
    status = SmartLinkStatus.Fallback,
    ui
  } = cardContext || {};
  const [actionDropdownOpen, setActionDropdownOpen] = useState(false);
  const onDropdownOpenChange = useCallback(isOpen => {
    setActionDropdownOpen(isOpen);
    if (onActionMenuOpenChange) {
      onActionMenuOpenChange({
        isOpen
      });
    }
  }, [onActionMenuOpenChange]);
  const actionGroup = actions.length > 0 && /*#__PURE__*/React.createElement(ActionGroup, {
    items: actions,
    visibleButtonsNum: showActionOnHover ? 1 : 2,
    onDropdownOpenChange: onDropdownOpenChange
  });
  const overrideText = !!text ? {
    text
  } : {};
  const onMouseDown = useMouseDownEvent();
  const title = /*#__PURE__*/React.createElement(Title, _extends({
    hideTooltip: hideTitleTooltip,
    maxLines: maxLines,
    onMouseDown: onMouseDown,
    target: anchorTarget,
    theme: ui === null || ui === void 0 ? void 0 : ui.theme,
    anchorRef: anchorRef
  }, overrideText));
  const Component = getTitleBlockViewComponent(status);
  return /*#__PURE__*/React.createElement(Component, _extends({}, props, {
    actionGroup: actionGroup
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
    ,
    className: ax([showActionOnHover && !actionDropdownOpen && "_1bfhidpf _wn5xkb7n _h157kb7n", className]),
    testId: testId,
    title: title,
    metadataPosition: metadataPosition,
    hideIcon: hideIcon,
    hideRetry: hideRetry,
    icon: icon,
    hideIconLoadingSkeleton: hideIconLoadingSkeleton,
    size: (_props$size = props.size) !== null && _props$size !== void 0 ? _props$size : ui === null || ui === void 0 ? void 0 : ui.size,
    CompetitorPrompt: CompetitorPrompt,
    url: url
  }));
};
export default TitleBlock;