"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.elementType = exports.name = exports["default"] = void 0;

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

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

var _reactIntl = require("react-intl");

var _rjsfCore = require("@visma/rjsf-core");

var _react = require("react");

var _api = require("../../api");

var _lodash = require("lodash");

function Compose(props) {
  var intl = (0, _reactIntl.useIntl)();

  var _useContext = (0, _react.useContext)(_rjsfCore.utils.Context),
      formData = _useContext.formData;

  var fields = (0, _api.useFields)();
  var path = props.id.split('_').slice(1, -1);
  var usableFormData = path.length > 0 ? (0, _lodash.get)(formData, path) : formData;
  (0, _react.useEffect)(function () {
    var _props$options = props.options,
        values = _props$options.values,
        separator = _props$options.separator;
    var composedField = values.map(function (value) {
      var _ref = value ? value.split('.') : ['error', 'error'],
          _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
          key = _ref2[0],
          id = _ref2[1];

      return key === 'error' ? {
        value: undefined
      } : {
        value: usableFormData === null || usableFormData === void 0 ? void 0 : usableFormData[key],
        key: key,
        id: parseInt(id)
      };
    }).filter(function (data) {
      return data.value !== undefined;
    }).map(function getStringValue(data) {
      var _field$choices$find, _choice$enumNames, _data$value$start, _data$value$end;

      var field = fields.find(function (field) {
        return field._id === data.id;
      });

      if (!field) {
        return undefined;
      }

      if (Array.isArray(data.value)) {
        var listValue = data.value.map(function (dataValue) {
          return getStringValue((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, data), {}, {
            value: dataValue
          }));
        });
        return listValue.length > 0 ? listValue.join(separator) : undefined;
      } else {
        switch (field.type) {
          case 'select':
          case 'multiselect':
            var choice = (_field$choices$find = field.choices.find(function (choice) {
              return choice["enum"] === data.value;
            })) !== null && _field$choices$find !== void 0 ? _field$choices$find : field.choices[data.value];
            return (_choice$enumNames = choice === null || choice === void 0 ? void 0 : choice.enumNames) !== null && _choice$enumNames !== void 0 ? _choice$enumNames : data.value;

          case 'boolean':
            if (data.value) {
              return field.yes && field.yes !== '' ? field.yes : intl.formatMessage({
                id: "4ajajg",
                defaultMessage: [{
                  "type": 0,
                  "value": "Kyllä"
                }]
              });
            } else {
              return field.no && field.no !== '' ? field.no : intl.formatMessage({
                id: "JVSOMe",
                defaultMessage: [{
                  "type": 0,
                  "value": "Ei"
                }]
              });
            }

          case 'consent':
            if (data.value) {
              return field.yes && field.yes !== '' ? field.yes : intl.formatMessage({
                id: "F0dHQ0",
                defaultMessage: [{
                  "type": 0,
                  "value": "Hyväksyn"
                }]
              });
            } else {
              return undefined;
            }

          case 'dateRange':
            return "".concat((_data$value$start = data.value.start) !== null && _data$value$start !== void 0 ? _data$value$start : '', " - ").concat((_data$value$end = data.value.end) !== null && _data$value$end !== void 0 ? _data$value$end : '');

          case 'text':
          case 'textarea':
          case 'richtext':
          case 'email':
          case 'number':
          case 'password':
          case 'range':
          case 'bmi':
          case 'date':
            return data.value;

          default:
            return undefined;
        }
      }
    }).filter(function (data) {
      return data !== undefined;
    }).join(separator);
    setTimeout(function () {
      props.onChange(composedField);
    });
  }, [usableFormData, props.options, formData, fields]);
  return null;
}

var _default = function _default(props) {
  var _props$config = props.config,
      _props$config$compose = _props$config.composeValues,
      composeValues = _props$config$compose === void 0 ? [] : _props$config$compose,
      _props$config$compose2 = _props$config.composeSeparator,
      composeSeparator = _props$config$compose2 === void 0 ? '' : _props$config$compose2,
      type = _props$config.type;
  return {
    schema: {
      type: 'string',
      additionalType: type
    },
    uiSchema: {
      'ui:widget': Compose,
      'ui:options': {
        values: composeValues,
        separator: composeSeparator
      }
    }
  };
};

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "nYaKFF",
  defaultMessage: [{
    "type": 0,
    "value": "Koottu kenttä"
  }]
});
exports.name = name;
var elementType = 'element';
exports.elementType = elementType;
//# sourceMappingURL=compose.cjs.map