import _objectSpread from "@babel/runtime/helpers/objectSpread2";

/* eslint-disable formatjs/no-id */

/* eslint-disable formatjs/enforce-default-message */
import { defineMessages } from '@formatjs/intl';
import { get, mapValues } from 'lodash';
import { useIntl } from 'react-intl';
var minItemsDefaults = defineMessages({
  multiselect: {
    id: "G6+GRP",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" tulee sisältää vähintään "
    }, {
      "type": 1,
      "value": "limit"
    }, {
      "type": 0,
      "value": " valintaa"
    }]
  }
});
var maxItemsDefaults = defineMessages({
  multiselect: {
    id: "/rNoT8",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" tulee sisältää korkeintaan "
    }, {
      "type": 1,
      "value": "limit"
    }, {
      "type": 0,
      "value": " valintaa"
    }]
  }
});
var messages = defineMessages({
  minItems: {
    id: "hmeyg+",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" tulee sisältää vähintään "
    }, {
      "type": 1,
      "value": "limit"
    }, {
      "type": 0,
      "value": " alkiota"
    }]
  },
  maxItems: {
    id: "W4OYt3",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" tulee sisältää korkeintaan "
    }, {
      "type": 1,
      "value": "limit"
    }, {
      "type": 0,
      "value": " alkiota"
    }]
  },
  minLength: {
    id: "IVPaJf",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" "
    }, {
      "type": 6,
      "value": "limit",
      "options": {
        "=1": {
          "value": [{
            "type": 0,
            "value": "on pakollinen kenttä"
          }]
        },
        "other": {
          "value": [{
            "type": 0,
            "value": "tulee sisältää vähintään "
          }, {
            "type": 7
          }, {
            "type": 0,
            "value": " merkkiä"
          }]
        }
      },
      "offset": 0,
      "pluralType": "cardinal"
    }]
  },
  maxLength: {
    id: "G7a3as",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" "
    }, {
      "type": 6,
      "value": "limit",
      "options": {
        "=1": {
          "value": [{
            "type": 0,
            "value": "on pakollinen kenttä"
          }]
        },
        "other": {
          "value": [{
            "type": 0,
            "value": "tulee sisältää korkeintaan "
          }, {
            "type": 7
          }, {
            "type": 0,
            "value": " merkkiä"
          }]
        }
      },
      "offset": 0,
      "pluralType": "cardinal"
    }]
  },
  "enum": {
    id: "+r5C/M",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" vaatii hyväksynnän"
    }]
  },
  minimum: {
    id: "FwbDUm",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" täytyy olla suurempi tai yhtä suuri kuin "
    }, {
      "type": 1,
      "value": "limit"
    }]
  },
  maximum: {
    id: "tcHDm5",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" täytyy olla pienempi tai yhtä suuri kuin "
    }, {
      "type": 1,
      "value": "limit"
    }]
  },
  anyOf: {
    id: "56lFh0",
    defaultMessage: [{
      "type": 0,
      "value": "Vähintään yhteen kysymysryhmän \""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" kysymyksistä pitää vastata"
    }]
  },
  anyOfRequired: {
    id: "WTXEWy",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" sisältyy kysymysryhmään, jossa vähintään yhteen pitää vastata."
    }]
  },
  pattern: {
    id: "f5j8Od",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" arvon on oltava "
    }, {
      "type": 1,
      "value": "description"
    }]
  },
  required: {
    id: "vDtuSs",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" on pakollinen kenttä"
    }]
  },
  requiredStart: {
    id: "ABKcKk",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" alkupäivä on pakollinen kenttä"
    }]
  },
  requiredEnd: {
    id: "R2MYSW",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" loppupäivä on pakollinen kenttä"
    }]
  },
  choices: {
    id: "9lzqYa",
    defaultMessage: [{
      "type": 0,
      "value": "Valintalistassa tulee olla vähintään yksi vaihtoehto"
    }]
  },
  type: {
    id: "DM3D34",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "field"
    }, {
      "type": 0,
      "value": "\" tulee olla "
    }, {
      "type": 1,
      "value": "type"
    }]
  }
});
var type = defineMessages({
  file: {
    id: "Xj0Za3",
    defaultMessage: [{
      "type": 0,
      "value": "tiedosto"
    }]
  },
  string: {
    id: "ry193p",
    defaultMessage: [{
      "type": 0,
      "value": "tekstiarvo"
    }]
  },
  number: {
    id: "jE1kM/",
    defaultMessage: [{
      "type": 0,
      "value": "numero"
    }]
  },
  "boolean": {
    id: "OW2+mS",
    defaultMessage: [{
      "type": 0,
      "value": "booleanarvo"
    }]
  },
  array: {
    id: "B0D64L",
    defaultMessage: [{
      "type": 0,
      "value": "taulu"
    }]
  },
  'number,null': {
    id: "jE1kM/",
    defaultMessage: [{
      "type": 0,
      "value": "numero"
    }]
  }
});
var params = {
  type: type,
  minItemsDefaults: minItemsDefaults,
  maxItemsDefaults: maxItemsDefaults
};
export default (function (props) {
  var intl = useIntl();
  return _objectSpread(_objectSpread({}, props), {}, {
    transformErrors: function transformErrors(errors) {
      var transformedErrors = errors.map(function (error) {
        var _fieldUISchema$uiOpt, _fieldUISchema$uiOpt$, _fieldUISchema$uiOpt2, _fieldUISchema$uiOpt3, _fieldUISchema$uiOpt4, _fieldUISchema$uiOpt5, _fieldUISchema$uiOpt6, _fieldUISchema$uiOpt8;

        var errorName = error.property.includes('.end') ? 'requiredEnd' : error.property.includes('.start') ? 'requiredStart' : error.property.includes('choices') ? 'choices' : error.name;
        var messageDescriptor = messages[errorName];

        if (!messageDescriptor) {
          return error;
        }

        var _props$__useDynamicEl = props.__useDynamicElements_original_props__,
            schema = _props$__useDynamicEl.schema,
            uiSchema = _props$__useDynamicEl.uiSchema;
        var errorProperty = error.property // .properties[0].enum --> [0]
        .replace(/^\.properties/, '').replace(/\.end$/, '').replace(/\.start$/, '').replace(/\.enum$/, '') // Array item's array: ['0'][10] --> ['0']
        .replace(/\[\d+\]$/, '');
        var errorPropertyArray = errorProperty // remove first character ['0'] --> '0'], .key --> key
        .slice(1) // replace ][ with . '0'][1].key -> '0'.1.key
        .replaceAll('[', '.').replaceAll(']', '').split('.');

        var _errorPropertyArray$r = errorPropertyArray.reduce(function accumulator(_ref, pathPart, index) {
          var _ref$fieldSchema = _ref.fieldSchema,
              fieldSchema = _ref$fieldSchema === void 0 ? {} : _ref$fieldSchema,
              _ref$fieldUISchema = _ref.fieldUISchema,
              fieldUISchema = _ref$fieldUISchema === void 0 ? {} : _ref$fieldUISchema;

          if (fieldSchema.type === 'object') {
            var _itemUiSchema$items;

            var itemUiSchema = get(fieldUISchema, "[".concat(pathPart, "]"));
            return {
              fieldSchema: get(fieldSchema.properties, "[".concat(pathPart, "]")),
              //fieldUISchema: (itemUiSchema?.items?.patternDescription || itemUiSchema?.items) ? itemUiSchema?.items : itemUiSchema,
              fieldUISchema: (itemUiSchema === null || itemUiSchema === void 0 ? void 0 : (_itemUiSchema$items = itemUiSchema.items) === null || _itemUiSchema$items === void 0 ? void 0 : _itemUiSchema$items.patternDescription) ? itemUiSchema === null || itemUiSchema === void 0 ? void 0 : itemUiSchema.items : itemUiSchema
            };
          }

          if (fieldSchema.type === 'array' && !fieldSchema.items["enum"]) {
            return accumulator({
              fieldSchema: fieldSchema.items,
              fieldUISchema: fieldUISchema.items
            }, errorPropertyArray[index + 1]);
          }

          return {
            fieldSchema: fieldSchema,
            fieldUISchema: fieldUISchema
          };
        }, {
          fieldSchema: schema,
          fieldUISchema: uiSchema
        }),
            fieldSchema = _errorPropertyArray$r.fieldSchema,
            fieldUISchema = _errorPropertyArray$r.fieldUISchema;

        var title = (fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.title) !== '' ? fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.title : ((_fieldUISchema$uiOpt = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt === void 0 ? void 0 : (_fieldUISchema$uiOpt$ = _fieldUISchema$uiOpt.element) === null || _fieldUISchema$uiOpt$ === void 0 ? void 0 : _fieldUISchema$uiOpt$.labelError) !== '' && ((_fieldUISchema$uiOpt2 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt2 === void 0 ? void 0 : (_fieldUISchema$uiOpt3 = _fieldUISchema$uiOpt2.element) === null || _fieldUISchema$uiOpt3 === void 0 ? void 0 : _fieldUISchema$uiOpt3.useLabel) ? (_fieldUISchema$uiOpt4 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt4 === void 0 ? void 0 : (_fieldUISchema$uiOpt5 = _fieldUISchema$uiOpt4.element) === null || _fieldUISchema$uiOpt5 === void 0 ? void 0 : _fieldUISchema$uiOpt5.labelError : '';

        var _ref2 = fieldUISchema !== null && fieldUISchema !== void 0 ? fieldUISchema : {},
            patternDescription = _ref2.patternDescription;

        if (error.name === 'minItems' && minItemsDefaults[(_fieldUISchema$uiOpt6 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt6 === void 0 ? void 0 : _fieldUISchema$uiOpt6.element.type]) {
          var _fieldUISchema$uiOpt7;

          messageDescriptor = minItemsDefaults[(_fieldUISchema$uiOpt7 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt7 === void 0 ? void 0 : _fieldUISchema$uiOpt7.element.type];
        } else if (error.name === 'maxItems' && maxItemsDefaults[(_fieldUISchema$uiOpt8 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt8 === void 0 ? void 0 : _fieldUISchema$uiOpt8.element.type]) {
          var _fieldUISchema$uiOpt9;

          messageDescriptor = maxItemsDefaults[(_fieldUISchema$uiOpt9 = fieldUISchema['ui:options']) === null || _fieldUISchema$uiOpt9 === void 0 ? void 0 : _fieldUISchema$uiOpt9.element.type];
        }

        var message = intl.formatMessage(messageDescriptor, _objectSpread({
          field: title,
          description: {
            pattern: patternDescription
          }[errorName]
        }, mapValues(mapValues(error.params, stringDataUrlToFile(fieldSchema, error)), function (value, key) {
          return key in params && value in params[key] ? intl.formatMessage(params[key][value]) : value;
        })));
        return _objectSpread(_objectSpread({}, error), {}, {
          params: {
            pattern: patternDescription
          },
          message: message,
          stack: message
        });
      });
      return props.transformErrors ? props.transformErrors(transformedErrors) : transformedErrors;
    }
  });
});

var stringDataUrlToFile = function stringDataUrlToFile(fieldSchema, error) {
  return function (value) {
    var _fieldSchema$items;

    return [fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.format, fieldSchema === null || fieldSchema === void 0 ? void 0 : (_fieldSchema$items = fieldSchema.items) === null || _fieldSchema$items === void 0 ? void 0 : _fieldSchema$items.format].includes('data-url') && error.name !== 'minItems' ? 'file' : value;
  };
};
//# sourceMappingURL=useLocalizeErrors.js.map