Liferay.Loader.define("commerce-frontend-js@3.0.33/components/dataset_display/views/table/Table", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/table', 'frontend-js-react-web$classnames', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../../../utilities/index', '../../../data_renderers/ActionsDropdownRenderer', '../../../data_renderers/CheckboxRenderer', '../../../data_renderers/CommentRenderer', '../../../data_renderers/RadioRenderer', '../../../data_renderers/index', '../../DatasetDisplayContext', './TableHeadRow'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    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.default = void 0;

    var _table = _interopRequireDefault(require("@frontend-taglib-clay$clayui/table"));

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _index = require("../../../../utilities/index");

    var _ActionsDropdownRenderer = _interopRequireDefault(require("../../../data_renderers/ActionsDropdownRenderer"));

    var _CheckboxRenderer = _interopRequireDefault(require("../../../data_renderers/CheckboxRenderer"));

    var _CommentRenderer = _interopRequireDefault(require("../../../data_renderers/CommentRenderer"));

    var _RadioRenderer = _interopRequireDefault(require("../../../data_renderers/RadioRenderer"));

    var _index2 = require("../../../data_renderers/index");

    var _DatasetDisplayContext = _interopRequireDefault(require("../../DatasetDisplayContext"));

    var _TableHeadRow = _interopRequireDefault(require("./TableHeadRow"));

    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 _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }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;
    }

    function CustomTableCell(props) {
      var view = props.view;

      var _useState = (0, _react.useState)(_objectSpread(_objectSpread({}, view), {}, {
        Component: view.contentRendererModuleUrl ? null : (0, _index2.getDataRendererById)(view.contentRenderer)
      })),
          _useState2 = _slicedToArray(_useState, 2),
          currentView = _useState2[0],
          updateCurrentView = _useState2[1];

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          loading = _useState4[0],
          setLoading = _useState4[1];

      (0, _react.useEffect)(function () {
        if (loading) {
          return;
        }

        if (currentView.contentRendererModuleUrl) {
          setLoading(true);
          (0, _index2.getDataRendererByUrl)(currentView.contentRendererModuleUrl).then(function (Component) {
            updateCurrentView(_objectSpread(_objectSpread({}, currentView), {}, {
              Component: Component
            }));
            setLoading(false);
          });
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [currentView]);
      return (/*#__PURE__*/_react.default.createElement(_table.default.Cell, null, currentView.Component && !loading ? /*#__PURE__*/_react.default.createElement(currentView.Component, {
          actions: props.actions,
          itemData: props.itemData,
          itemId: props.itemId,
          options: props.options,
          value: props.value
        }) : /*#__PURE__*/_react.default.createElement("span", {
          "aria-hidden": "true",
          className: "loading-animation loading-animation-sm"
        }), props.comment && /*#__PURE__*/_react.default.createElement(_CommentRenderer.default, null, props.comment))
      );
    }

    function getItemFields(item, fields, itemId, itemsActions) {
      return fields.map(function (field, i) {
        var actionItems = item.actionItems,
            comments = item.comments;
        var rawValue = (0, _index.getValueFromItem)(item, field.fieldName);
        var formattedValue = field.mapData ? field.mapData(rawValue) : rawValue;
        var comment = comments ? comments[field.fieldName] : null;
        return (/*#__PURE__*/_react.default.createElement(CustomTableCell, {
            actions: itemsActions || actionItems,
            comment: comment,
            itemData: item,
            itemId: itemId,
            key: field.fieldName || i,
            options: field,
            value: formattedValue,
            view: {
              contentRenderer: field.contentRenderer,
              contentRendererModuleUrl: field.contentRendererModuleUrl
            }
          })
        );
      });
    }

    function Table(props) {
      var _useContext = (0, _react.useContext)(_DatasetDisplayContext.default),
          actionLoading = _useContext.actionLoading,
          highlightedItemsValue = _useContext.highlightedItemsValue,
          nestedItemsKey = _useContext.nestedItemsKey,
          nestedItemsReferenceKey = _useContext.nestedItemsReferenceKey,
          selectItems = _useContext.selectItems,
          selectable = _useContext.selectable,
          selectedItemsKey = _useContext.selectedItemsKey,
          selectedItemsValue = _useContext.selectedItemsValue,
          selectionType = _useContext.selectionType,
          sorting = _useContext.sorting,
          updateSorting = _useContext.updateSorting;

      var showActionItems = Boolean(props.itemsActions && props.itemsActions.length || props.items.find(function (el) {
        return el.actionItems;
      }));
      var SelectionComponent = selectionType === 'multiple' ? _CheckboxRenderer.default : _RadioRenderer.default;
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "table-style-".concat(props.style)
        }, /*#__PURE__*/_react.default.createElement(_table.default, {
          borderless: true,
          hover: false,
          responsive: true
        }, /*#__PURE__*/_react.default.createElement(_TableHeadRow.default, {
          items: props.items,
          schema: props.schema,
          selectItems: selectItems,
          selectable: selectable,
          selectedItemsKey: selectedItemsKey,
          selectedItemsValue: selectedItemsValue,
          selectionType: selectionType,
          showActionItems: showActionItems,
          sorting: sorting,
          updateSorting: updateSorting
        }), /*#__PURE__*/_react.default.createElement(_table.default.Body, null, props.items.map(function (item, i) {
          var itemId = item[selectedItemsKey] || i;
          var nestedItems = nestedItemsReferenceKey && item[nestedItemsReferenceKey];
          return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
              key: itemId
            }, /*#__PURE__*/_react.default.createElement(_table.default.Row, {
              className: (0, _classnames.default)(highlightedItemsValue.includes(itemId) && 'active')
            }, selectable && /*#__PURE__*/_react.default.createElement(_table.default.Cell, {
              className: "dataset-item-selector-wrapper",
              rowSpan: 1 + (nestedItems && nestedItems.length || 0)
            }, /*#__PURE__*/_react.default.createElement(SelectionComponent, {
              checked: !!selectedItemsValue.find(function (el) {
                return String(el) === String(itemId);
              }),
              disabled: actionLoading,
              onChange: function onChange() {
                return selectItems(itemId);
              },
              value: itemId
            })), getItemFields(item, props.schema.fields, itemId, props.itemsActions), showActionItems && /*#__PURE__*/_react.default.createElement(_table.default.Cell, {
              className: "dataset-item-actions-wrapper"
            }, (props.itemsActions || item.actionItems) && /*#__PURE__*/_react.default.createElement(_ActionsDropdownRenderer.default, {
              actions: props.itemsActions || item.actionItems,
              itemData: item,
              itemId: itemId
            }))), nestedItems && nestedItems.length ? nestedItems.map(function (nestedItem, i) {
              return (/*#__PURE__*/_react.default.createElement(_table.default.Row, {
                  className: (0, _classnames.default)('dataset-sub-item', highlightedItemsValue.includes(nestedItem[nestedItemsKey]) && 'active', i === nestedItems.length - 1 && 'last-of-group'),
                  key: nestedItem[nestedItemsKey]
                }, getItemFields(nestedItem, props.schema.fields, nestedItem[nestedItemsKey], props.itemsActions), showActionItems ? /*#__PURE__*/_react.default.createElement(_table.default.Cell, null) : null)
              );
            }) : null)
          );
        }))))
      );
    }

    Table.defaultProps = {
      items: []
    };
    var _default = Table;
    exports.default = _default;
    //# sourceMappingURL=Table.js.map
  }
});
//# sourceMappingURL=Table.js.map