"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _react = _interopRequireWildcard(require("react"));

var _uuid = require("uuid");

var _listSelector = require("./list-selector.css-rules");

var _fieldSet = require("../field-set");

var _checkBox = require("../check-box");

var _radio = require("../radio");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Item = function Item(_ref) {
  var multiSelect = _ref.multiSelect,
      item = _ref.item,
      name = _ref.name,
      selectedItemIds = _ref.selectedItemIds,
      onChange = _ref.onChange;

  if (item.isNonOptionRow) {
    return item.label;
  }

  var isSelected = !!selectedItemIds.includes(item.id);
  return multiSelect ? /*#__PURE__*/_react["default"].createElement(_checkBox.CheckBox, {
    justify: "space-between",
    spacing: "ph2",
    direction: "right",
    name: "".concat(name, "-").concat(item.id),
    selected: isSelected,
    onToggle: function onToggle() {
      return onChange(isSelected ? selectedItemIds.filter(function (id) {
        return id !== item.id;
      }) : selectedItemIds.concat(item.id).sort());
    },
    disabled: item.disabled
  }, item.label) : /*#__PURE__*/_react["default"].createElement(_radio.Radio, {
    justify: "space-between",
    spacing: "ph2",
    direction: "right",
    value: item.id,
    name: name,
    selected: isSelected,
    onSelected: function onSelected() {
      return onChange([item.id]);
    },
    disabled: item.disabled
  }, item.label);
};

var ListSelector = function ListSelector(_ref2) {
  var name = _ref2.name,
      value = _ref2.value,
      onChange = _ref2.onChange,
      items = _ref2.items,
      label = _ref2.label,
      _ref2$hideLabel = _ref2.hideLabel,
      hideLabel = _ref2$hideLabel === void 0 ? false : _ref2$hideLabel,
      _ref2$small = _ref2.small,
      small = _ref2$small === void 0 ? false : _ref2$small,
      _ref2$multiSelect = _ref2.multiSelect,
      multiSelect = _ref2$multiSelect === void 0 ? false : _ref2$multiSelect,
      rest = _objectWithoutProperties(_ref2, ["name", "value", "onChange", "items", "label", "hideLabel", "small", "multiSelect"]);

  var defaultedName = (0, _react.useMemo)(function () {
    return name !== null && name !== void 0 ? name : "list-selector-".concat((0, _uuid.v4)());
  }, [name]);
  return /*#__PURE__*/_react["default"].createElement(_fieldSet.FieldSet, _extends({
    label: label,
    hideLabel: hideLabel
  }, rest), /*#__PURE__*/_react["default"].createElement(_listSelector.StyledFormList, {
    items: items.map(function (item) {
      return /*#__PURE__*/_react["default"].createElement(_listSelector.HoverBox, {
        key: item.id,
        isDisabled: item.disabled,
        stacked: "column",
        align: "stretch"
      }, /*#__PURE__*/_react["default"].createElement(Item, {
        name: defaultedName,
        multiSelect: multiSelect,
        small: small,
        item: item,
        selectedItemIds: value,
        onChange: onChange
      }));
    })
  }));
};

exports.ListSelector = ListSelector;
ListSelector.displayName = 'ListSelector';