import * as React from 'react';
import Box from '@mui/material/Box';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';
import { defineMessage } from 'react-intl';
import { ensureValueIsAvailable, sortChoices } from '../../utils';
import { Typography } from '@material-ui/core';
import { utils } from '@visma/rjsf-core';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";

function DefaultSelect(props) {
  var _options$element, _options$element2, _uiSchema$uiOptions, _uiSchema$uiOptions2;

  var id = props.id,
      options = props.options,
      value = props.value,
      _onChange = props.onChange,
      label = props.label,
      onBlur = props.onBlur,
      onFocus = props.onFocus,
      uiSchema = props.uiSchema,
      required = props.required,
      rawErrors = props.rawErrors;
  var choicesSorted = sortChoices(options === null || options === void 0 ? void 0 : (_options$element = options.element) === null || _options$element === void 0 ? void 0 : _options$element.choices, options === null || options === void 0 ? void 0 : (_options$element2 = options.element) === null || _options$element2 === void 0 ? void 0 : _options$element2.autoSort);
  var scoreObject = ((_uiSchema$uiOptions = uiSchema["ui:options"]) === null || _uiSchema$uiOptions === void 0 ? void 0 : _uiSchema$uiOptions.showScore) ? choicesSorted.find(function (choice) {
    return choice["enum"] === value;
  }) : undefined;
  return /*#__PURE__*/_jsxs("div", {
    style: {
      display: 'flex'
    },
    children: [/*#__PURE__*/_jsx("div", {
      style: {
        flex: '1 1'
      },
      children: /*#__PURE__*/_jsx(Box, {
        children: /*#__PURE__*/_jsx(FormControl, {
          variant: "standard",
          sx: {
            minWidth: '100%'
          },
          disabled: options.disabled || options.readonly,
          children: /*#__PURE__*/_jsx(Select, {
            id: id,
            value: value || '',
            label: label,
            "aria-label": utils.generateAriaLabel(label, options, required),
            "aria-describedby": utils.ariaDescribedBy(id, uiSchema, rawErrors),
            onBlur: onBlur,
            onFocus: onFocus,
            onChange: function onChange(e) {
              return _onChange(e.target.value);
            },
            role: "combobox",
            inputProps: {
              'aria-labelledby': id
            },
            children: choicesSorted === null || choicesSorted === void 0 ? void 0 : choicesSorted.map(function (choice) {
              return /*#__PURE__*/_jsx(MenuItem, {
                value: choice["enum"],
                children: choice.enumNames
              }, choice["enum"]);
            })
          })
        })
      })
    }), ((_uiSchema$uiOptions2 = uiSchema["ui:options"]) === null || _uiSchema$uiOptions2 === void 0 ? void 0 : _uiSchema$uiOptions2.showScore) && value && scoreObject && /*#__PURE__*/_jsx(Typography, {
      children: scoreObject.meta.score
    })]
  });
}

export default (function (_ref) {
  var _config$widget;

  var config = _ref.config,
      fillProps = _ref.fillProps;
  var choices = config.choices !== null ? config.choices : [];
  var defaults = config["default"],
      autocomplete = config.autocomplete,
      _config$autoSort = config.autoSort,
      autoSort = _config$autoSort === void 0 ? false : _config$autoSort;
  var choicesSorted = sortChoices(choices, autoSort);
  return {
    schema: choicesSorted.length || autocomplete ? {
      "default": defaults,
      "enum": choicesSorted.map(function (v, i) {
        var _v$enum;

        return (_v$enum = v === null || v === void 0 ? void 0 : v["enum"]) !== null && _v$enum !== void 0 ? _v$enum : String(i);
      }),
      enumNames: choicesSorted.map(function (v) {
        return typeof v === 'object' ? v.enumNames : v;
      }),
      type: 'string'
    } : {
      type: 'string',
      "default": '',
      readOnly: true
    },
    uiSchema: {
      'ui:placeholder': config.placeholder,
      'ui:enumDisabled': config.choicesDisabled,
      'ui:widget': (config === null || config === void 0 ? void 0 : config.type) === 'select' && (config.widget === undefined || config.widget === 'select') ? DefaultSelect : (config === null || config === void 0 ? void 0 : (_config$widget = config.widget) === null || _config$widget === void 0 ? void 0 : _config$widget.endsWith('Row')) ? config.widget.slice(0, -3) : ensureValueIsAvailable(config.widget, widgets),
      'ui:options': {
        inline: (config === null || config === void 0 ? void 0 : config.widget) && (config === null || config === void 0 ? void 0 : config.widget.endsWith('Row')),
        showScore: fillProps === null || fillProps === void 0 ? void 0 : fillProps.showScores
      }
    }
  };
});
export var name = defineMessage({
  id: "r/LS4F",
  defaultMessage: [{
    "type": 0,
    "value": "Valinta"
  }]
});
export var elementType = 'field';
export var widgets = [{
  value: 'select',
  message: defineMessage({
    id: "1GAb3J",
    defaultMessage: [{
      "type": 0,
      "value": "Valintalista"
    }]
  })
}, {
  value: 'radio',
  message: defineMessage({
    id: "jxSL/l",
    defaultMessage: [{
      "type": 0,
      "value": "Radiopainikkeet allekkain"
    }]
  })
}, {
  value: 'radioRow',
  message: defineMessage({
    id: "4SG/Av",
    defaultMessage: [{
      "type": 0,
      "value": "Radiopainikkeet rivissä"
    }]
  })
}];
//# sourceMappingURL=selectV2.js.map