"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.validators = exports.converter = exports.types = void 0;

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

var _immer = require("immer");

var _2 = _interopRequireDefault(require(".."));

var _useDynamicElements = require("../../useDynamicElements");

var _intl = require("@formatjs/intl");

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

var types = ['formGroup']; //https://github.com/rjsf-team/react-jsonschema-form/issues/1206

exports.types = types;

function FormGroupListField(props) {
  var schemas = (0, _2["default"])({
    extraTypes: props.uiSchema['ui:options'].formulaProps.extraTypes,
    extraTypePlugins: props.uiSchema['ui:options'].formulaProps.extraTypePlugins,
    config: (0, _useDynamicElements.dynamicElements)((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props.uiSchema['ui:options'].element), {}, {
      list: false,
      title: undefined
    }), props.formData)
  });
  var SchemaField = props.registry.fields.SchemaField;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(SchemaField, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props), schemas));
}

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 = (0, _immer.freeze)(formulaProps);
    }

    if (config.pagination) {
      props.schema['pagination'] = true;
      props.schema['elementsPerPage'] = config.elementsPerPage;
    }
  };
};

exports.converter = converter;
var validationMessages = (0, _intl.defineMessages)({
  duplicateKey: {
    id: "pFHtzl",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "title"
    }, {
      "type": 0,
      "value": "\" ei saa sisältää samaa tekstiavainta kahdesti."
    }]
  }
});
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);
          }
        }
      }
    }
  }
};
exports.validators = validators;
//# sourceMappingURL=formGroupList.cjs.map