import React, { useEffect, useState, useCallback } from "react";
import { useSelector } from "react-redux";
import { BsDownload } from "react-icons/bs";
import { unByKey } from "ol/Observable";
import useTranslation from "../../utils/useTranslation";
import MapButton from "../../components/MapButton";
import GaExportMenu from "./GaExportMenu";
import useHasScreenSize from "../../utils/useHasScreenSize";
const useVisibleLayer = layers => {
  const [visible, setVisible] = useState(false);
  const getVisible = useCallback(() => {
    setVisible(layers.some(l => l.get("visible")));
  }, [layers]);
  useEffect(() => {
    getVisible();
    const listeners = layers.map(l => l.on("change:visible", getVisible));
    return () => unByKey(listeners);
  }, [layers, getVisible]);
  return visible;
};
function GaExportMapButton() {
  const isMobile = useHasScreenSize();
  const {
    t
  } = useTranslation();
  const [showMenu, setShowMenu] = useState(false);
  const layers = useSelector(state => state.map.layers);
  const exportableLayers = layers.filter(layer => layer.get("isExportable"));
  const showButton = useVisibleLayer(exportableLayers);
  if (isMobile || !showButton) {
    return null;
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(MapButton, {
    onClick: () => setShowMenu(!showMenu),
    style: {
      padding: 10,
      color: "#444"
    },
    title: t("Karte als PDF exportieren")
  }, /*#__PURE__*/React.createElement(BsDownload, null)), showMenu && /*#__PURE__*/React.createElement(GaExportMenu, {
    onClose: () => setShowMenu(false)
  }));
}
export default GaExportMapButton;
//# sourceMappingURL=GaExportMapButton.js.map