function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { useReducer, Fragment } from "react";
import PropTypes from "prop-types";
import { jsx, css } from "@emotion/core";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import Typography from "@material-ui/core/Typography";
import Collapse from "@material-ui/core/Collapse";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";
import { ThemeProvider } from "@material-ui/styles";
import BookIcon from "@material-ui/icons/Book";
import { groupBy } from "lodash";
import { generate } from "shortid";
import { MaterialTheme } from "@hackoregon/ui-themes";
var MANUAL_LOCATION_SORT = ["National"];
var MANUAL_LOCATION_EXCLUDE = ["Nevada", "Georgia"];

var PackageSelector = function PackageSelector(_ref) {
  var packages = _ref.packages,
      onChange = _ref.onChange;

  var _useReducer = useReducer(function (state, action) {
    switch (action.type) {
      case "toggle":
        return _objectSpread({}, state, _defineProperty({}, action.location, !state[action.location]));

      default:
        return state;
    }
  }, {}),
      _useReducer2 = _slicedToArray(_useReducer, 2),
      collapsedItems = _useReducer2[0],
      dispatch = _useReducer2[1];

  var packagesWithLocations = packages && packages.length && packages.map(function (pkg) {
    var displayName = pkg.displayName;
    var nameAndLocation = displayName.split(":");
    var location = nameAndLocation.length === 2 ? nameAndLocation[0] : "";
    var name = nameAndLocation.length === 2 ? nameAndLocation[1] : "";
    return _objectSpread({}, pkg, {
      name: name,
      location: location
    });
  });
  var packagesByLocation = packagesWithLocations && groupBy(packagesWithLocations, function (item) {
    return item.location;
  });
  var unsortedLocations = packagesWithLocations && Object.keys(packagesByLocation).filter(function (location) {
    return !MANUAL_LOCATION_SORT.includes(location) && !MANUAL_LOCATION_EXCLUDE.includes(location);
  });
  var sortedLocations = packagesWithLocations && [].concat(MANUAL_LOCATION_SORT, _toConsumableArray(unsortedLocations));
  return jsx(ThemeProvider, {
    theme: MaterialTheme
  }, jsx(List, {
    component: "nav",
    "aria-label": "data collection list"
  }, sortedLocations && sortedLocations.map(function (location) {
    return jsx(Fragment, null, jsx(ListItem, {
      key: generate(),
      button: true,
      onClick: function onClick() {
        return dispatch({
          type: "toggle",
          location: location
        });
      }
    }, jsx(ListItemText, {
      disableTypography: true,
      primary: jsx(Typography, {
        style: {
          fontWeight: "600"
        }
      }, location)
    }), !collapsedItems[location] ? jsx(ExpandLess, null) : jsx(ExpandMore, null)), jsx(Collapse, {
      "in": !collapsedItems[location],
      timeout: "auto",
      unmountOnExit: true
    }, jsx(List, {
      component: "div",
      disablePadding: true
    }, packagesWithLocations && packagesWithLocations.filter(function (pkg) {
      return pkg.location === location;
    }).map(function (_ref2) {
      var displayName = _ref2.displayName,
          name = _ref2.name;
      return jsx(ListItem, {
        key: generate(),
        button: true,
        onClick: function onClick() {
          return onChange(displayName);
        },
        css: css("padding-left: ".concat(MaterialTheme.spacing(3), "px !important;"))
      }, jsx(BookIcon, {
        color: "secondary",
        fontSize: "small"
      }), jsx(ListItemText, {
        disableTypography: true,
        primary: jsx(Typography, {
          style: {
            paddingLeft: "4px"
          }
        }, name)
      }));
    }))));
  })));
};

PackageSelector.displayName = "PackageSelector";
PackageSelector.propTypes = {
  packages: PropTypes.shape({
    displayName: PropTypes.string,
    layers: PropTypes.arrayOf(PropTypes.shape)
  }),
  onChange: PropTypes.func
};
export default PackageSelector;