const _excluded = ["isLoading"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
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, { useState } from "react";
import PropTypes from "prop-types";
import { useSelector } from "react-redux";
import { makeStyles } from "@mui/styles";
import CanvasSaveButton from "react-spatial/components/CanvasSaveButton";
import { ScaleLine } from "ol/control";
import html2canvas from "html2canvas";
import useTranslation from "../../utils/useTranslation";
import Button from "../Button";
import { ReactComponent as Loader } from "./loader.svg";
import { getMapHd, generateExtraData, exportPdf, getStyledPdfScaleLine, sizesByFormat } from "../../utils/exportUtils";
import LayerService from "../../utils/LayerService";
import { trackEvent } from "../../utils/trackingUtils";
const useStyles = makeStyles(theme => ({
  buttonContent: _objectSpread(_objectSpread({}, theme.styles.flexCenter), {}, {
    padding: "5px 10px",
    height: 35,
    width: 110,
    minWidth: 110,
    backgroundColor: "#dcdcdc",
    "&:hover": {
      backgroundColor: "#cdcdcd"
    },
    "&:disabled": {
      opacity: 0.9
    }
  })
}));
function ExportButton(_ref) {
  let {
    exportFormat = "a0",
    exportScale = 1,
    // High res
    exportSize = [3370, 2384],
    // a0
    exportCoordinates = null,
    exportZoom = null,
    // 10,
    exportExtent = [620000, 5741000, 1200000, 6058000],
    exportCopyright = true,
    children = /*#__PURE__*/React.createElement(DefaultChildren, null),
    style = {},
    id = null,
    scaleLineConfig = null,
    trackingEventOptions = {}
  } = _ref;
  const map = useSelector(state => state.app.map);
  const activeTopic = useSelector(state => state.app.activeTopic);
  const layers = useSelector(state => state.map.layers);
  const exportPrintOptions = useSelector(state => state.app.exportPrintOptions);
  const {
    t,
    i18n
  } = useTranslation();
  const [isLoading, setLoading] = useState(false);
  const {
    exportConfig
  } = activeTopic || {};
  const renderChildren = () => {
    const styles = _objectSpread({
      pointerEvents: isLoading ? "none" : "auto",
      opacity: isLoading ? 0.5 : 1
    }, style);
    return isLoading ? /*#__PURE__*/React.cloneElement(children, {
      isLoading,
      style: styles,
      disabled: true
    }) : React.Children.map(children, child => {
      return /*#__PURE__*/React.cloneElement(child, {
        style: styles,
        id
      });
    });
  };
  return /*#__PURE__*/React.createElement(CanvasSaveButton, {
    extraData: exportCopyright ? generateExtraData(layers) : null,
    autoDownload: false,
    format: "image/jpeg",
    onSaveStart: () => {
      const {
        getExportFileName
      } = exportConfig || {};
      if (trackingEventOptions) {
        trackEvent(_objectSpread({
          eventType: "download",
          componentName: "secondary button",
          label: t("PDF exportieren"),
          location: t(activeTopic === null || activeTopic === void 0 ? void 0 : activeTopic.name, {
            lng: "de"
          }),
          variant: "PDF export",
          value: (getExportFileName === null || getExportFileName === void 0 ? void 0 : getExportFileName(t, exportFormat, i18n.language)) || "trafimage-".concat(new Date().toISOString().slice(0, 10), ".pdf")
        }, trackingEventOptions), activeTopic);
      }
      setLoading(true);
      return getMapHd(map, new LayerService(layers), exportScale, exportCoordinates, exportSize, exportZoom, exportExtent);
    },
    onSaveEnd: async (mapToExport, canvas) => {
      if (!mapToExport) {
        setLoading(false);
        return;
      }
      let templateValues;
      let imageUrl;
      let fileName;
      if (exportConfig) {
        const {
          getTemplateValues,
          getOverlayImageUrl,
          getExportFileName
        } = exportConfig;
        templateValues = getTemplateValues === null || getTemplateValues === void 0 ? void 0 : getTemplateValues(i18n.language, t);
        imageUrl = getOverlayImageUrl === null || getOverlayImageUrl === void 0 ? void 0 : getOverlayImageUrl(i18n.language, exportFormat);
        fileName = getExportFileName === null || getExportFileName === void 0 ? void 0 : getExportFileName(t, exportFormat, i18n.language);
      }
      const scaleLineControl = mapToExport.getControls().getArray().find(c => c instanceof ScaleLine);
      const scaleLineElement = scaleLineControl && getStyledPdfScaleLine(scaleLineControl, exportPrintOptions === null || exportPrintOptions === void 0 ? void 0 : exportPrintOptions.quality);
      const scaleLineCanvas = scaleLineElement && (await html2canvas(scaleLineElement));
      await exportPdf(mapToExport, map, layers, exportFormat, canvas, exportScale, sizesByFormat[exportFormat], templateValues, imageUrl, fileName, scaleLineConfig && scaleLineCanvas && _objectSpread(_objectSpread({}, scaleLineConfig), {}, {
        canvas: scaleLineCanvas
      }));
      setLoading(false);
    }
  }, renderChildren());
}
function DefaultChildren(_ref2) {
  let {
      isLoading = false
    } = _ref2,
    props = _objectWithoutProperties(_ref2, _excluded);
  const classes = useStyles();
  const {
    t
  } = useTranslation();
  return /*#__PURE__*/React.createElement(Button, _extends({}, props, {
    className: classes.buttonContent,
    disabled: isLoading
  }), isLoading && /*#__PURE__*/React.createElement(Loader, null), t(isLoading ? "Export läuft..." : "PDF exportieren"));
}
DefaultChildren.propTypes = {
  isLoading: PropTypes.bool
};
DefaultChildren.propTypes = {
  style: PropTypes.object,
  id: PropTypes.string,
  onClick: PropTypes.func
};
ExportButton.propTypes = {
  exportFormat: PropTypes.string,
  exportExtent: PropTypes.arrayOf(PropTypes.number),
  exportScale: PropTypes.number,
  exportZoom: PropTypes.number,
  exportCoordinates: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)),
  exportSize: PropTypes.arrayOf(PropTypes.number),
  exportCopyright: PropTypes.bool,
  children: PropTypes.node,
  style: PropTypes.object,
  id: PropTypes.string,
  scaleLineConfig: PropTypes.shape({
    x: PropTypes.number,
    y: PropTypes.number
  }),
  trackingEventOptions: PropTypes.shape({
    category: PropTypes.string,
    action: PropTypes.string,
    label: PropTypes.string,
    variant: PropTypes.string,
    value: PropTypes.string,
    eventType: PropTypes.string,
    location: PropTypes.string,
    componentName: PropTypes.string
  })
};
export default /*#__PURE__*/React.memo(ExportButton);
//# sourceMappingURL=ExportButton.js.map