"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Image = exports.useHasImageLoaded = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _Box = require("../Box");

var _Lightbox = require("../Lightbox");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var useHasImageLoaded = function useHasImageLoaded(_ref) {
  var src = _ref.src,
      _ref$onLoad = _ref.onLoad,
      onLoad = _ref$onLoad === void 0 ? null : _ref$onLoad,
      _ref$onError = _ref.onError,
      onError = _ref$onError === void 0 ? null : _ref$onError;
  var isMounted = (0, _react.useRef)(true);

  var _useState = (0, _react.useState)(false),
      hasLoaded = _useState[0],
      setHasLoaded = _useState[1];

  (0, _react.useEffect)(function () {
    if (!src) {
      return;
    }

    var image = new window.Image();
    image.src = src;

    image.onload = function (event) {
      if (isMounted.current) {
        setHasLoaded(true);

        if (onLoad) {
          onLoad(event);
        }
      }
    };

    image.onerror = function (event) {
      if (isMounted.current) {
        setHasLoaded(false);

        if (onError) {
          onError(event);
        }
      }
    };
  }, [src, onLoad, onError]);
  (0, _react.useEffect)(function () {
    return function () {
      isMounted.current = false;
    };
  }, []);
  return hasLoaded;
};

exports.useHasImageLoaded = useHasImageLoaded;
var NativeImage = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var width = _ref2.width,
      height = _ref2.height,
      alt = _ref2.alt,
      props = (0, _objectWithoutProperties2["default"])(_ref2, ["width", "height", "alt"]);
  return (// @ts-ignore
    __jsx("img", (0, _extends2["default"])({
      width: width,
      height: height,
      ref: ref,
      alt: alt
    }, props))
  );
});
var Image = /*#__PURE__*/(0, _react.forwardRef)(function (_ref3, ref) {
  var src = _ref3.src,
      fallbackSrc = _ref3.fallbackSrc,
      onError = _ref3.onError,
      onLoad = _ref3.onLoad,
      ignoreFallback = _ref3.ignoreFallback,
      withLightbox = _ref3.withLightbox,
      htmlHeight = _ref3.htmlHeight,
      htmlWidth = _ref3.htmlWidth,
      props = (0, _objectWithoutProperties2["default"])(_ref3, ["src", "fallbackSrc", "onError", "onLoad", "ignoreFallback", "withLightbox", "htmlHeight", "htmlWidth"]);
  var hasLoaded = useHasImageLoaded({
    src: src,
    onLoad: onLoad,
    onError: onError
  });
  var baseProps = {
    height: htmlHeight,
    width: htmlWidth
  };
  var imageProps;

  if (ignoreFallback) {
    imageProps = _objectSpread({
      src: src,
      onLoad: onLoad,
      onError: onError
    }, baseProps);
  } else {
    imageProps = _objectSpread({
      src: hasLoaded ? src : fallbackSrc
    }, baseProps);
  }

  if (withLightbox) {
    return __jsx(_Lightbox.LightboxMedia, {
      src: src || fallbackSrc,
      type: "image",
      cover: src || fallbackSrc
    }, __jsx(_Box.Box, (0, _extends2["default"])({
      as: NativeImage,
      ref: ref
    }, imageProps, props)));
  }

  return __jsx(_Box.Box, (0, _extends2["default"])({
    as: NativeImage,
    ref: ref
  }, imageProps, props));
});
exports.Image = Image;
//# sourceMappingURL=Image.js.map