"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = require("react");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _core = require("@emotion/core");

var _List = _interopRequireDefault(require("@material-ui/core/List"));

var _ListItem = _interopRequireDefault(require("@material-ui/core/ListItem"));

var _ListItemText = _interopRequireDefault(require("@material-ui/core/ListItemText"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _Collapse = _interopRequireDefault(require("@material-ui/core/Collapse"));

var _ExpandLess = _interopRequireDefault(require("@material-ui/icons/ExpandLess"));

var _ExpandMore = _interopRequireDefault(require("@material-ui/icons/ExpandMore"));

var _styles = require("@material-ui/styles");

var _Book = _interopRequireDefault(require("@material-ui/icons/Book"));

var _lodash = require("lodash");

var _shortid = require("shortid");

var _uiThemes = require("@hackoregon/ui-themes");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

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 = (0, _react.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 && (0, _lodash.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 (0, _core.jsx)(_styles.ThemeProvider, {
    theme: _uiThemes.MaterialTheme
  }, (0, _core.jsx)(_List["default"], {
    component: "nav",
    "aria-label": "data collection list"
  }, sortedLocations && sortedLocations.map(function (location) {
    return (0, _core.jsx)(_react.Fragment, null, (0, _core.jsx)(_ListItem["default"], {
      key: (0, _shortid.generate)(),
      button: true,
      onClick: function onClick() {
        return dispatch({
          type: "toggle",
          location: location
        });
      }
    }, (0, _core.jsx)(_ListItemText["default"], {
      disableTypography: true,
      primary: (0, _core.jsx)(_Typography["default"], {
        style: {
          fontWeight: "600"
        }
      }, location)
    }), !collapsedItems[location] ? (0, _core.jsx)(_ExpandLess["default"], null) : (0, _core.jsx)(_ExpandMore["default"], null)), (0, _core.jsx)(_Collapse["default"], {
      "in": !collapsedItems[location],
      timeout: "auto",
      unmountOnExit: true
    }, (0, _core.jsx)(_List["default"], {
      component: "div",
      disablePadding: true
    }, packagesWithLocations && packagesWithLocations.filter(function (pkg) {
      return pkg.location === location;
    }).map(function (_ref2) {
      var displayName = _ref2.displayName,
          name = _ref2.name;
      return (0, _core.jsx)(_ListItem["default"], {
        key: (0, _shortid.generate)(),
        button: true,
        onClick: function onClick() {
          return onChange(displayName);
        },
        css: (0, _core.css)("padding-left: ".concat(_uiThemes.MaterialTheme.spacing(3), "px !important;"))
      }, (0, _core.jsx)(_Book["default"], {
        color: "secondary",
        fontSize: "small"
      }), (0, _core.jsx)(_ListItemText["default"], {
        disableTypography: true,
        primary: (0, _core.jsx)(_Typography["default"], {
          style: {
            paddingLeft: "4px"
          }
        }, name)
      }));
    }))));
  })));
};

PackageSelector.displayName = "PackageSelector";
PackageSelector.propTypes = {
  packages: _propTypes["default"].shape({
    displayName: _propTypes["default"].string,
    layers: _propTypes["default"].arrayOf(_propTypes["default"].shape)
  }),
  onChange: _propTypes["default"].func
};
var _default = PackageSelector;
exports["default"] = _default;