"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.widgets = 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 _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));

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

var _utils = require("../../utils");

var _select = _interopRequireDefault(require("./select"));

var _extendType = _interopRequireDefault(require("./_extendType"));

var _sift = _interopRequireDefault(require("sift"));

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

function MultiSelect(props) {
  var _props$value;

  var _props$options$elemen = props.options.element,
      widget = _props$options$elemen.widget,
      _props$options$elemen2 = _props$options$elemen.choices,
      choices = _props$options$elemen2 === void 0 ? [] : _props$options$elemen2,
      filterDialog = _props$options$elemen.filterDialog;
  var data = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : [];
  var disabled = [];
  var ensuredWidget = (0, _utils.ensureValueIsAvailable)(widget, widgets);

  if (!filterDialog) {
    var _iterator = (0, _createForOfIteratorHelper2["default"])(choices.entries()),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var _step$value = (0, _slicedToArray2["default"])(_step.value, 2),
            i = _step$value[0],
            choice = _step$value[1];

        if (choice.enumDisabled) {
          var _choice$enumDisabled$, _choice$enumDisabled$2;

          var choiceEnum = choice["enum"] ? choice["enum"] : "".concat(i);

          if ((0, _sift["default"])((_choice$enumDisabled$ = choice.enumDisabled.selected) === null || _choice$enumDisabled$ === void 0 ? void 0 : _choice$enumDisabled$.query)(data)) {
            disabled.push(choiceEnum);

            if (!data.includes(choiceEnum)) {
              props.value.push(choiceEnum);
            }
          }

          if ((0, _sift["default"])((_choice$enumDisabled$2 = choice.enumDisabled.notSelected) === null || _choice$enumDisabled$2 === void 0 ? void 0 : _choice$enumDisabled$2.query)(data)) {
            disabled.push(choiceEnum);

            if (data.includes(choiceEnum)) {
              props.value.splice(props.value.indexOf(choiceEnum), 1);
            }
          }
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  }

  var Widget = ensuredWidget === 'select' ? props.registry.widgets.SelectWidget : ensuredWidget && ensuredWidget.startsWith('checkboxes') ? props.registry.widgets.CheckboxesWidget : null;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: Widget ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Widget, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props), {}, {
      options: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props.options), {}, {
        enumOptions: props.options.enumOptions ? props.options.enumOptions : [],
        enumDisabled: disabled
      })
    })) : null
  });
}

var _default = (0, _extendType["default"])(_select["default"], function (_ref) {
  var config = _ref.config,
      fillProps = _ref.fillProps;
  return function (props) {
    var autocomplete = config.autocomplete,
        _config$choices = config.choices,
        choices = _config$choices === void 0 ? [] : _config$choices,
        minItems = config.minItems,
        required = config.required,
        widget = config.widget,
        _config$autoSort = config.autoSort,
        autoSort = _config$autoSort === void 0 ? false : _config$autoSort;
    var choicesSorted = (0, _utils.sortChoices)(choices, autoSort);
    props.schema = choicesSorted.length || autocomplete ? {
      items: props.schema,
      type: 'array',
      uniqueItems: true,
      "default": [],
      minItems: minItems !== null && minItems !== void 0 ? minItems : required ? 1 : undefined
    } : // Empty list of choices (enums) matches incorrectly when used in
    // dynamic list item, when oneOf has multiple fields with same key.
    {
      type: 'string',
      "default": [],
      readOnly: true
    };
    props.uiSchema = {
      'ui:widget': MultiSelect,
      'ui:options': {
        inline: widget && widget.endsWith('Row'),
        showScore: fillProps === null || fillProps === void 0 ? void 0 : fillProps.showScores
      }
    };
  };
});

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "xuWA1g",
  defaultMessage: [{
    "type": 0,
    "value": "Monivalinta"
  }]
});
exports.name = name;
var elementType = 'field';
exports.elementType = elementType;
var widgets = [{
  value: 'select',
  message: (0, _reactIntl.defineMessage)({
    id: "mrf29j",
    defaultMessage: [{
      "type": 0,
      "value": "Monivalintalista"
    }]
  })
}, {
  value: 'checkboxes',
  message: (0, _reactIntl.defineMessage)({
    id: "gQteSY",
    defaultMessage: [{
      "type": 0,
      "value": "Valintaruudut allekkain"
    }]
  })
}, {
  value: 'checkboxesRow',
  message: (0, _reactIntl.defineMessage)({
    id: "RTxVAk",
    defaultMessage: [{
      "type": 0,
      "value": "Valintaruudut rivissä"
    }]
  })
}];
exports.widgets = widgets;
//# sourceMappingURL=multiselect.cjs.map