Liferay.Loader.define("app-builder-web@1.0.73/js/pages/entry/ViewEntry.es", ['module', 'exports', 'require', 'frontend-js-react-web', 'frontend-js-react-web$react', '../../AppContext.es', '../../components/control-menu/ControlMenu.es', '../../components/loading/Loading.es', '../../hooks/useDataLayout.es', '../../hooks/useQuery.es', '../../utils/client.es', '../../utils/toast.es', '../../utils/utils.es', './FieldPreview.es', './ViewEntryUpperToolbar.es'], 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.ViewDataLayoutPageValues = ViewDataLayoutPageValues;
    exports.default = ViewEntry;

    var _frontendJsReactWeb = require("frontend-js-react-web");

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

    var _AppContext = require("../../AppContext.es");

    var _ControlMenu = _interopRequireDefault(require("../../components/control-menu/ControlMenu.es"));

    var _Loading = require("../../components/loading/Loading.es");

    var _useDataLayout2 = _interopRequireDefault(require("../../hooks/useDataLayout.es"));

    var _useQuery3 = _interopRequireDefault(require("../../hooks/useQuery.es"));

    var _client = require("../../utils/client.es");

    var _toast = require("../../utils/toast.es");

    var _utils = require("../../utils/utils.es");

    var _FieldPreview = _interopRequireWildcard(require("./FieldPreview.es"));

    var _ViewEntryUpperToolbar = _interopRequireDefault(require("./ViewEntryUpperToolbar.es"));

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

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

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

    var getSections = function getSections(_ref) {
      var _ref$dataDefinitionFi = _ref.dataDefinitionFields,
          dataDefinitionFields = _ref$dataDefinitionFi === void 0 ? [] : _ref$dataDefinitionFi;
      var sections = {};
      dataDefinitionFields.forEach(function (_ref2) {
        var collapsible = _ref2.customProperties.collapsible,
            name = _ref2.name,
            nestedDataDefinitionFields = _ref2.nestedDataDefinitionFields;

        if (nestedDataDefinitionFields.length) {
          sections[name] = {
            collapsible: collapsible,
            fields: nestedDataDefinitionFields.map(function (_ref3) {
              var name = _ref3.name;
              return name;
            }),
            nestedDataDefinitionFields: nestedDataDefinitionFields
          };
        }
      });
      return sections;
    };

    function ViewDataLayoutPageValues(_ref4) {
      var dataDefinition = _ref4.dataDefinition,
          dataLayoutPage = _ref4.dataLayoutPage,
          dataRecordValues = _ref4.dataRecordValues;
      var dataLayoutRows = dataLayoutPage.dataLayoutRows;
      var defaultLanguageId = dataDefinition.defaultLanguageId;
      var sections = getSections(dataDefinition);
      return dataLayoutRows.reduce(function (fields, _ref5) {
        var _ref5$dataLayoutColum = _ref5.dataLayoutColumns,
            dataLayoutColumns = _ref5$dataLayoutColum === void 0 ? [] : _ref5$dataLayoutColum;
        return [].concat(_toConsumableArray(fields), _toConsumableArray(dataLayoutColumns.reduce(function (fields, _ref6) {
          var _ref6$fieldNames = _ref6.fieldNames,
              fieldNames = _ref6$fieldNames === void 0 ? [] : _ref6$fieldNames;
          return [].concat(_toConsumableArray(fields), _toConsumableArray(fieldNames));
        }, [])));
      }, []).map(function (fieldName) {
        var fieldGroup = sections[fieldName];

        if (fieldGroup) {
          return (/*#__PURE__*/_react.default.createElement(_FieldPreview.SectionRenderer, {
              collapsible: fieldGroup.collapsible,
              dataDefinition: dataDefinition,
              fieldName: fieldName
            }, fieldGroup.fields.map(function (field) {
              return (/*#__PURE__*/_react.default.createElement(_FieldPreview.default, {
                  dataDefinition: _objectSpread(_objectSpread({}, dataDefinition), {}, {
                    dataDefinitionFields: fieldGroup.nestedDataDefinitionFields
                  }),
                  dataRecordValues: dataRecordValues,
                  defaultLanguageId: defaultLanguageId,
                  fieldName: field,
                  key: field
                })
              );
            }))
          );
        }

        return (/*#__PURE__*/_react.default.createElement(_FieldPreview.default, {
            dataDefinition: dataDefinition,
            dataRecordValues: dataRecordValues,
            defaultLanguageId: defaultLanguageId,
            fieldName: fieldName,
            key: fieldName
          })
        );
      });
    }

    function ViewEntry(_ref7) {
      var history = _ref7.history,
          entryIndex = _ref7.match.params.entryIndex;

      var _useContext = (0, _react.useContext)(_AppContext.AppContext),
          dataDefinitionId = _useContext.dataDefinitionId,
          dataLayoutId = _useContext.dataLayoutId,
          dataListViewId = _useContext.dataListViewId;

      var _useDataLayout = (0, _useDataLayout2.default)(dataLayoutId, dataDefinitionId),
          dataDefinition = _useDataLayout.dataDefinition,
          dataLayoutPages = _useDataLayout.dataLayout.dataLayoutPages,
          isLoading = _useDataLayout.isLoading;

      var _useState = (0, _react.useState)({
        dataRecord: {},
        isFetching: true,
        page: 1,
        totalCount: 0
      }),
          _useState2 = _slicedToArray(_useState, 2),
          _useState2$ = _useState2[0],
          dataRecord = _useState2$.dataRecord,
          isFetching = _useState2$.isFetching,
          page = _useState2$.page,
          totalCount = _useState2$.totalCount,
          setState = _useState2[1];

      var _dataRecord$dataRecor = dataRecord.dataRecordValues,
          dataRecordValues = _dataRecord$dataRecor === void 0 ? {} : _dataRecord$dataRecor,
          dataRecordId = dataRecord.id;

      var _useQuery = (0, _useQuery3.default)(history, {
        keywords: '',
        page: 1,
        sort: ''
      }),
          _useQuery2 = _slicedToArray(_useQuery, 1),
          query = _useQuery2[0];

      var previousQuery = (0, _frontendJsReactWeb.usePrevious)(query);
      var previousIndex = (0, _frontendJsReactWeb.usePrevious)(entryIndex);
      (0, _react.useEffect)(function () {
        if (!(0, _utils.isEqualObjects)(query, previousQuery) || entryIndex !== previousIndex) {
          (0, _client.getItem)("/o/data-engine/v2.0/data-definitions/".concat(dataDefinitionId, "/data-records"), _objectSpread(_objectSpread({}, query), {}, {
            dataListViewId: dataListViewId,
            page: entryIndex,
            pageSize: 1
          })).then(function (_ref8) {
            var _ref8$items = _ref8.items,
                items = _ref8$items === void 0 ? [] : _ref8$items,
                response = _objectWithoutProperties(_ref8, ["items"]);

            if (items.length > 0) {
              setState(_objectSpread({
                dataRecord: items.pop(),
                isFetching: false
              }, response));
            }
          }).catch(function () {
            setState(function (prevState) {
              return _objectSpread(_objectSpread({}, prevState), {}, {
                isFetching: false
              });
            });
            (0, _toast.errorToast)();
          });
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [entryIndex, query]);

      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "view-entry"
        }, /*#__PURE__*/_react.default.createElement(_ControlMenu.default, {
          backURL: function getBackURL() {
            var urlParams = new URLSearchParams(window.location.hash);
            var backURL = urlParams.get('backURL') || '../../';
            return backURL;
          }(),
          title: Liferay.Language.get('details-view')
        }), /*#__PURE__*/_react.default.createElement(_ViewEntryUpperToolbar.default, {
          dataRecordId: dataRecordId,
          page: page,
          totalCount: totalCount
        }), /*#__PURE__*/_react.default.createElement(_Loading.Loading, {
          isLoading: isLoading || isFetching
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "container"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "justify-content-center row"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "col-lg-8"
        }, dataLayoutPages && dataRecordValues && dataLayoutPages.map(function (dataLayoutPage, index) {
          return (/*#__PURE__*/_react.default.createElement("div", {
              className: "sheet",
              key: index
            }, /*#__PURE__*/_react.default.createElement(ViewDataLayoutPageValues, {
              dataDefinition: dataDefinition,
              dataLayoutPage: dataLayoutPage,
              dataRecordValues: dataRecordValues,
              key: index
            }))
          );
        }))))))
      );
    }
    //# sourceMappingURL=ViewEntry.es.js.map
  }
});
//# sourceMappingURL=ViewEntry.es.js.map