"use strict";

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

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

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

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

var _styles = require("@material-ui/core/styles");

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _Box = _interopRequireDefault(require("@material-ui/core/Box"));

var _react = require("react");

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

var types = _interopRequireWildcard(require("./types"));

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

var _excluded = ["__withStepped_original_props__"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    typeContainer: {
      marginBottom: theme.spacing(3)
    }
  };
});
var TitleVariantContext = /*#__PURE__*/(0, _react.createContext)();
var titleComponents = {
  form: 'h2',
  formGroup: 'h3'
};

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 = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
  var classes = useStyles();

  if (__withStepped_original_props__) {
    props = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props), __withStepped_original_props__);
  }

  var Type = types[Array.isArray(props.schema.type) ? 'number' : props.schema.type];
  var variant = (_useContext = (0, _react.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__*/(0, _jsxRuntime.jsx)(TitleVariantContext.Provider, {
    value: {
      h4: 'h6',
      // h5 is reserved for stepTitle
      h6: 'h6',
      subtitle1: 'subtitle2',
      subtitle2: 'subtitle2'
    }[variant],
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Box["default"], {
      pl: element && element.indent ? 3 * element.indent : 0,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_utils.Customize, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        customizer: props[props.preview ? 'previewField' : 'reviewField']
      }, props), {}, {
        children: [props.schema.title && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          style: {
            wordBreak: 'break-word'
          },
          variant: variant,
          component: Object.keys(titleComponents).includes(element.type) ? titleComponents[element.type] : 'p',
          gutterBottom: true,
          children: props.schema.title
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: classes.typeContainer,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, (0, _objectSpread2["default"])({}, 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__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("hr", {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {})]
        })]
      }))
    })
  });
}
//# sourceMappingURL=Field.cjs.map