"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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

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

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

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

var _ToggleButton = _interopRequireDefault(require("@material-ui/lab/ToggleButton"));

var _ToggleButtonGroup = _interopRequireDefault(require("@material-ui/lab/ToggleButtonGroup"));

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

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

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

var _excluded = ["onChange", "options", "value"];

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__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_core.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__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_core.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__*/(0, _jsxRuntime.jsx)(_ToggleButtonGroup["default"], {
      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__*/(0, _jsxRuntime.jsx)(_ToggleButton["default"], {
          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 = (0, _objectWithoutProperties2["default"])(_ref3, _excluded);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Switch, (0, _objectSpread2["default"])({
    onChange: function onChange(value) {
      return _onChange2(value === NOTHING ? undefined : value);
    },
    options: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, options), {}, {
      enumOptions: [{
        label: (0, _reactIntl.useIntl)().formatMessage({
          id: "nk8wjQ",
          defaultMessage: [{
            "type": 0,
            "value": "Ei valintaa"
          }]
        }),
        value: NOTHING
      }].concat((0, _toConsumableArray2["default"])(options.enumOptions))
    }),
    value: value !== null && value !== void 0 ? value : NOTHING
  }, other));
};

var _default = function _default(_ref4) {
  var _switch$switchWithEmp;

  var config = _ref4.config;
  var defaults = config["default"],
      yes = config.yes,
      no = config.no;
  var widget = config.widget ? (0, _utils.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')
      }
    }
  };
};

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "yoBGY/",
  defaultMessage: [{
    "type": 0,
    "value": "Totuusarvo"
  }]
});
exports.name = name;
var elementType = 'field';
exports.elementType = elementType;
var widgets = [{
  value: 'checkbox',
  message: (0, _reactIntl.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: (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ä"
    }]
  })
}, {
  value: 'switch',
  message: (0, _reactIntl.defineMessage)({
    id: "eubY+W",
    defaultMessage: [{
      "type": 0,
      "value": "Painikkeet"
    }]
  })
}, {
  value: 'switchWithEmptyOption',
  message: (0, _reactIntl.defineMessage)({
    id: "ZSBYCq",
    defaultMessage: [{
      "type": 0,
      "value": "Painikkeet tyhjällä valinnalla"
    }]
  })
}];
exports.widgets = widgets;
//# sourceMappingURL=boolean.cjs.map