"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 React = _interopRequireWildcard(require("react"));

var _Box = _interopRequireDefault(require("@mui/material/Box"));

var _MenuItem = _interopRequireDefault(require("@mui/material/MenuItem"));

var _FormControl = _interopRequireDefault(require("@mui/material/FormControl"));

var _Select = _interopRequireDefault(require("@mui/material/Select"));

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

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

var _core = require("@material-ui/core");

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

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 = (0, _utils.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__*/(0, _jsxRuntime.jsxs)("div", {
    style: {
      display: 'flex'
    },
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      style: {
        flex: '1 1'
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Box["default"], {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormControl["default"], {
          variant: "standard",
          sx: {
            minWidth: '100%'
          },
          disabled: options.disabled || options.readonly,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Select["default"], {
            id: id,
            value: value || '',
            label: label,
            "aria-label": _rjsfCore.utils.generateAriaLabel(label, options, required),
            "aria-describedby": _rjsfCore.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__*/(0, _jsxRuntime.jsx)(_MenuItem["default"], {
                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__*/(0, _jsxRuntime.jsx)(_core.Typography, {
      children: scoreObject.meta.score
    })]
  });
}

var _default = function _default(_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 = (0, _utils.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) : (0, _utils.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
      }
    }
  };
};

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "r/LS4F",
  defaultMessage: [{
    "type": 0,
    "value": "Valinta"
  }]
});
exports.name = name;
var elementType = 'field';
exports.elementType = elementType;
var widgets = [{
  value: 'select',
  message: (0, _reactIntl.defineMessage)({
    id: "1GAb3J",
    defaultMessage: [{
      "type": 0,
      "value": "Valintalista"
    }]
  })
}, {
  value: 'radio',
  message: (0, _reactIntl.defineMessage)({
    id: "jxSL/l",
    defaultMessage: [{
      "type": 0,
      "value": "Radiopainikkeet allekkain"
    }]
  })
}, {
  value: 'radioRow',
  message: (0, _reactIntl.defineMessage)({
    id: "4SG/Av",
    defaultMessage: [{
      "type": 0,
      "value": "Radiopainikkeet rivissä"
    }]
  })
}];
exports.widgets = widgets;
//# sourceMappingURL=selectV2.cjs.map