import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["__withStepped_original_props__"];
import { makeStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import Box from '@material-ui/core/Box';
import { createContext, useContext } from 'react';
import { Customize } from '../utils';
import * as types from './types';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var useStyles = makeStyles(function (theme) {
  return {
    typeContainer: {
      marginBottom: theme.spacing(3)
    }
  };
});
var TitleVariantContext = /*#__PURE__*/createContext();
var titleComponents = {
  form: 'h2',
  formGroup: 'h3'
};
export default function Field(_ref) {
  var _useContext, _props$uiSchema$uiOp, _props$uiSchema$uiOp$, _props$uiSchema$uiOp2, _props$uiSchema$uiOp3, _props$uiSchema, _props$uiSchema$items, _props$uiSchema2, _props$uiSchema2$uiO, _props, _props$schema, _props2;

  var __withStepped_original_props__ = _ref.__withStepped_original_props__,
      props = _objectWithoutProperties(_ref, _excluded);

  var classes = useStyles();

  if (__withStepped_original_props__) {
    props = _objectSpread(_objectSpread({}, props), __withStepped_original_props__);
  }

  var Type = types[Array.isArray(props.schema.type) ? 'number' : props.schema.type];
  var variant = (_useContext = useContext(TitleVariantContext)) !== null && _useContext !== void 0 ? _useContext : 'h4';

  if (!Type) {
    console.error('Not implemented', props.schema.type, props);
    return null;
  }

  if (props.uiSchema && (props.uiSchema['ui:widget'] === 'hidden' || ((_props$uiSchema$uiOp = props.uiSchema['ui:options']) === null || _props$uiSchema$uiOp === void 0 ? void 0 : (_props$uiSchema$uiOp$ = _props$uiSchema$uiOp.element) === null || _props$uiSchema$uiOp$ === void 0 ? void 0 : _props$uiSchema$uiOp$.type) === 'compose' || ((_props$uiSchema$uiOp2 = props.uiSchema['ui:options']) === null || _props$uiSchema$uiOp2 === void 0 ? void 0 : (_props$uiSchema$uiOp3 = _props$uiSchema$uiOp2.element) === null || _props$uiSchema$uiOp3 === void 0 ? void 0 : _props$uiSchema$uiOp3.type) === 'button')) {
    return null;
  }

  var element = ((_props$uiSchema = props.uiSchema) === null || _props$uiSchema === void 0 ? void 0 : _props$uiSchema.items) ? (_props$uiSchema$items = props.uiSchema.items['ui:options']) === null || _props$uiSchema$items === void 0 ? void 0 : _props$uiSchema$items.element : (_props$uiSchema2 = props.uiSchema) === null || _props$uiSchema2 === void 0 ? void 0 : (_props$uiSchema2$uiO = _props$uiSchema2['ui:options']) === null || _props$uiSchema2$uiO === void 0 ? void 0 : _props$uiSchema2$uiO.element;
  return /*#__PURE__*/_jsx(TitleVariantContext.Provider, {
    value: {
      h4: 'h6',
      // h5 is reserved for stepTitle
      h6: 'h6',
      subtitle1: 'subtitle2',
      subtitle2: 'subtitle2'
    }[variant],
    children: /*#__PURE__*/_jsx(Box, {
      pl: element && element.indent ? 3 * element.indent : 0,
      children: /*#__PURE__*/_jsxs(Customize, _objectSpread(_objectSpread({
        customizer: props[props.preview ? 'previewField' : 'reviewField']
      }, props), {}, {
        children: [props.schema.title && /*#__PURE__*/_jsx(Typography, {
          style: {
            wordBreak: 'break-word'
          },
          variant: variant,
          component: Object.keys(titleComponents).includes(element.type) ? titleComponents[element.type] : 'p',
          gutterBottom: true,
          children: props.schema.title
        }), /*#__PURE__*/_jsx("div", {
          className: classes.typeContainer,
          children: /*#__PURE__*/_jsx(Type, _objectSpread({}, props))
        }), (element === null || element === void 0 ? void 0 : element.type) === "formGroup" && (element === null || element === void 0 ? void 0 : element.listItem) && ((_props = props) === null || _props === void 0 ? void 0 : (_props$schema = _props.schema) === null || _props$schema === void 0 ? void 0 : _props$schema.type) !== "array" && !((_props2 = props) === null || _props2 === void 0 ? void 0 : _props2.lastItem) && /*#__PURE__*/_jsxs(_Fragment, {
          children: [/*#__PURE__*/_jsx("hr", {}), /*#__PURE__*/_jsx("p", {})]
        })]
      }))
    })
  });
}
//# sourceMappingURL=Field.js.map