function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useEffect, useState, useCallback } from "react";
import PropTypes from "prop-types";
import makeStyles from "@mui/styles/makeStyles";
import { ZoomIn } from "@mui/icons-material";
import useTranslation from "../../utils/useTranslation";
import useHasScreenSize from "../../utils/useHasScreenSize";
import Pagination from "../Pagination";
import Loading from "../Loading";
import { ReactComponent as Loader } from "../../img/loader.svg";
const useStylesPhoto = makeStyles(theme => ({
  wrapper: _objectSpread(_objectSpread({}, theme.styles.flexCenter), {}, {
    justifyContent: "center",
    maxWidth: "100%",
    width: "100%"
  })
}));
function Photo(_ref) {
  let {
    src = ""
  } = _ref;
  const {
    t
  } = useTranslation();
  const classes = useStylesPhoto();
  const [loading, setLoading] = useState(true);
  useEffect(() => setLoading(true), [src]);
  return /*#__PURE__*/React.createElement("div", {
    className: classes.wrapper
  }, loading && /*#__PURE__*/React.createElement(Loading, {
    icon: /*#__PURE__*/React.createElement(Loader, null),
    label: "".concat(t("Wird geladen"), "...")
  }), /*#__PURE__*/React.createElement("img", {
    src: src,
    alt: src,
    onLoad: () => setLoading(false),
    style: {
      maxWidth: "100%",
      maxHeight: "80vh",
      display: loading ? "none" : "block"
    },
    "data-testid": "carousel-photo"
  }));
}
Photo.propTypes = {
  src: PropTypes.string
};
const useStyles = makeStyles(theme => ({
  wrapper: {
    maxWidth: "100%",
    display: "flex",
    flexDirection: "column",
    alignItems: "center"
  },
  imageButton: {
    backgroundColor: "transparent",
    border: "none",
    padding: 0,
    cursor: "pointer",
    position: "relative",
    width: "100%",
    height: "fit-content",
    display: "flex"
  },
  imageOverlay: _objectSpread(_objectSpread({
    position: "absolute",
    top: 0,
    left: 0,
    width: "100%",
    height: "100%",
    backgroundColor: props => props.imageHover ? "rgba(0, 0, 0, 0.2)" : "rgba(0, 0, 0, 0)",
    zIndex: 99,
    transition: "background-color 0.3s"
  }, theme.styles.flexCenter), {}, {
    "&:hover": {
      backgroundColor: "rgba(0, 0, 0, 0.2)"
    }
  }),
  zoomInIcon: _objectSpread(_objectSpread({
    borderRadius: "50%",
    backgroundColor: "white",
    width: 40,
    height: 40
  }, theme.styles.flexCenter), {}, {
    opacity: props => props.imageHover ? 1 : 0,
    transition: "opacity 0.3s"
  }),
  photoNavigation: {
    margin: "0 auto",
    display: "flex",
    alignItems: "center"
  }
}));
function PhotoCarusel(_ref2) {
  let {
    photos,
    initialPhotoIndex = 0,
    onImageClick = null,
    onIncrement = null,
    onDecrement = null
  } = _ref2;
  const {
    t
  } = useTranslation();
  const isSmallScreen = useHasScreenSize(["xs", "s"]);
  const [imageHover, setImageHover] = useState(false);
  const classes = useStyles({
    imageHover
  });
  const [currentPhotoIndex, setCurrentPhotoIndex] = useState(initialPhotoIndex);
  const incrementPhotoIndex = useCallback(() => {
    setCurrentPhotoIndex(currentPhotoIndex + 1);
    if (onIncrement) {
      onIncrement(currentPhotoIndex + 1);
    }
  }, [currentPhotoIndex, onIncrement]);
  const decrementPhotoIndex = useCallback(() => {
    setCurrentPhotoIndex(currentPhotoIndex - 1);
    if (onDecrement) {
      onDecrement(currentPhotoIndex - 1);
    }
  }, [onDecrement, currentPhotoIndex]);
  useEffect(() => {
    setCurrentPhotoIndex(initialPhotoIndex || 0);
  }, [photos, initialPhotoIndex]);
  if (!photos || (photos === null || photos === void 0 ? void 0 : photos.length) === 0) {
    return null;
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classes.wrapper
  }, onImageClick && !isSmallScreen ?
  /*#__PURE__*/
  // eslint-disable-next-line jsx-a11y/control-has-associated-label
  React.createElement("button", {
    className: classes.imageButton,
    onClick: onImageClick,
    type: "button",
    onMouseEnter: () => setImageHover(true),
    onMouseLeave: () => setImageHover(false),
    "data-testid": "carousel-photo-button",
    title: t("Foto vergrössern"),
    tabIndex: 0
  }, /*#__PURE__*/React.createElement(Photo, {
    src: photos[currentPhotoIndex]
  }), /*#__PURE__*/React.createElement("div", {
    className: classes.imageOverlay
  }, /*#__PURE__*/React.createElement("div", {
    className: classes.zoomInIcon
  }, /*#__PURE__*/React.createElement(ZoomIn, null)))) : /*#__PURE__*/React.createElement(Photo, {
    src: photos[currentPhotoIndex]
  }), photos.length > 1 && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(Pagination, {
    onNext: incrementPhotoIndex,
    onPrevious: decrementPhotoIndex,
    index: currentPhotoIndex,
    count: photos.length
  })));
}
PhotoCarusel.propTypes = {
  photos: PropTypes.arrayOf(PropTypes.string),
  initialPhotoIndex: PropTypes.number,
  onImageClick: PropTypes.func,
  onIncrement: PropTypes.func,
  onDecrement: PropTypes.func
};
export default PhotoCarusel;
//# sourceMappingURL=PhotoCarusel.js.map