/* 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-next';
import ImageLoader from 'react-render-image';
import { LoadingSkeleton } from '../loading-skeleton';
const styles = {
  roundImg: "_2rko1rr0"
};
const messages = defineMessages({
  imageAltText: {
    id: 'smart-link.image-icon.altText',
    defaultMessage: 'Link Icon'
  }
});
const ImageIcon = ({
  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) {
    return /*#__PURE__*/React.createElement("img", {
      src: url,
      "data-testid": `${testId}-image`,
      alt: "",
      style: {
        width,
        height
      },
      onError: () => setHasImageErrored(true),
      className: ax([appearance === 'round' && styles.roundImg])
    });
  } else {
    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: formatMessage(messages.imageAltText),
        style: {
          width,
          height
        },
        className: ax([appearance === 'round' && styles.roundImg])
      }),
      errored: defaultIcon,
      onError: onError,
      onLoad: onLoad
    });
  }
};
export default ImageIcon;