"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.NChoice = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _renderUi = require("@pie-lib/render-ui");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _radioWithLabel = _interopRequireDefault(require("./radio-with-label"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var styles = function styles(theme) {
  return {
    group: {
      display: 'flex',
      flexWrap: 'wrap',
      paddingLeft: 0,
      marginTop: theme.spacing.unit
    },
    vertical: {
      flexDirection: 'column'
    }
  };
};

var RawNChoice = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(RawNChoice, _React$Component);

  var _super = _createSuper(RawNChoice);

  function RawNChoice() {
    var _this;

    (0, _classCallCheck2["default"])(this, RawNChoice);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleChange", function (event) {
      _this.props.onChange(event.currentTarget.value);
    });
    return _this;
  }

  (0, _createClass2["default"])(RawNChoice, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          header = _this$props.header,
          className = _this$props.className,
          classes = _this$props.classes,
          customLabel = _this$props.customLabel,
          opts = _this$props.opts,
          value = _this$props.value,
          direction = _this$props.direction;
      var preppedOpts = opts.map(function (o) {
        return typeof o === 'string' ? {
          label: o,
          value: o
        } : o;
      });
      var LabelComponent = customLabel || _radioWithLabel["default"];
      return /*#__PURE__*/_react["default"].createElement(_renderUi.InputContainer, {
        label: header,
        className: className
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(classes.group, classes[direction])
      }, preppedOpts.map(function (o, index) {
        return /*#__PURE__*/_react["default"].createElement(LabelComponent, {
          value: o.value,
          key: index,
          checked: o.value === value,
          onChange: _this2.handleChange,
          label: o.label
        });
      })));
    }
  }]);
  return RawNChoice;
}(_react["default"].Component);

(0, _defineProperty2["default"])(RawNChoice, "propTypes", {
  header: _propTypes["default"].string.isRequired,
  className: _propTypes["default"].string,
  customLabel: _propTypes["default"].func,
  opts: _propTypes["default"].array.isRequired,
  value: _propTypes["default"].string,
  onChange: _propTypes["default"].func.isRequired,
  direction: _propTypes["default"].oneOf(['horizontal', 'vertical']),
  classes: _propTypes["default"].object.isRequired
});
var NChoice = (0, _styles.withStyles)(styles)(RawNChoice);
exports.NChoice = NChoice;

var labelValue = _propTypes["default"].shape({
  label: _propTypes["default"].string,
  value: _propTypes["default"].string
});

var TwoChoice = /*#__PURE__*/function (_React$Component2) {
  (0, _inherits2["default"])(TwoChoice, _React$Component2);

  var _super2 = _createSuper(TwoChoice);

  function TwoChoice() {
    (0, _classCallCheck2["default"])(this, TwoChoice);
    return _super2.apply(this, arguments);
  }

  (0, _createClass2["default"])(TwoChoice, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          one = _this$props2.one,
          two = _this$props2.two,
          header = _this$props2.header,
          className = _this$props2.className,
          customLabel = _this$props2.customLabel,
          value = _this$props2.value,
          onChange = _this$props2.onChange;
      var opts = [one, two];
      return /*#__PURE__*/_react["default"].createElement(NChoice, {
        customLabel: customLabel,
        header: header,
        className: className,
        opts: opts,
        value: value,
        onChange: onChange
      });
    }
  }]);
  return TwoChoice;
}(_react["default"].Component);

(0, _defineProperty2["default"])(TwoChoice, "propTypes", {
  header: _propTypes["default"].string.isRequired,
  value: _propTypes["default"].string.isRequired,
  onChange: _propTypes["default"].func.isRequired,
  one: _propTypes["default"].oneOfType([labelValue, _propTypes["default"].string]),
  two: _propTypes["default"].oneOfType([labelValue, _propTypes["default"].string]),
  className: _propTypes["default"].string,
  customLabel: _propTypes["default"].func
});

var _default = (0, _styles.withStyles)(styles)(TwoChoice);

exports["default"] = _default;
//# sourceMappingURL=two-choice.js.map