"use strict";

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

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

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

var _reactSelect = _interopRequireDefault(require("react-select"));

var _map = _interopRequireDefault(require("lodash/map"));

var _find = _interopRequireDefault(require("lodash/find"));

var _validation = _interopRequireDefault(require("../../helpers/validation"));

var _TooltipComponent = _interopRequireDefault(require("../TooltipComponent"));

var _multiSelectCustomControl = require("../multiselect/multiSelectCustomControl");

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

/** SelectField Component */
var SelectField =
/*#__PURE__*/
function (_React$Component) {
  _inherits(SelectField, _React$Component);

  function SelectField(props) {
    var _this;

    _classCallCheck(this, SelectField);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(SelectField).call(this, props));
    _this.state = {
      value: props.attributes.selected,
      errorText: props.attributes.errorText || '',
      selectedOption: _this.getProperValueForReactSelect()
    };
    _this.onChange = _this.onChange.bind(_assertThisInitialized(_this));
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(SelectField, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      this.setState({
        value: props.attributes.selected,
        errorText: props.attributes.errorText || '',
        selectedOption: this.getProperValueForReactSelect()
      });
    }
  }, {
    key: "validate",
    value: function validate(value) {
      var isValid = true;
      var props = this.props;

      if (props.rules && props.rules.validation) {
        for (var i = 0; i < props.rules.validation.length; i += 1) {
          var data = props.rules.validation[i];
          isValid = _validation.default[data.rule](value, data.value);

          if (!isValid) {
            return {
              isValid: false,
              message: data.message
            };
          }
        }
      }

      return {
        isValid: true,
        message: ''
      };
    }
  }, {
    key: "onChange",
    value: function onChange() {
      var props = this.props;

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

      var value = args[0].target.value;
      var validator = this.validate(value);

      if (!validator.isValid) {
        this.setState({
          errorText: validator.message,
          value: value
        });
      } else {
        this.setState({
          errorText: '',
          value: value
        });
      }

      if (typeof props.onChange === 'function') {
        props.onChange(props.control, '', '', value);
      }
    }
  }, {
    key: "handleChange",
    value: function handleChange(selectedOption) {
      var props = this.props;
      this.setState({
        selectedOption: selectedOption
      });

      if (typeof props.onChange === 'function') {
        props.onChange(props.control, '', '', (0, _map.default)(selectedOption, 'value'));
      }
    }
  }, {
    key: "getProperValueForReactSelect",
    value: function getProperValueForReactSelect() {
      var props = this.props;
      var selectedOption = [];
      var options = props.control.options;
      var o = props.attributes.selected || props.attributes.value;
      (0, _map.default)(o, function (value) {
        var f = (0, _find.default)(options, {
          value: value
        });
        f && selectedOption.push({
          value: f.value,
          label: f.primaryText || f.label || ''
        });
      });
      return selectedOption;
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;
      var SELECTFIELD = props.library[props.component];
      var FORMCONTROL = props.library.FormControl;
      var INPUTLABEL = props.library.InputLabel;
      var FORMHELPERTEXT = props.library.FormHelperText;
      var OPTION = props.library[props.option];
      var _this$state = this.state,
          selectedOption = _this$state.selectedOption,
          value = _this$state.value,
          errorText = _this$state.errorText;
      return _react.default.createElement("div", {
        style: {
          display: 'flex'
        }
      }, props.attributes.isMulti ? _react.default.createElement("div", {
        style: Object.assign({}, {
          width: '100%',
          marginTop: '25px',
          marginRight: '5px',
          maxWidth: '100%'
        }, props.attributes.style)
      }, _react.default.createElement(_reactSelect.default, _extends({
        menuPlacement: props.attributes.menuPlacement || 'auto',
        captureMenuScroll: props.attributes.captureMenuScroll || false,
        menuShouldScrollIntoView: props.attributes.menuShouldScrollIntoView || false,
        components: props.attributes.enablefloatingLabel ? {
          Control: _multiSelectCustomControl.ControlComponent
        } : null,
        value: selectedOption,
        onChange: this.handleChange,
        isMulti: true
      }, props.attributes, {
        options: props.control.options.map(function (option) {
          return {
            value: option.value,
            label: option.primaryText || option.label || ''
          };
        }),
        styles: SelectField.styles
      }))) : _react.default.createElement(FORMCONTROL, props.attributes.formControl, _react.default.createElement(INPUTLABEL, {
        htmlFor: props.control.id
      }, props.attributes.label), _react.default.createElement(SELECTFIELD, _extends({}, props.attributes, {
        inputProps: {
          name: props.control.id,
          id: props.control.id
        },
        value: value,
        errorText: errorText,
        onChange: this.onChange
      }), props.control.options.map(function (option, index) {
        var primaryText = option.primaryText;
        option = Object.assign({}, option, {
          primaryText: null
        });
        return _react.default.createElement(OPTION, _extends({}, option, {
          key: index
        }), primaryText);
      })), _react.default.createElement(FORMHELPERTEXT, props.attributes.errorStyle, props.attributes.errorText)), props.attributes.tooltip && _react.default.createElement(_TooltipComponent.default, {
        tooltip: props.attributes.tooltip
      }));
    }
  }], [{
    key: "styles",
    get: function get() {
      return {
        menu: function menu(base) {
          return Object.assign({}, base, {
            zIndex: '20000 !important'
          });
        }
      };
    }
  }]);

  return SelectField;
}(_react.default.Component);

SelectField.propTypes = process.env.NODE_ENV !== "production" ? {
  library: _propTypes.default.object,
  component: _propTypes.default.string.isRequired,
  attributes: _propTypes.default.object,
  control: _propTypes.default.object,
  option: _propTypes.default.string.isRequired,
  rules: _propTypes.default.object,
  onChange: _propTypes.default.func
} : {};
SelectField.defaultProps = {
  library: null,
  attributes: null,
  control: null,
  rules: null,
  onChange: null
};
var _default = SelectField;
exports.default = _default;