"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _isNil = _interopRequireDefault(require("lodash/fp/isNil"));
var _map = _interopRequireDefault(require("lodash/fp/map"));
var _isEmpty = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _listItem = _interopRequireDefault(require("../list-item"));
var _draggableList = _interopRequireDefault(require("../../molecule/draggable-list"));
var _title = _interopRequireDefault(require("../../atom/title"));
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _selectMultiple = _interopRequireDefault(require("../../molecule/select-multiple"));
var _expandibleActionableTable = _interopRequireDefault(require("../../molecule/expandible-actionable-table"));
var _loader = _interopRequireDefault(require("../../atom/loader"));
var _inputSearch = _interopRequireDefault(require("../../atom/input-search"));
var _checkboxWithTitle = _interopRequireDefault(require("../../atom/checkbox-with-title"));
var _buttonMenuAction = _interopRequireDefault(require("../../molecule/button-menu-action"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const buildTitleView = title => {
  if (typeof title === 'string') {
    return /*#__PURE__*/_react.default.createElement(_title.default, {
      title: title,
      type: "form-group",
      titleSize: "standard-light-weight",
      "data-name": "list-title"
    });
  }
  return /*#__PURE__*/_react.default.createElement(_title.default, title);
};
const buildListItemsView = (content, ariaLabel, selectMultiple) => {
  const {
    items,
    itemType,
    onDrop,
    isDraggable = false,
    tableHeader
  } = content;
  const itemsView = isDraggable && items.length > 1 ? /*#__PURE__*/_react.default.createElement(_draggableList.default, {
    items: items.map(item => ({
      ...item,
      contentType: itemType
    })),
    itemType: "list-item",
    onDrop: onDrop
  }) : items.map((item, index) => /*#__PURE__*/_react.default.createElement("li", {
    key: item.id,
    className: _style.default.item,
    "data-name": `content-${index}`
  }, /*#__PURE__*/_react.default.createElement(_listItem.default, _extends({}, item, {
    order: null,
    contentType: itemType,
    "data-name": `list-item-${index}`
  }))));
  const tableHeaderView = !(0, _isNil.default)(tableHeader) && !(0, _isEmpty.default)(tableHeader) ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.tableHeaderWrapper
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dataColumnsWrapper
  }, tableHeader.map((tableHeaderItem, index) => /*#__PURE__*/_react.default.createElement("div", {
    key: `tableHeadeColumn-key-${index}`,
    className: _style.default.tableHeaderItem,
    "data-name": `tableHeader-${index}`,
    style: index === 0 ? {
      flexGrow: 2,
      flexShrink: 0,
      flexBasis: 0
    } : {
      flexGrow: 1,
      flexShrink: 0,
      flexBasis: 0
    }
  }, tableHeaderItem))), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.settings
  }, '')) : null;
  return /*#__PURE__*/_react.default.createElement("div", null, tableHeaderView, /*#__PURE__*/_react.default.createElement("ul", {
    className: !selectMultiple ? _style.default.list : _style.default.listWithSelectMultiple,
    "aria-label": ariaLabel,
    "data-name": 'content-list'
  }, itemsView));
};
const buildEmptyResultView = emptyResult => {
  const {
    title,
    description,
    button
  } = emptyResult;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.emptySearchResultContainer
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.emptySearchResultTitle
  }, title), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.emptySearchResultDescription
  }, description), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.emptySearchResultButton
  }, button?.menu ? /*#__PURE__*/_react.default.createElement(_buttonMenuAction.default, button) : /*#__PURE__*/_react.default.createElement(_buttonLink.default, button)));
};
const buildContentView = (content, ariaLabel, selectMultiple) => {
  const {
    type
  } = content;
  if (type === 'list' && (0, _isEmpty.default)(content.items) && content.emptyResult) {
    return buildEmptyResultView(content.emptyResult);
  }
  switch (type) {
    case 'list':
      return buildListItemsView(content, ariaLabel, selectMultiple);
    case 'expandible-actionable-table':
      return /*#__PURE__*/_react.default.createElement(_expandibleActionableTable.default, content);
  }
};
const ListItems = ({
  title,
  buttonLink,
  selectMultiple,
  content,
  'aria-label': ariaLabel,
  isFetching,
  search,
  checkboxWithTitle,
  actionButtons,
  buttonMenuAction
}) => {
  const contentView = buildContentView(content, ariaLabel, selectMultiple);
  const titleView = buildTitleView(title);
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.header
  }, checkboxWithTitle ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.checkboxAndActionsWrapper
  }, /*#__PURE__*/_react.default.createElement(_checkboxWithTitle.default, _extends({}, checkboxWithTitle, {
    title: checkboxWithTitle.title
  })), actionButtons ? _map.default.convert({
    cap: false
  })((action, index) => /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, action, {
    key: `action-button-key-${index}`
  })), actionButtons) : null) : /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.title
  }, titleView), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.inputWrapper
  }, !(0, _isEmpty.default)(search) ? /*#__PURE__*/_react.default.createElement(_inputSearch.default, search) : null, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.actionsWrapper
  }, !(0, _isEmpty.default)(selectMultiple) ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.selectMultiple
  }, /*#__PURE__*/_react.default.createElement(_selectMultiple.default, selectMultiple)) : null, !(0, _isEmpty.default)(buttonLink) ? /*#__PURE__*/_react.default.createElement(_buttonLink.default, buttonLink) : null), buttonMenuAction ? /*#__PURE__*/_react.default.createElement(_buttonMenuAction.default, buttonMenuAction) : null)), isFetching ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.loaderContainer
  }, /*#__PURE__*/_react.default.createElement(_loader.default, {
    className: _style.default.loader,
    theme: "coorpmanager"
  })) : contentView);
};
ListItems.propTypes = process.env.NODE_ENV !== "production" ? {
  'aria-label': _propTypes.default.string,
  buttonLink: _propTypes.default.shape(_buttonLink.default.propTypes),
  selectMultiple: _propTypes.default.shape(_selectMultiple.default.propTypes),
  content: _propTypes.default.oneOfType([_propTypes.default.shape({
    items: _propTypes.default.arrayOf(_propTypes.default.shape(_listItem.default.propTypes)),
    type: _propTypes.default.oneOf(['list']),
    itemType: _propTypes.default.string,
    onDrop: _propTypes.default.func,
    isDraggable: _propTypes.default.bool,
    tableHeader: _propTypes.default.arrayOf(_propTypes.default.string),
    emptyResult: _propTypes.default.shape({
      title: _propTypes.default.string,
      description: _propTypes.default.string,
      button: _propTypes.default.oneOfType([_propTypes.default.shape(_buttonLink.default.propTypes), _propTypes.default.shape(_buttonMenuAction.default.propTypes)])
    })
  }), _propTypes.default.shape({
    ..._expandibleActionableTable.default.propTypes,
    type: _propTypes.default.oneOf(['expandible-actionable-table'])
  })]),
  title: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.shape(_title.default.propTypes)]),
  isFetching: _propTypes.default.bool,
  search: _propTypes.default.shape(_inputSearch.default.propTypes),
  checkboxWithTitle: _propTypes.default.shape(_checkboxWithTitle.default.propTypes),
  actionButtons: _propTypes.default.arrayOf(_propTypes.default.shape(_buttonLink.default.propTypes)),
  buttonMenuAction: _propTypes.default.shape(_buttonMenuAction.default.propTypes)
} : {};
var _default = exports.default = ListItems;
//# sourceMappingURL=index.js.map