"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var DEV = false;

try {
  DEV = process.env.NODE_ENV !== "production";
} catch (e) {
  DEV = false;
}

function warn(msg) {
  if (DEV) {
    console.warn("@crystallize/react-image: ".concat(msg));
  }
}

var getVariantSrc = function getVariantSrc(variant) {
  return "".concat(variant.url, " ").concat(variant.width, "w");
};

var ReactImage = function ReactImage(_ref) {
  var childRenderFunc = _ref.children,
      restOfAllProps = _objectWithoutProperties(_ref, ["children"]);

  // Regular image
  if (restOfAllProps.src) {
    if (childRenderFunc) {
      return childRenderFunc(restOfAllProps);
    }

    return _react["default"].createElement("img", restOfAllProps);
  } // Continue using data from Crystallize


  var url = restOfAllProps.url,
      sizes = restOfAllProps.sizes,
      variants = restOfAllProps.variants,
      alt = restOfAllProps.altText,
      rest = _objectWithoutProperties(restOfAllProps, ["url", "sizes", "variants", "altText"]);

  var hasVariants = variants.length > 0; // Determine srcSet

  var std = variants.filter(function (v) {
    return !v.url.endsWith(".webp");
  });
  var webp = variants.filter(function (v) {
    return v.url.endsWith(".webp");
  });
  var srcSet = std.map(getVariantSrc).join(", ");
  var srcSetWebp = webp.map(getVariantSrc).join(", "); // Ensure fallback src for older browsers

  var src = url || (hasVariants ? std[0].url : undefined);

  if (childRenderFunc) {
    return childRenderFunc(_objectSpread({
      src: src,
      srcSet: srcSet,
      srcSetWebp: srcSetWebp,
      sizes: sizes,
      alt: alt
    }, rest));
  }

  if (hasVariants && !sizes) {
    warn("You have provided variants, but not sizes. This has a negative impact on performance. Check out https://crystallize.com/blog/react-image-sizes-attribute-for-fast-ecommerce");
  }

  return _react["default"].createElement("picture", null, srcSetWebp.length > 0 && _react["default"].createElement("source", {
    srcSet: srcSetWebp,
    type: "image/webp",
    sizes: sizes
  }), srcSet.length > 0 && _react["default"].createElement("source", {
    srcSet: srcSet,
    type: "image/jpg",
    sizes: sizes
  }), _react["default"].createElement("img", _extends({
    src: src,
    sizes: sizes,
    alt: alt
  }, rest)));
};

ReactImage.propTypes = {
  children: _propTypes["default"].func,
  src: _propTypes["default"].string,
  url: _propTypes["default"].string,
  variants: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    url: _propTypes["default"].string,
    width: _propTypes["default"].number
  })),
  sizes: _propTypes["default"].string
};
var _default = ReactImage;
exports["default"] = _default;

//# sourceMappingURL=index.js.map