import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import Feature from "ol/Feature";
import { MenuItem } from "@mui/material";
import { makeStyles } from "@mui/styles";
import qs from "query-string";
import useTranslation from "../../utils/useTranslation";
import Select from "../../components/Select";
import Person from "./Person";
import Line from "./Line";
const useStyles = makeStyles(theme => ({
  description: {
    flex: "0 0",
    "& > div:first-child": {
      paddingBottom: theme.spacing(2)
    }
  }
}));
const roles = ["leiter_region", "leiter_kbn", "leiter_natur", "leiter_fahrweg", "mitarbeitende_kbn", "mitarbeitende_fahrweg"];

// The old WKP had different roles. These roles are used for translations.
const oldRoles = ["Leiter Region", "Leiter KBN", "Leiter Natur", "Leiter FW TEC", "Mitarbeitende KBN", "Mitarbeitende FW TEC"];
const PERMALINK_PARAM = "regionRole";
const getRoleFromPermalinkParam = param => {
  const idx = oldRoles.indexOf(param);
  return idx > -1 ? roles[idx] : null;
};
const getPermalinkParamFromRole = role => {
  const idx = roles.indexOf(role);
  return idx > -1 ? oldRoles[idx] : null;
};
function Region(_ref) {
  let {
    feature
  } = _ref;
  const {
    t
  } = useTranslation();
  const classes = useStyles();
  const parsed = qs.parseUrl(window.location.href);
  const orderedRoles = [...roles].sort((a, b) => t(oldRoles[roles.indexOf(a)]) < t(oldRoles[roles.indexOf(b)]) ? -1 : 1);
  const [role, setRole] = useState(getRoleFromPermalinkParam(parsed.query[PERMALINK_PARAM]) || orderedRoles[0]);
  const str = feature.get(role);
  const person = str && JSON.parse(str) || {};
  useEffect(() => {
    if (role !== parsed.query[PERMALINK_PARAM]) {
      parsed.query[PERMALINK_PARAM] = getPermalinkParamFromRole(role);
      window.history.replaceState(undefined, undefined, "?".concat(qs.stringify(parsed.query)));
    }
  }, [parsed, role]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Line, {
    feature: feature
  }), /*#__PURE__*/React.createElement("div", {
    className: classes.description
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Select, {
    value: role,
    fullWidth: true,
    onChange: evt => setRole(evt.target.value)
  }, orderedRoles.map(value => {
    return /*#__PURE__*/React.createElement(MenuItem, {
      key: value,
      value: value
    }, t(oldRoles[roles.indexOf(value)]));
  })))), /*#__PURE__*/React.createElement("div", null, (Array.isArray(person) ? person : [person]).sort((_ref2, _ref3) => {
    let {
      unterrolle: unterrolleA,
      kommentar: kommentarA,
      name: nameA
    } = _ref2;
    let {
      unterrolle: unterrolleB,
      kommentar: kommentarB,
      name: nameB
    } = _ref3;
    // First, persons with unterolle, then persons without.
    if (unterrolleA || unterrolleB) {
      if (!unterrolleA || !unterrolleB) {
        return unterrolleA < unterrolleB ? 1 : -1;
      }
      if (kommentarA || kommentarB) {
        return "".concat(unterrolleA, " ").concat(kommentarA) < "".concat(unterrolleB, " ").concat(kommentarB) ? -1 : 1;
      }
      return unterrolleA < unterrolleB ? -1 : 1;
    }
    return nameA < nameB ? -1 : 1;
  }).map(data => /*#__PURE__*/React.createElement(Person, {
    key: JSON.stringify(data),
    person: data,
    isIntern: true
  }))));
}
Region.propTypes = {
  feature: PropTypes.instanceOf(Feature).isRequired
};
export default Region;
//# sourceMappingURL=Region.js.map