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);
    }

    if (config.pagination) {
      props.schema['pagination'] = true;
      props.schema['elementsPerPage'] = config.elementsPerPage;
    }
  };
};
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, _) {
      if (value) {
        var valueKeys = Object.keys(value);
        var keys = [];

        for (var _i = 0, _valueKeys = valueKeys; _i < _valueKeys.length; _i++) {
          var valueKey = _valueKeys[_i];
          var elementKey = value[valueKey].key;

          if (keys.includes(elementKey)) {
            return validationMessages.duplicateKey;
          } else {
            keys.push(elementKey);
          }
        }
      }
    }
  }
};
//# sourceMappingURL=formGroupList.js.map