/* index.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React from 'react';
import { FormattedMessage } from 'react-intl-next';
import LockLockedIcon from '@atlaskit/icon/core/lock-locked';
import LegacyLockIcon from '@atlaskit/icon/glyph/lock-filled';
import { fg } from '@atlaskit/platform-feature-flags';
import { Pressable } from '@atlaskit/primitives/compiled';
import { N500 } from '@atlaskit/theme/colors';
import { messages } from '../../../messages';
import { HoverCard } from '../../HoverCard';
import { ActionButton } from '../common/action-button';
import InlineLozenge from '../common/inline-lozenge';
import { Frame } from '../Frame';
import { IconAndTitleLayout } from '../IconAndTitleLayout';
const styles = {
  actionButtonLozengeStyle: "_2rkolb4i _bfhksm61 _ca0qze3t _u5f3ze3t _n3tdze3t _19bvze3t _p12f1osq"
};
const fallbackForbiddenIcon = () => {
  return /*#__PURE__*/React.createElement(LockLockedIcon, {
    label: "error",
    color: "var(--ds-icon-danger, #C9372C)",
    LEGACY_fallbackIcon: LegacyLockIcon,
    LEGACY_size: "small",
    testId: "forbidden-view-fallback-icon"
  });
};
export const InlineCardForbiddenView = ({
  url,
  icon,
  onClick,
  isSelected,
  testId = 'inline-card-forbidden-view',
  truncateInline,
  requestAccessContext,
  onAuthorise,
  context,
  showHoverPreview
}) => {
  const frameRef = React.useRef(null);
  const [hasRequestAccessContextMessage] = React.useState(!!(requestAccessContext !== null && requestAccessContext !== void 0 && requestAccessContext.callToActionMessageKey));
  const handleRetry = React.useCallback(event => {
    event.preventDefault();
    event.stopPropagation();
    if (onAuthorise) {
      onAuthorise();
    } else {
      var _requestAccessContext;
      requestAccessContext === null || requestAccessContext === void 0 ? void 0 : (_requestAccessContext = requestAccessContext.action) === null || _requestAccessContext === void 0 ? void 0 : _requestAccessContext.promise();
    }
  }, [onAuthorise, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.action]);
  const renderForbiddenAccessMessage = React.useCallback(() => {
    if (requestAccessContext !== null && requestAccessContext !== void 0 && requestAccessContext.callToActionMessageKey) {
      const {
        callToActionMessageKey
      } = requestAccessContext;
      return /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, messages[callToActionMessageKey], {
        values: {
          product: context
        }
      }));
    }
    return /*#__PURE__*/React.createElement(FormattedMessage, messages.invalid_permissions, formattedMessage => /*#__PURE__*/React.createElement(React.Fragment, null, formattedMessage));
  }, [context, requestAccessContext]);
  const renderActionButton = React.useCallback(() => {
    const accessType = requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.accessType;
    if (hasRequestAccessContextMessage) {
      const isDisabled = accessType === 'PENDING_REQUEST_EXISTS';
      return /*#__PURE__*/React.createElement(ActionButton, {
        isDisabled: isDisabled,
        onClick: handleRetry,
        testId: "button-connect-other-account"
      }, renderForbiddenAccessMessage());
    }
    if (onAuthorise) {
      return /*#__PURE__*/React.createElement(Pressable, {
        xcss: styles.actionButtonLozengeStyle,
        onClick: handleRetry,
        style: {
          font: `inherit`
        },
        testId: "button-connect-other-account"
      }, /*#__PURE__*/React.createElement(InlineLozenge, _extends({
        appearance: "moved"
      }, fg('platform-component-visual-refresh') ? {
        isBold: true
      } : undefined), renderForbiddenAccessMessage()));
    }
    return null;
  }, [handleRetry, hasRequestAccessContextMessage, onAuthorise, renderForbiddenAccessMessage, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.accessType]);
  const content = /*#__PURE__*/React.createElement(Frame, {
    testId: testId,
    isSelected: isSelected,
    ref: frameRef,
    truncateInline: truncateInline
  }, /*#__PURE__*/React.createElement(IconAndTitleLayout, {
    icon: icon ? icon : fallbackForbiddenIcon(),
    link: url,
    title: url,
    onClick: onClick,
    titleColor: `var(--ds-text-subtle, ${N500})`
  }), renderActionButton());
  if (showHoverPreview) {
    return /*#__PURE__*/React.createElement(HoverCard, {
      url: url
    }, content);
  }
  return content;
};