/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useEffect, useState } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import ImageLoader from 'react-render-image';
import { fg } from '@atlaskit/platform-feature-flags';
import { LoadingSkeleton } from '../loading-skeleton';
const styles = {
  roundImg: "_2rko1rr0"
};
const messages = defineMessages({
  imageAltText: {
    id: 'smart-link.image-icon.altText',
    defaultMessage: 'Link Icon',
    description: 'Alternative text for the link icon image displayed on a flexible smart link card, used by screen readers and when the image cannot be rendered.'
  }
});
const ImageIcon = ({
  label,
  defaultIcon,
  testId,
  url,
  width,
  height,
  appearance = 'square',
  onError,
  onLoad,
  hideLoadingSkeleton = false
}) => {
  const [hasImageErrored, setHasImageErrored] = useState(false);
  const {
    formatMessage
  } = useIntl();

  // If url changes, reset state
  useEffect(() => {
    setHasImageErrored(false);
  }, [url]);
  if (hasImageErrored) {
    return defaultIcon;
  }
  if (hideLoadingSkeleton) {
    var _label$trim;
    return /*#__PURE__*/React.createElement("img", {
      src: url,
      "data-testid": `${testId}-image`,
      alt: fg('platform_navx_smart_link_icon_label_a11y') ? (_label$trim = label === null || label === void 0 ? void 0 : label.trim()) !== null && _label$trim !== void 0 ? _label$trim : '' : '',
      style: {
        width,
        height
      },
      onError: () => setHasImageErrored(true),
      className: ax([appearance === 'round' && styles.roundImg])
    });
  } else {
    var _label$trim2;
    return /*#__PURE__*/React.createElement(ImageLoader, {
      src: url,
      loading: /*#__PURE__*/React.createElement(LoadingSkeleton, {
        testId: `${testId}-loading`,
        width: width,
        height: height
      }),
      loaded: /*#__PURE__*/React.createElement("img", {
        src: url,
        "data-testid": `${testId}-image`,
        alt: fg('platform_navx_smart_link_icon_label_a11y') ? (_label$trim2 = label === null || label === void 0 ? void 0 : label.trim()) !== null && _label$trim2 !== void 0 ? _label$trim2 : '' : formatMessage(messages.imageAltText),
        style: {
          width,
          height
        },
        className: ax([appearance === 'round' && styles.roundImg])
      }),
      errored: defaultIcon,
      onError: onError,
      onLoad: onLoad
    });
  }
};
export default ImageIcon;