import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["formData", "schema", "uiSchema"];
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import { format } from 'date-fns';
import Field from '../Field';
import { dynamicElements } from '../../useDynamicElements';
import { jsx as _jsx } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import { jsxs as _jsxs } from "react/jsx-runtime";
export default (function (props) {
  var _uiSchema$uiOptions, _schema$properties;

  var formData = props.formData,
      schema = props.schema,
      uiSchema = props.uiSchema,
      otherProps = _objectWithoutProperties(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 && 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 && 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 && format(new Date(formData.start), 'd.M.yyyy');
      end = formData.end && 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__*/_jsx(Field, _objectSpread(_objectSpread({}, 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 = dynamicElements(_objectSpread(_objectSpread({}, 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__*/_createElement(Field, _objectSpread(_objectSpread({}, 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__*/_createElement(Field, _objectSpread(_objectSpread({}, 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__*/_jsx(TableContainer, {
      children: /*#__PURE__*/_jsxs(Table, {
        size: "small",
        children: [/*#__PURE__*/_jsx(TableHead, {
          children: /*#__PURE__*/_jsx(TableRow, {
            children: columns.map(function (item, index) {
              return /*#__PURE__*/_jsx(TableCell, {
                children: item
              }, 'header-' + index);
            })
          })
        }), /*#__PURE__*/_jsx(TableBody, {
          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__*/_jsx(TableRow, {
              children: rowItem.map(function (item, itemIndex) {
                return /*#__PURE__*/_jsx(TableCell, {
                  children: item
                }, 'cell-' + rowIndex + '-' + itemIndex);
              })
            }, 'row-' + rowIndex);
          })
        })]
      })
    });
  }
});
//# sourceMappingURL=object.js.map