"use strict";

exports.__esModule = true;
exports.default = exports.CategoryRow = exports.CategoryList = exports.CategoryIndicator = exports.CategoryHeader = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _recompose = require("recompose");
var _utils = require("../utils");
var _withScrollBox = _interopRequireDefault(require("../hocs/withScrollBox"));
var _useViewState3 = _interopRequireDefault(require("../hooks/useViewState"));
var _Icon = _interopRequireDefault(require("./Icon"));
var _Row = _interopRequireDefault(require("./List/Row"));
var _HeadRow = _interopRequireDefault(require("./List/HeadRow"));
var _List = require("./List/List");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
/*
	Multiple lists with a single header, aligned with each other
	No virtualization or page loading
	Categories can be folded up to hide content
	Sortable, within categories
*/
var arrayToggle = function arrayToggle(array, item) {
  return array.includes(item) ? array.filter(function (x) {
    return x !== item;
  }) : array.concat(item);
};
var CategoryRow = exports.CategoryRow = _styledComponents.default.tr.withConfig({
  displayName: "CategoryList__CategoryRow",
  componentId: "sc-rh0l6t-0"
})([""]);
var CategoryHeader = exports.CategoryHeader = _styledComponents.default.td.withConfig({
  displayName: "CategoryList__CategoryHeader",
  componentId: "sc-rh0l6t-1"
})(["border:0 solid ", ";border-top-width:1px;tr:first-child &{border-top-width:0;}tr:last-child &{border-bottom-width:1px;}position:relative;padding:11px 45px;background-color:#f1eae0;", " cursor:pointer;"], (0, _utils.getThemeProp)(["colors", "borderLight"], "#cccccc"), (0, _utils.ifFlag)("closed", (0, _styledComponents.css)(["border-bottom:1px solid ", ";"], (0, _utils.getThemeProp)(["colors", "borderLight"], "#cccccc"))));
var CategoryIndicator = exports.CategoryIndicator = (0, _styledComponents.default)(_Icon.default).attrs(function (props) {
  return {
    id: (0, _utils.ifFlag)("closed", (0, _utils.getThemeProp)(["icons", "indicators", "right"], "chevron-right"), (0, _utils.getThemeProp)(["icons", "indicators", "down"], "chevron-down"))(props)
  };
}).withConfig({
  displayName: "CategoryList__CategoryIndicator",
  componentId: "sc-rh0l6t-2"
})(["font-size:12px;position:absolute;left:22px;top:15px;"]);
var CategoryList = exports.CategoryList = function CategoryList(_ref) {
  var name = _ref.name,
    _ref$columnDefs = _ref.columnDefs,
    columnDefs = _ref$columnDefs === void 0 ? [] : _ref$columnDefs,
    _ref$rows = _ref.rows,
    rows = _ref$rows === void 0 ? [] : _ref$rows,
    rowOnClick = _ref.rowOnClick,
    placeholder = _ref.placeholder,
    _ref$keyField = _ref.keyField,
    keyField = _ref$keyField === void 0 ? ["id"] : _ref$keyField,
    _ref$categoryField = _ref.categoryField,
    categoryField = _ref$categoryField === void 0 ? ["category"] : _ref$categoryField,
    _ref$openAll = _ref.openAll,
    openAll = _ref$openAll === void 0 ? false : _ref$openAll,
    height = _ref.height,
    _ref$rowBackgroundGet = _ref.rowBackgroundGetter,
    rowBackgroundGetter = _ref$rowBackgroundGet === void 0 ? function () {} : _ref$rowBackgroundGet;
  var _useListState = (0, _List.useListState)(name, columnDefs),
    _useListState2 = _slicedToArray(_useListState, 2),
    enhancedColumnDefs = _useListState2[0],
    selection = _useListState2[1];
  var _useViewState = (0, _useViewState3.default)(name),
    _useViewState2 = _slicedToArray(_useViewState, 2),
    _useViewState2$0$clos = _useViewState2[0].closedCategories,
    closedCategories = _useViewState2$0$clos === void 0 ? [] : _useViewState2$0$clos,
    updateViewState = _useViewState2[1];
  if (columnDefs.length === 0) return null;
  var rowIds = [],
    rowCategories = {};
  rows.forEach(function (row, index) {
    var id = _utils.safeGet.apply(void 0, [row].concat(keyField)) + ""; // Ensure rowId is string
    var category = _utils.safeGet.apply(void 0, [row].concat(categoryField)) || "";
    rowIds.push(id);
    if (!rowCategories[category]) {
      rowCategories[category] = [];
    }
    rowCategories[category].push(/*#__PURE__*/_react.default.createElement(_Row.default, {
      columnDefs: enhancedColumnDefs,
      key: id,
      rowId: id,
      row: row,
      rowOnClick: rowOnClick,
      selected: selection.indexOf(id) !== -1,
      bgColor: rowBackgroundGetter(row, index)
    }));
  });
  var rowElements = [];
  if (Object.keys(rowCategories).length === 0) {
    if (placeholder) {
      rowElements.push(/*#__PURE__*/_react.default.createElement(_List.Placeholder, {
        key: "placeholder",
        width: columnDefs.length,
        height: height - _List.HEADER_HEIGHT
      }, placeholder));
    }
  } else {
    Object.entries(rowCategories).forEach(function (_ref2) {
      var _ref3 = _slicedToArray(_ref2, 2),
        key = _ref3[0],
        rows = _ref3[1];
      var clickHandler = function clickHandler() {
        return updateViewState("closedCategories", arrayToggle(closedCategories, key));
      };
      var closed = !openAll && closedCategories.includes(key);
      rowElements.push(/*#__PURE__*/_react.default.createElement(CategoryRow, {
        key: "category_" + key
      }, /*#__PURE__*/_react.default.createElement(CategoryHeader, {
        "data-test-id": "category_" + key,
        closed: closed,
        colSpan: columnDefs.length,
        onClick: clickHandler
      }, /*#__PURE__*/_react.default.createElement(CategoryIndicator, {
        closed: closed
      }), key)));
      if (!closed) {
        rowElements.push.apply(rowElements, rows);
      }
    });
  }
  return /*#__PURE__*/_react.default.createElement(_List.Table, null, /*#__PURE__*/_react.default.createElement("thead", null, /*#__PURE__*/_react.default.createElement(_HeadRow.default, {
    columnDefs: enhancedColumnDefs,
    rowIds: rowIds,
    allSelected: rows.length === selection.length && rows.length !== 0
  })), /*#__PURE__*/_react.default.createElement("tbody", null, rowElements));
};
__signature__(CategoryList, "useListState{[enhancedColumnDefs, selection]}\nuseViewState{[{ closedCategories = [] }, updateViewState]}", function () {
  return [_List.useListState, _useViewState3.default];
});
__signature__(CategoryList, "useListState{[enhancedColumnDefs, selection]}\nuseViewState{[{ closedCategories = [] }, updateViewState]}", function () {
  return [_List.useListState, _useViewState3.default];
});
var StatefulCategoryList = (0, _recompose.compose)((0, _recompose.setDisplayName)("CategoryList"), _withScrollBox.default)(CategoryList);
var _default = StatefulCategoryList;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(arrayToggle, "arrayToggle", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryRow, "CategoryRow", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryHeader, "CategoryHeader", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryIndicator, "CategoryIndicator", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryList, "CategoryList", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(StatefulCategoryList, "StatefulCategoryList", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/CategoryList.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(arrayToggle, "arrayToggle", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryRow, "CategoryRow", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryHeader, "CategoryHeader", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryIndicator, "CategoryIndicator", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(CategoryList, "CategoryList", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(StatefulCategoryList, "StatefulCategoryList", "/home/vsts/work/1/s/src/components/CategoryList.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/CategoryList.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();