"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _Table = _interopRequireDefault(require("@material-ui/core/Table"));

var _TableBody = _interopRequireDefault(require("@material-ui/core/TableBody"));

var _TableCell = _interopRequireDefault(require("@material-ui/core/TableCell"));

var _TableContainer = _interopRequireDefault(require("@material-ui/core/TableContainer"));

var _TableHead = _interopRequireDefault(require("@material-ui/core/TableHead"));

var _TableRow = _interopRequireDefault(require("@material-ui/core/TableRow"));

var _dateFns = require("date-fns");

var _Field = _interopRequireDefault(require("../Field"));

var _useDynamicElements = require("../../useDynamicElements");

var _jsxRuntime = require("react/jsx-runtime");

var _react = require("react");

var _excluded = ["formData", "schema", "uiSchema"];

var _default = function _default(props) {
  var _uiSchema$uiOptions, _schema$properties;

  var formData = props.formData,
      schema = props.schema,
      uiSchema = props.uiSchema,
      otherProps = (0, _objectWithoutProperties2["default"])(props, _excluded); // dateRange

  if (((_uiSchema$uiOptions = uiSchema['ui:options']) === null || _uiSchema$uiOptions === void 0 ? void 0 : _uiSchema$uiOptions.element.type) === 'dateRange') {
    var _start, _end;

    var start;
    var end;

    try {
      var _otherProps$reviewPro, _otherProps$reviewPro2, _otherProps$reviewPro3, _otherProps$reviewPro4;

      start = formData.start && (0, _dateFns.format)(new Date(formData.start), (_otherProps$reviewPro = otherProps === null || otherProps === void 0 ? void 0 : (_otherProps$reviewPro2 = otherProps.reviewProps) === null || _otherProps$reviewPro2 === void 0 ? void 0 : _otherProps$reviewPro2.dateFormat) !== null && _otherProps$reviewPro !== void 0 ? _otherProps$reviewPro : 'd.M.yyyy');
      end = formData.end && (0, _dateFns.format)(new Date(formData.end), (_otherProps$reviewPro3 = otherProps === null || otherProps === void 0 ? void 0 : (_otherProps$reviewPro4 = otherProps.reviewProps) === null || _otherProps$reviewPro4 === void 0 ? void 0 : _otherProps$reviewPro4.dateFormat) !== null && _otherProps$reviewPro3 !== void 0 ? _otherProps$reviewPro3 : 'd.M.yyyy');
    } catch (e) {
      var _otherProps$reviewPro5;

      // eslint-disable-next-line no-console
      console.warn("".concat(otherProps === null || otherProps === void 0 ? void 0 : (_otherProps$reviewPro5 = otherProps.reviewProps) === null || _otherProps$reviewPro5 === void 0 ? void 0 : _otherProps$reviewPro5.dateFormat, " is not a valid dateformat, using default format d.M.yyyy"));
      start = formData.start && (0, _dateFns.format)(new Date(formData.start), 'd.M.yyyy');
      end = formData.end && (0, _dateFns.format)(new Date(formData.end), 'd.M.yyyy');
    }

    var dateValue = "".concat((_start = start) !== null && _start !== void 0 ? _start : '', " - ").concat((_end = end) !== null && _end !== void 0 ? _end : '');
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Field["default"], (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, otherProps), {}, {
      formData: dateValue,
      schema: {
        schema: schema,
        type: 'string'
      },
      uiSchema: uiSchema
    }));
  } // forms and formGroups


  if (uiSchema['ui:order']) {
    var _uiSchema$uiOptions2, _uiSchema$uiOptions2$;

    if (((_uiSchema$uiOptions2 = uiSchema['ui:options']) === null || _uiSchema$uiOptions2 === void 0 ? void 0 : (_uiSchema$uiOptions2$ = _uiSchema$uiOptions2.element) === null || _uiSchema$uiOptions2$ === void 0 ? void 0 : _uiSchema$uiOptions2$.type) === 'formGroup') {
      var elementKeys = (0, _useDynamicElements.dynamicElements)((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, uiSchema['ui:options'].element), {}, {
        list: false,
        title: undefined
      }), props.formData).elements.map(function (element) {
        return element.key;
      });
      return uiSchema['ui:order'].filter(function (name) {
        return elementKeys.includes(name);
      }).map(function (name) {
        return /*#__PURE__*/(0, _react.createElement)(_Field["default"], (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, otherProps), {}, {
          key: name,
          formData: formData === null || formData === void 0 ? void 0 : formData[name],
          schema: schema.properties[name],
          uiSchema: uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[name]
        }));
      });
    } else {
      return uiSchema['ui:order'].map(function (name) {
        return /*#__PURE__*/(0, _react.createElement)(_Field["default"], (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, otherProps), {}, {
          key: name,
          formData: formData === null || formData === void 0 ? void 0 : formData[name],
          schema: schema.properties[name],
          uiSchema: uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[name]
        }));
      });
    }
  } // tables


  if ((_schema$properties = schema.properties) === null || _schema$properties === void 0 ? void 0 : _schema$properties.table) {
    var _formData$table;

    var columns = uiSchema['ui:options'].element.tableColumns;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableContainer["default"], {
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Table["default"], {
        size: "small",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_TableHead["default"], {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableRow["default"], {
            children: columns.map(function (item, index) {
              return /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableCell["default"], {
                children: item
              }, 'header-' + index);
            })
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableBody["default"], {
          children: formData === null || formData === void 0 ? void 0 : (_formData$table = formData.table) === null || _formData$table === void 0 ? void 0 : _formData$table.map(function (rowItem, rowIndex) {
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableRow["default"], {
              children: rowItem.map(function (item, itemIndex) {
                return /*#__PURE__*/(0, _jsxRuntime.jsx)(_TableCell["default"], {
                  children: item
                }, 'cell-' + rowIndex + '-' + itemIndex);
              })
            }, 'row-' + rowIndex);
          })
        })]
      })
    });
  }
};

exports["default"] = _default;
//# sourceMappingURL=object.cjs.map