"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ANY_CODE = undefined;

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _reactJsonschemaForm = require("react-jsonschema-form");

var _reactJsonschemaForm2 = _interopRequireDefault(_reactJsonschemaForm);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var ANY_CODE = exports.ANY_CODE = "any";

var toProperties = function toProperties(modifiers) {
  var properties = modifiers.reduce(function (properties, _ref) {
    var name = _ref.name,
        options = _ref.options;

    var enums = options.map(function (_ref2) {
      var code = _ref2.code;
      return code;
    });
    var enumNames = options.map(function (_ref3) {
      var title = _ref3.title;
      return title;
    });
    properties[name] = {
      type: "string",
      enum: enums,
      enumNames: enumNames
    };
    return properties;
  }, {});
  return withDefaults(properties);
};

var withDefaults = function withDefaults(properties) {
  Object.keys(properties).forEach(function (field) {
    var conf = properties[field];
    conf.enum.unshift(ANY_CODE);
    conf.enumNames.unshift(ANY_CODE);
    conf.default = ANY_CODE;
  });
  return properties;
};

var toUiSchema = function toUiSchema(properties) {
  return Object.keys(properties).reduce(function (uiSchema, field) {
    uiSchema[field] = {
      classNames: "col-md-3",
      "ui:widget": "radio"
    };
    return uiSchema;
  }, {});
};

var Modifiers = function Modifiers(_ref4) {
  var modifiers = _ref4.modifiers,
      activeModifiers = _ref4.activeModifiers,
      _onChange = _ref4.onChange;

  var schema = {
    type: "object",
    properties: toProperties(modifiers)
  };
  var uiSchema = toUiSchema(schema.properties);

  return _react2.default.createElement(
    _reactJsonschemaForm2.default,
    {
      schema: schema,
      uiSchema: uiSchema,
      formData: activeModifiers,
      onChange: function onChange(_ref5) {
        var formData = _ref5.formData;
        return _onChange(formData);
      } },
    _react2.default.createElement("div", null)
  );
};

exports.default = Modifiers;