import _createForOfIteratorHelper from "@babel/runtime/helpers/createForOfIteratorHelper";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { freeze } from 'immer';
import configToSchemas from '..';
import { dynamicElements } from '../../useDynamicElements';
import { defineMessages } from '@formatjs/intl';
import { jsx as _jsx } from "react/jsx-runtime";
export var types = ['formGroup']; //https://github.com/rjsf-team/react-jsonschema-form/issues/1206

function FormGroupListField(props) {
  var schemas = configToSchemas({
    extraTypes: props.uiSchema['ui:options'].formulaProps.extraTypes,
    extraTypePlugins: props.uiSchema['ui:options'].formulaProps.extraTypePlugins,
    config: dynamicElements(_objectSpread(_objectSpread({}, props.uiSchema['ui:options'].element), {}, {
      list: false,
      title: undefined
    }), props.formData)
  });
  var SchemaField = props.registry.fields.SchemaField;
  return /*#__PURE__*/_jsx(SchemaField, _objectSpread(_objectSpread({}, props), schemas));
}

export var converter = function converter(formulaProps) {
  return function (props) {
    var config = formulaProps.config;

    if (config.list) {
      var _props$uiSchema, _uiOptions, _props$uiSchema$_uiOp;

      props.uiSchema['ui:field'] = FormGroupListField;
      (_props$uiSchema$_uiOp = (_props$uiSchema = props.uiSchema)[_uiOptions = 'ui:options']) !== null && _props$uiSchema$_uiOp !== void 0 ? _props$uiSchema$_uiOp : _props$uiSchema[_uiOptions] = {};
      props.uiSchema['ui:options'].formulaProps = freeze(formulaProps);
    }
  };
};
var validationMessages = defineMessages({
  duplicateKey: {
    id: "pFHtzl",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "title"
    }, {
      "type": 0,
      "value": "\" ei saa sisältää samaa tekstiavainta kahdesti."
    }]
  }
});
export var validators = {
  noDuplicateElementKeys: {
    name: "Ei duplikaattiavaimia",
    fn: function fn(value, element) {
      var keys = [];

      var _iterator = _createForOfIteratorHelper(value),
          _step;

      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var field = _step.value;

          if (keys.includes(field.key)) {
            return validationMessages.duplicateKey;
          } else {
            keys.push(field.key);
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
  }
};
//# sourceMappingURL=formGroupList.js.map