"use strict";

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

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

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

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

var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));

var _lodash = require("lodash");

var _shortid = require("shortid");

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

var useStyles = (0, _styles.makeStyles)(function () {
  return {
    root: {
      display: "block !important",
      padding: "5px !important"
    }
  };
});

var SelectField = function SelectField(_ref) {
  var id = _ref.id,
      label = _ref.label,
      options = _ref.options,
      formik = _ref.formik,
      isRequired = _ref.isRequired;
  var classes = useStyles();
  var optionValues = options.values;

  if (options.values && !options.values[0].value) {
    optionValues = options.values.map(function (x) {
      return {
        value: x,
        label: x
      };
    });
  }

  if (options.limitByField && options.limitByValues && !(0, _lodash.isEmpty)(formik.values[options.limitByField])) {
    var includeValues = options.limitByValues[formik.values[options.limitByField]];
    optionValues = optionValues.filter(function (x) {
      return includeValues.indexOf(x.value) !== -1;
    });

    if (!optionValues.some(function (e) {
      return e.value === formik.values[id];
    }) && formik.values[id] !== "") {
      formik.values[id] = "";
    }
  }

  return _react["default"].createElement(_TextField["default"], {
    required: isRequired,
    select: true,
    SelectProps: {
      "native": true
    },
    InputLabelProps: {
      shrink: true
    },
    id: id,
    label: label,
    helperText: formik.touched[id] ? formik.errors[id] : "",
    error: formik.touched[id] && Boolean(formik.errors[id]),
    value: formik.values[id] || "",
    onChange: formik.handleChange,
    onBlur: function onBlur() {
      formik.touched[id] = true;
      formik.handleBlur(id);
    },
    inputProps: {
      name: id,
      id: id
    },
    fullWidth: true
  }, _react["default"].createElement("option", {
    value: "",
    key: "",
    disabled: true
  }, "Select option"), optionValues.map(function (option) {
    return _react["default"].createElement("option", {
      value: option.value,
      key: (0, _shortid.generate)(),
      className: classes.root
    }, option.label);
  }));
};

SelectField.propTypes = {
  id: _propTypes["default"].string,
  isRequired: _propTypes["default"].bool,
  label: _propTypes["default"].string,
  options: _propTypes["default"].shape({
    limitByField: _propTypes["default"].string,
    limitByValues: function limitByValues(props, propName) {
      if (props.limitByField !== undefined && props[propName] === undefined) {
        return new Error("limitByValues array is required when limitByField is set");
      }
    },
    values: _propTypes["default"].array
  }),
  formik: _propTypes["default"].shape({})
};
var _default = SelectField;
exports["default"] = _default;