/* ImageIcon.tsx generated by @compiled/babel-plugin v0.39.1 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ImageIcon = void 0;
require("./ImageIcon.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _reactRenderImage = _interopRequireDefault(require("react-render-image"));
var _styled = require("./styled");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var styles = {
  roundedImage: "_2rko1rr0"
};
var ImageIcon = exports.ImageIcon = function ImageIcon(_ref) {
  var _ref$alt = _ref.alt,
    alt = _ref$alt === void 0 ? '' : _ref$alt,
    src = _ref.src,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 16 : _ref$size,
    title = _ref.title,
    defaultIcon = _ref.default,
    _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'square' : _ref$appearance,
    hideLoadingSkeleton = _ref.hideLoadingSkeleton;
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    hasImageErrored = _useState2[0],
    setHasImageErrored = _useState2[1];

  // If url changes, reset state
  (0, _react.useEffect)(function () {
    setHasImageErrored(false);
  }, [src]);
  var handleError = (0, _react.useCallback)(function () {
    setHasImageErrored(true);
  }, []);

  // TODO: do we need this?
  if (!src) {
    return defaultIcon || null;
  }
  if (hasImageErrored) {
    return defaultIcon;
  }
  var LoadedImageComponent = /*#__PURE__*/_react.default.createElement(_styled.Image
  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
  , (0, _extends2.default)({
    className: (0, _runtime.ax)([appearance === 'round' && styles.roundedImage, "smart-link-icon"]),
    src: src,
    alt: alt,
    size: size,
    title: title
  }, hideLoadingSkeleton ? {
    onError: handleError
  } : undefined));
  if (hideLoadingSkeleton) {
    return LoadedImageComponent;
  } else {
    return /*#__PURE__*/_react.default.createElement(_reactRenderImage.default, {
      src: src,
      loading: defaultIcon,
      loaded: LoadedImageComponent,
      errored: defaultIcon
    });
  }
};