import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onChange", "options", "value"];
import { FormHelperText, Typography } from '@material-ui/core';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';
import { defineMessage, useIntl } from 'react-intl';
import { ensureValueIsAvailable } from '../../utils';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";

var Switch = function Switch(_ref) {
  var _options$element, _options$element2;

  var _onChange = _ref.onChange,
      options = _ref.options,
      schema = _ref.schema,
      value = _ref.value;
  var title = options.element.required ? schema.title + ' *' : schema.title;
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(Typography, {
      component: "p",
      variant: "subtitle1",
      children: title
    }), (options === null || options === void 0 ? void 0 : (_options$element = options.element) === null || _options$element === void 0 ? void 0 : _options$element.list) ? /*#__PURE__*/_jsx(_Fragment, {}) : /*#__PURE__*/_jsx(FormHelperText, {
      component: "span",
      variant: "contained",
      children: options === null || options === void 0 ? void 0 : (_options$element2 = options.element) === null || _options$element2 === void 0 ? void 0 : _options$element2.description
    }), /*#__PURE__*/_jsx(ToggleButtonGroup, {
      exclusive: true,
      value: value,
      onChange: function onChange(event, value) {
        value === null ? _onChange(undefined) : _onChange(value);
      },
      children: options.enumOptions.map(function (_ref2, index) {
        var label = _ref2.label,
            value = _ref2.value;
        return /*#__PURE__*/_jsx(ToggleButton, {
          value: value,
          disabled: schema.readOnly || options.readonly,
          children: label
        }, index);
      })
    })]
  });
};

var NOTHING = 'nothing';

var SwitchWithEmptyOption = function SwitchWithEmptyOption(_ref3) {
  var _onChange2 = _ref3.onChange,
      options = _ref3.options,
      value = _ref3.value,
      other = _objectWithoutProperties(_ref3, _excluded);

  return /*#__PURE__*/_jsx(Switch, _objectSpread({
    onChange: function onChange(value) {
      return _onChange2(value === NOTHING ? undefined : value);
    },
    options: _objectSpread(_objectSpread({}, options), {}, {
      enumOptions: [{
        label: useIntl().formatMessage({
          id: "nk8wjQ",
          defaultMessage: [{
            "type": 0,
            "value": "Ei valintaa"
          }]
        }),
        value: NOTHING
      }].concat(_toConsumableArray(options.enumOptions))
    }),
    value: value !== null && value !== void 0 ? value : NOTHING
  }, other));
};

export default (function (_ref4) {
  var _switch$switchWithEmp;

  var config = _ref4.config;
  var defaults = config["default"],
      yes = config.yes,
      no = config.no;
  var widget = config.widget ? ensureValueIsAvailable(config.widget.endsWith('Row') ? config.widget.slice(0, -3) : config.widget, widgets) : undefined;
  return {
    schema: {
      type: 'boolean',
      enumNames: [yes, no],
      "default": defaults
    },
    uiSchema: {
      'ui:widget': (_switch$switchWithEmp = {
        "switch": Switch,
        switchWithEmptyOption: SwitchWithEmptyOption
      }[widget]) !== null && _switch$switchWithEmp !== void 0 ? _switch$switchWithEmp : widget,
      'ui:options': {
        inline: config.widget && config.widget.endsWith('Row')
      }
    }
  };
});
export var name = defineMessage({
  id: "yoBGY/",
  defaultMessage: [{
    "type": 0,
    "value": "Totuusarvo"
  }]
});
export var elementType = 'field';
export var widgets = [{
  value: 'checkbox',
  message: defineMessage({
    id: "cG6IFs",
    defaultMessage: [{
      "type": 0,
      "value": "Valintaruutu"
    }]
  })
}, // Not available: https://github.com/rjsf-team/react-jsonschema-form/issues/2326
// {
//   value: 'select',
//   message: defineMessage({
//     defaultMessage: '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ä"
    }]
  })
}, {
  value: 'switch',
  message: defineMessage({
    id: "eubY+W",
    defaultMessage: [{
      "type": 0,
      "value": "Painikkeet"
    }]
  })
}, {
  value: 'switchWithEmptyOption',
  message: defineMessage({
    id: "ZSBYCq",
    defaultMessage: [{
      "type": 0,
      "value": "Painikkeet tyhjällä valinnalla"
    }]
  })
}];
//# sourceMappingURL=boolean.js.map