"use strict";

exports.__esModule = true;
exports.CustomFieldsForm = void 0;
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _designSystem = require("@wix/design-system");
var _FormFieldInput = require("../FormFieldInput");
var _CustomFieldsFormContext = require("../CustomFieldsFormContext");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/CustomFieldsForm/CustomFieldsForm.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const toCellSpan = type => {
  switch (type) {
    case 'longText':
    case 'checkbox':
    case 'shortText':
    case 'url':
    case 'dateTime':
      return 12;
    default:
      return 6;
  }
};
const _CustomFieldsForm = ({
  state,
  onChange
}) => {
  const {
    _namespace
  } = state;
  const renderFieldInput = (0, _react.useCallback)(field => {
    if (!field) {
      return null;
    }
    if (!_FormFieldInput.EDITABLE_INPUT_ELEMENTS.hasOwnProperty(field.type)) {
      return null;
    }
    const span = toCellSpan(field.type);
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
      key: field.id,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 42,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_designSystem.Cell, {
      span: span,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 43,
        columnNumber: 9
      }
    }, /*#__PURE__*/_react.default.createElement(_FormFieldInput.FormFieldInput, {
      dataHook: `field-${field.id}`,
      state: state,
      schema: field,
      initialValue: state.getInitialValue(field.id),
      onChange: newValue => state.setValue({
        id: field.id,
        value: newValue,
        onChange
      }),
      inputRef: inputRef => {
        state.setInputRef(field.id, inputRef);
      },
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 44,
        columnNumber: 11
      }
    })), span !== 12 && /*#__PURE__*/_react.default.createElement(_designSystem.Cell, {
      span: 12 - span,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 57,
        columnNumber: 25
      }
    }));
  }, []);
  const {
    writeableFields
  } = state;
  return /*#__PURE__*/_react.default.createElement(_CustomFieldsFormContext.CustomFieldsFormContextProvider, {
    value: state,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 65,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Layout, {
    dataHook: `custom-fields-form-${_namespace}`,
    gap: "SP3",
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 66,
      columnNumber: 7
    }
  }, writeableFields.map(renderFieldInput)));
};
const CustomFieldsForm = exports.CustomFieldsForm = (0, _mobxReactLite.observer)(_CustomFieldsForm);
//# sourceMappingURL=CustomFieldsForm.js.map