/* 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 { Box, Pressable } from '@atlaskit/primitives/compiled';
import { InternalActionName, SmartLinkStatus } from '../../../../../constants';
import { useFlexibleCardContext } from '../../../../../state/flexible-ui-context';
const styles = {
  button: "_1e0c1bgi",
  message: "_1rjcze3t _18zrze3t"
};
const validErrorStatuses = [SmartLinkStatus.Forbidden, SmartLinkStatus.Unauthorized];
const validateErrorStatus = status => {
  return status !== undefined && validErrorStatuses.includes(status);
};
const getMessageDescriptor = (status, accessType, props) => {
  var _props$status;
  if (!validateErrorStatus(status)) {
    return undefined;
  }
  const message = props === null || props === void 0 ? void 0 : (_props$status = props[status]) === null || _props$status === void 0 ? void 0 : _props$status[accessType !== null && accessType !== void 0 ? accessType : 'FALLBACK'];
  return message;
};
const CustomUnresolvedAction = ({
  testId = 'custom-unresolved-action',
  onlyShowIfAction = false,
  Container,
  ...props
}) => {
  var _context$data, _context$data$actions, _context$data2, _context$data2$meta, _getMessageDescriptor;
  const context = useFlexibleCardContext();
  const data = context === null || context === void 0 ? void 0 : (_context$data = context.data) === null || _context$data === void 0 ? void 0 : (_context$data$actions = _context$data.actions) === null || _context$data$actions === void 0 ? void 0 : _context$data$actions[InternalActionName.UnresolvedAction];
  if (!data) {
    return null;
  }
  const {
    descriptor,
    onClick,
    values
  } = data;
  const {
    status
  } = context;
  const accessType = context === null || context === void 0 ? void 0 : (_context$data2 = context.data) === null || _context$data2 === void 0 ? void 0 : (_context$data2$meta = _context$data2.meta) === null || _context$data2$meta === void 0 ? void 0 : _context$data2$meta.accessType;
  const hasAction = onClick !== undefined;
  if (onlyShowIfAction && !hasAction) {
    return null;
  }
  const messageDescriptor = (_getMessageDescriptor = getMessageDescriptor(status, accessType, props)) !== null && _getMessageDescriptor !== void 0 ? _getMessageDescriptor : descriptor;
  if (!messageDescriptor) {
    return null;
  }
  const message = /*#__PURE__*/React.createElement(Box, {
    testId: `${testId}-errored-view-message`,
    tabIndex: hasAction ? 0 : undefined,
    xcss: styles.message
  }, /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, messageDescriptor, {
    values: values
  })));
  const component = hasAction ? /*#__PURE__*/React.createElement(Pressable, {
    onClick: data.onClick,
    style: {
      color: `inherit`,
      font: `inherit`,
      textAlign: `inherit`
    },
    xcss: styles.button
  }, message) : message;
  return Container ? /*#__PURE__*/React.createElement(Container, null, component) : component;
};
export default CustomUnresolvedAction;