/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["actions", "anchorTarget", "hideTitleTooltip", "maxLines", "onActionMenuOpenChange", "showActionOnHover", "testId", "text", "icon", "hideRetry", "metadataPosition", "hideIcon", "className", "anchorRef", "CompetitorPrompt", "url", "hideIconLoadingSkeleton"];
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';
var actionStyles = null;
var getTitleBlockViewComponent = function 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
 */
var TitleBlock = function TitleBlock(_ref) {
  var _props$size;
  var _ref$actions = _ref.actions,
    actions = _ref$actions === void 0 ? [] : _ref$actions,
    anchorTarget = _ref.anchorTarget,
    hideTitleTooltip = _ref.hideTitleTooltip,
    maxLines = _ref.maxLines,
    onActionMenuOpenChange = _ref.onActionMenuOpenChange,
    showActionOnHover = _ref.showActionOnHover,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-block-title' : _ref$testId,
    text = _ref.text,
    icon = _ref.icon,
    hideRetry = _ref.hideRetry,
    metadataPosition = _ref.metadataPosition,
    _ref$hideIcon = _ref.hideIcon,
    hideIcon = _ref$hideIcon === void 0 ? false : _ref$hideIcon,
    className = _ref.className,
    anchorRef = _ref.anchorRef,
    CompetitorPrompt = _ref.CompetitorPrompt,
    url = _ref.url,
    hideIconLoadingSkeleton = _ref.hideIconLoadingSkeleton,
    props = _objectWithoutProperties(_ref, _excluded);
  var cardContext = useFlexibleCardContext();
  var _ref2 = cardContext || {},
    _ref2$status = _ref2.status,
    status = _ref2$status === void 0 ? SmartLinkStatus.Fallback : _ref2$status,
    ui = _ref2.ui;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    actionDropdownOpen = _useState2[0],
    setActionDropdownOpen = _useState2[1];
  var onDropdownOpenChange = useCallback(function (isOpen) {
    setActionDropdownOpen(isOpen);
    if (onActionMenuOpenChange) {
      onActionMenuOpenChange({
        isOpen: isOpen
      });
    }
  }, [onActionMenuOpenChange]);
  var actionGroup = actions.length > 0 && /*#__PURE__*/React.createElement(ActionGroup, {
    items: actions,
    visibleButtonsNum: showActionOnHover ? 1 : 2,
    onDropdownOpenChange: onDropdownOpenChange
  });
  var overrideText = !!text ? {
    text: text
  } : {};
  var onMouseDown = useMouseDownEvent();
  var 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));
  var 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;