import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";

var _templateObject, _templateObject2, _templateObject3;

import React from 'react'; // eslint-disable-line

import styled from 'styled-components';
import { useInView } from 'react-intersection-observer';
var transparentPixel = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJggg==';
export var LazyImage = (_ref) => {
  var {
    width = 300,
    height = 400,
    onClick,
    src: source,
    srcLarge: sourceLarge,
    background,
    alt,
    backgroundOverImage
  } = _ref;
  var [ref, inView] = useInView({
    triggerOnce: true
  });
  var ratio = height && width ? height / width : 1;
  var src = process.browser ? source : transparentPixel;
  var srcLarge = process.browser ? sourceLarge || src : transparentPixel;

  var handleClick = () => {
    if (onClick && typeof onClick === 'function') {
      return onClick();
    }
  };

  return /*#__PURE__*/React.createElement(ImageWrapper, {
    ref: ref,
    ratio: ratio,
    background: background,
    onClick: handleClick,
    backgroundOverImage: backgroundOverImage
  }, /*#__PURE__*/React.createElement(CenterImage, null, inView && /*#__PURE__*/React.createElement(Image, {
    src: src,
    srcSet: srcLarge && src + " 1x, " + srcLarge + " 2x",
    alt: alt,
    width: width,
    loading: "lazy"
  })));
};
// TODO: review the background color after demo
var ImageWrapper = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  position: relative;\n\n  padding-bottom: ", "%;\n  width: 100%;\n  max-height: 100%;\n  cursor: pointer;\n\n  ", "\n"])), props => props.ratio * 100, props => !props.backgroundOverImage ? "background-color: " + (props.background ? props.background : props.theme.palette.disabled) + ";" : "\n      &:after {\n        content: '';\n        width: 100%;\n        height: 100%;\n        position: absolute;\n        top: 0;\n        left: 0;\n        background-color: " + (props.background ? props.background : props.theme.palette.primary) + ";\n        opacity: 0.072;\n        pointer-events: none;\n      }\n  ");
var Image = styled.img(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n  width: auto;\n  height: auto;\n  max-width: 100%;\n  max-height: 100%;\n"])));
var CenterImage = styled.div(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["\n  position: absolute;\n  top: 0;\n  left: 0;\n  height: 100%;\n  width: 100%;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n"])));
//# sourceMappingURL=index.js.map