import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _createForOfIteratorHelper from "@babel/runtime/helpers/createForOfIteratorHelper";
import { defineMessage } from 'react-intl';
import { ensureValueIsAvailable, sortChoices } from '../../utils';
import select from './selectV2';
import extendType from './_extendType';
import sift from 'sift';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "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 = ensureValueIsAvailable(widget, widgets);

  if (!filterDialog) {
    var _iterator = _createForOfIteratorHelper(choices.entries()),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var _step$value = _slicedToArray(_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 (sift((_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 (sift((_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__*/_jsx(_Fragment, {
    children: Widget ? /*#__PURE__*/_jsx(Widget, _objectSpread(_objectSpread({}, props), {}, {
      options: _objectSpread(_objectSpread({}, props.options), {}, {
        enumOptions: props.options.enumOptions ? props.options.enumOptions : [],
        enumDisabled: disabled
      })
    })) : null
  });
}

export default extendType(select, 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 = 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
      }
    };
  };
});
export var name = defineMessage({
  id: "xuWA1g",
  defaultMessage: [{
    "type": 0,
    "value": "Monivalinta"
  }]
});
export var elementType = 'field';
export var widgets = [{
  value: 'select',
  message: defineMessage({
    id: "mrf29j",
    defaultMessage: [{
      "type": 0,
      "value": "Monivalintalista"
    }]
  })
}, {
  value: 'checkboxes',
  message: defineMessage({
    id: "gQteSY",
    defaultMessage: [{
      "type": 0,
      "value": "Valintaruudut allekkain"
    }]
  })
}, {
  value: 'checkboxesRow',
  message: defineMessage({
    id: "RTxVAk",
    defaultMessage: [{
      "type": 0,
      "value": "Valintaruudut rivissä"
    }]
  })
}];
//# sourceMappingURL=multiselect.js.map