const _excluded = ["url", "title", "children", "className", "trackMatomoEventAction"];
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; }
/* eslint-disable react/jsx-props-no-spreading */
import React from "react";
import PropTypes from "prop-types";
import { useMatomo } from "@jonkoops/matomo-tracker-react";
import { useSelector } from "react-redux";
import { TiImage, TiSocialFacebook } from "react-icons/ti";
import { RiTwitterXLine } from "react-icons/ri";
import { FaEnvelope } from "react-icons/fa";
import CanvasSaveButton from "react-spatial/components/CanvasSaveButton";
import { IconButton } from "@mui/material";
import useTranslation from "../../utils/useTranslation";
import SharePermalinkButton from "../SharePermalinkButton";
import { TRACK_SHARE_DL_ACTION, TRACK_SHARE_FB_ACTION, TRACK_SHARE_MAIL_ACTION, TRACK_SHARE_PERMALINK_ACTION, TRACK_SHARE_TW_ACTION } from "../../utils/constants";
import { generateExtraData } from "../../utils/exportUtils";
import { trackEvent } from "../../utils/trackingUtils";
const socialShareConfig = [{
  url: "mailto:?body={url}",
  title: "Per Email versenden",
  children: /*#__PURE__*/React.createElement(FaEnvelope, {
    focusable: false,
    style: {
      fontSize: 23
    }
  }),
  className: "ta-mail-icon",
  trackMatomoEventAction: TRACK_SHARE_MAIL_ACTION
}, {
  url: "//www.facebook.com/sharer.php?u={url}",
  title: "Auf Facebook teilen",
  children: /*#__PURE__*/React.createElement(TiSocialFacebook, {
    focusable: false,
    style: {
      fontSize: 28
    }
  }),
  className: "ta-facebook-icon",
  trackMatomoEventAction: TRACK_SHARE_FB_ACTION
}, {
  url: "//twitter.com/intent/tweet?url={url}",
  title: "Auf X teilen",
  children: /*#__PURE__*/React.createElement(RiTwitterXLine, {
    focusable: false,
    style: {
      fontSize: 22
    }
  }),
  className: "ta-x-icon",
  trackMatomoEventAction: TRACK_SHARE_TW_ACTION
}];
const replaceParams = (url, language, appBaseUrl) => {
  return url.replace("{url}", encodeURIComponent(window.location.href)).replace("{language}", language).replace("{appBaseUrl}", appBaseUrl);
};
function handleTracking(topic, matomoCategory, matomoTrackFunc, title, t) {
  let action = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "action";
  let variant = arguments.length > 6 ? arguments[6] : undefined;
  let value = arguments.length > 7 ? arguments[7] : undefined;
  matomoTrackFunc({
    category: topic === null || topic === void 0 ? void 0 : topic.key,
    action: matomoCategory
  });
  trackEvent({
    eventType: action,
    componentName: "share button",
    label: t(title),
    location: t(topic === null || topic === void 0 ? void 0 : topic.name, {
      lng: "de"
    }),
    variant: variant || t(title, {
      lng: "de"
    }),
    value
  }, topic);
}
function ShareLink(_ref) {
  let {
      url,
      title,
      children,
      className,
      trackMatomoEventAction
    } = _ref,
    props = _objectWithoutProperties(_ref, _excluded);
  const appBaseUrl = useSelector(state => state.app.appBaseUrl);
  const activeTopic = useSelector(state => state.app.activeTopic);
  const language = useSelector(state => state.app.language);
  const {
    t
  } = useTranslation();
  const {
    trackEvent: trackMatomoEvent
  } = useMatomo();
  return /*#__PURE__*/React.createElement(IconButton, _extends({
    className: className,
    href: replaceParams(url, language, appBaseUrl),
    title: t(title),
    target: "_blank",
    tabIndex: 0,
    onClick: () => {
      handleTracking(activeTopic, trackMatomoEventAction, trackMatomoEvent, title, t);
    },
    "data-testid": "wkp-share-".concat(title.replace(/\s+/g, "-").toLowerCase())
  }, props), children);
}
ShareLink.propTypes = {
  className: PropTypes.string,
  url: PropTypes.string.isRequired,
  title: PropTypes.string.isRequired,
  children: PropTypes.node.isRequired,
  trackMatomoEventAction: PropTypes.string
};
function Share() {
  const map = useSelector(state => state.app.map);
  const layers = useSelector(state => state.map.layers);
  const activeTopic = useSelector(state => state.app.activeTopic);
  const {
    t
  } = useTranslation();
  const {
    trackEvent: trackMatomoEvent
  } = useMatomo();
  return /*#__PURE__*/React.createElement("div", {
    className: "wkp-share"
  }, /*#__PURE__*/React.createElement(SharePermalinkButton, {
    buttonProps: {
      onClick: () => {
        handleTracking(activeTopic, TRACK_SHARE_PERMALINK_ACTION, trackMatomoEvent, "Permalink generieren", t);
      }
    }
  }), /*#__PURE__*/React.createElement(ShareLink, socialShareConfig[0]), /*#__PURE__*/React.createElement(CanvasSaveButton, {
    map: map,
    extraData: generateExtraData(layers),
    onSaveStart: mapp => {
      handleTracking(activeTopic, TRACK_SHARE_DL_ACTION, trackMatomoEvent, "Karte als Bild speichern", t, "download", "PNG export", "".concat(window.document.title.replace(/ /g, "_").toLowerCase(), ".png"));
      return Promise.resolve(mapp);
    }
  }, /*#__PURE__*/React.createElement(IconButton, {
    className: "rs-canvas-save-button",
    title: t("Karte als Bild speichern"),
    size: "large"
  }, /*#__PURE__*/React.createElement(TiImage, {
    focusable: false
  }))), /*#__PURE__*/React.createElement(ShareLink, socialShareConfig[1]), /*#__PURE__*/React.createElement(ShareLink, socialShareConfig[2]));
}
export default /*#__PURE__*/React.memo(Share);
//# sourceMappingURL=Share.js.map