/* ForbiddenView.tsx generated by @compiled/babel-plugin v0.38.1 */
import _extends from "@babel/runtime/helpers/extends";
import "./ForbiddenView.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useMemo } from 'react';
import { useIntl } from 'react-intl-next';
import { useAnalyticsEvents } from '@atlaskit/analytics-next';
import LockLockedIcon from '@atlaskit/icon/core/lock-locked';
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import { extractRequestAccessContextImproved } from '../../../extractors/common/context/extractAccessContext';
import extractHostname from '../../../extractors/common/hostname/extractHostname';
import { messages } from '../../../messages';
import { toMessage } from '../../../utils/intl-utils';
import { getForbiddenJsonLd } from '../../../utils/jsonld';
import Text from '../../FlexibleCard/components/elements/common/base-text-element';
import { ForbiddenAction } from '../actions/ForbiddenAction';
import UnresolvedView from './unresolved-view';
import { withFlexibleUIBlockCardStyle } from './utils/withFlexibleUIBlockCardStyle';
const textStyles = null;

/**
 * This view represent a Block Card with the 'Forbidden' status.
 * It should have a "Try another account" button that will allow a user to connect another account and view the block card.
 *
 * @see SmartLinkStatus
 * @see FlexibleCardProps
 */
const ForbiddenView = ({
  testId = 'smart-block-forbidden-view',
  ...props
}) => {
  var _details$meta;
  const {
    createAnalyticsEvent
  } = useAnalyticsEvents();
  const intl = useIntl();
  const {
    cardState,
    onAuthorize,
    url
  } = props;
  const details = cardState === null || cardState === void 0 ? void 0 : cardState.details;
  const cardMetadata = (_details$meta = details === null || details === void 0 ? void 0 : details.meta) !== null && _details$meta !== void 0 ? _details$meta : getForbiddenJsonLd().meta;
  const provider = extractSmartLinkProvider(details);
  const providerName = (provider === null || provider === void 0 ? void 0 : provider.text) || '';
  const messageContext = useMemo(() => {
    const hostname = /*#__PURE__*/React.createElement("b", null, extractHostname(url));
    return {
      product: providerName,
      hostname
    };
  }, [providerName, url]);
  const requestAccessContext = useMemo(() => {
    return extractRequestAccessContextImproved({
      jsonLd: cardMetadata,
      url,
      product: providerName,
      createAnalyticsEvent
    });
  }, [cardMetadata, providerName, url, createAnalyticsEvent]);
  const title = useMemo(() => {
    const descriptor = toMessage(messages.invalid_permissions, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.titleMessageKey);
    return intl.formatMessage(descriptor, {
      product: providerName
    });
  }, [intl, providerName, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.titleMessageKey]);
  const actions = useMemo(() => {
    let actionFromAccessContext = [];
    const tryAnotherAccountAction = onAuthorize ? [ForbiddenAction(onAuthorize, 'try_another_account')] : [];
    if (requestAccessContext) {
      const {
        action,
        callToActionMessageKey
      } = requestAccessContext;
      actionFromAccessContext = action && callToActionMessageKey ? [ForbiddenAction(action.promise, callToActionMessageKey, messageContext, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.buttonDisabled)] : [];
    }
    return [...tryAnotherAccountAction, ...actionFromAccessContext];
  }, [onAuthorize, requestAccessContext, messageContext]);
  return /*#__PURE__*/React.createElement(UnresolvedView, _extends({}, props, {
    actions: actions,
    showPreview: true,
    testId: testId,
    title: title
  }), /*#__PURE__*/React.createElement(LockLockedIcon, {
    label: "forbidden-lock-icon",
    color: "var(--ds-icon-danger, #C9372C)",
    testId: `${testId}-lock-icon`
  }), /*#__PURE__*/React.createElement(Text, {
    maxLines: 3,
    message: {
      descriptor: toMessage(messages.invalid_permissions_description, requestAccessContext === null || requestAccessContext === void 0 ? void 0 : requestAccessContext.descriptiveMessageKey),
      values: messageContext
    },
    className: ax(["_syazi7uo"])
  }));
};
export default withFlexibleUIBlockCardStyle(ForbiddenView);