/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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 { fg } from '@atlaskit/platform-feature-flags';
import { LoadingSkeleton } from '../loading-skeleton';
var styles = {
  roundImg: "_2rko1rr0"
};
var messages = defineMessages({
  imageAltText: {
    id: 'smart-link.image-icon.altText',
    defaultMessage: 'Link Icon',
    description: 'Alt text for a link icon.'
  }
});
var ImageIcon = function ImageIcon(_ref) {
  var label = _ref.label,
    defaultIcon = _ref.defaultIcon,
    testId = _ref.testId,
    url = _ref.url,
    width = _ref.width,
    height = _ref.height,
    _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'square' : _ref$appearance,
    onError = _ref.onError,
    onLoad = _ref.onLoad,
    _ref$hideLoadingSkele = _ref.hideLoadingSkeleton,
    hideLoadingSkeleton = _ref$hideLoadingSkele === void 0 ? false : _ref$hideLoadingSkele;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    hasImageErrored = _useState2[0],
    setHasImageErrored = _useState2[1];
  var _useIntl = useIntl(),
    formatMessage = _useIntl.formatMessage;

  // If url changes, reset state
  useEffect(function () {
    setHasImageErrored(false);
  }, [url]);
  if (hasImageErrored) {
    return defaultIcon;
  }
  if (hideLoadingSkeleton) {
    var _label$trim;
    return /*#__PURE__*/React.createElement("img", {
      src: url,
      "data-testid": "".concat(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: width,
        height: height
      },
      onError: function onError() {
        return 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: "".concat(testId, "-loading"),
        width: width,
        height: height
      }),
      loaded: /*#__PURE__*/React.createElement("img", {
        src: url,
        "data-testid": "".concat(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: width,
          height: height
        },
        className: ax([appearance === 'round' && styles.roundImg])
      }),
      errored: defaultIcon,
      onError: onError,
      onLoad: onLoad
    });
  }
};
export default ImageIcon;