"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

var React = _interopRequireWildcard(require("react"));

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

var _Text = _interopRequireDefault(require("../text/Text"));

var _Flex = _interopRequireDefault(require("../flex/Flex"));

var _excluded = ["type", "size", "color", "fullWidth", "withIcon", "value", "valid", "invalid", "className", "setInputRef", "errorMessage", "disabled"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var TYPE = {
  BUTTON: 'button',
  COLOR: 'color',
  DATE: 'date',
  DATETIME_LOCAL: 'datetime-local',
  EMAIL: 'email',
  FILE: 'file',
  HIDDEN: 'hidden',
  IMAGE: 'image',
  MONTH: 'month',
  NUMBER: 'number',
  PASSWORD: 'password',
  RANGE: 'range',
  RESET: 'reset',
  SEARCH: 'search',
  SUBMIT: 'submit',
  TEL: 'tel',
  TEXT: 'text',
  TIME: 'time',
  URL: 'url',
  WEEK: 'week'
};
exports.TYPE = TYPE;
var SIZE = {
  L: 'l',
  M: 'm',
  S: 's'
};
exports.SIZE = SIZE;
var COLOR = {
  DEFAULT: 'default',
  WHITE: 'white'
};
exports.COLOR = COLOR;

var Input = function Input(props) {
  var _classnames;

  var _props$type = props.type,
      type = _props$type === void 0 ? 'text' : _props$type,
      _props$size = props.size,
      size = _props$size === void 0 ? SIZE.M : _props$size,
      _props$color = props.color,
      color = _props$color === void 0 ? COLOR.DEFAULT : _props$color,
      fullWidth = props.fullWidth,
      withIcon = props.withIcon,
      value = props.value,
      valid = props.valid,
      invalid = props.invalid,
      className = props.className,
      setInputRef = props.setInputRef,
      errorMessage = props.errorMessage,
      disabled = props.disabled,
      additionalProps = _objectWithoutProperties(props, _excluded);

  if (valid === true && invalid === true) {
    throw {
      name: 'WrongValidation',
      message: 'Input can be either valid or invalid!'
    };
  }

  var inputClass = (0, _classnames2.default)('sg-input', (_classnames = {}, _defineProperty(_classnames, "sg-input--".concat(String(size)), size !== SIZE.M), _defineProperty(_classnames, "sg-input--".concat(String(color)), color !== COLOR.DEFAULT), _defineProperty(_classnames, 'sg-input--valid', valid), _defineProperty(_classnames, 'sg-input--invalid', invalid), _defineProperty(_classnames, 'sg-input--full-width', fullWidth), _defineProperty(_classnames, 'sg-input--with-icon', withIcon), _classnames), className);
  var wrapperClass = (0, _classnames2.default)('sg-input__wrapper', {
    'sg-input__wrapper--full-width': fullWidth,
    'sg-input__wrapper--disabled': disabled
  });
  var errorMessageDisplayed = invalid === true && errorMessage !== undefined && errorMessage !== '';
  return /*#__PURE__*/React.createElement("div", {
    className: wrapperClass
  }, /*#__PURE__*/React.createElement("input", _extends({}, additionalProps, {
    type: type,
    ref: setInputRef,
    className: inputClass,
    value: value,
    disabled: disabled
  })), errorMessageDisplayed && /*#__PURE__*/React.createElement(_Flex.default, {
    marginTop: "xxs",
    marginLeft: size === 'l' ? 'm' : 's',
    marginRight: size === 'l' ? 'm' : 's'
  }, /*#__PURE__*/React.createElement(_Text.default, {
    size: size === 'l' ? 'small' : 'xsmall',
    color: disabled ? 'text-gray-50' : 'text-red-60'
  }, errorMessage)));
};

var _default = Input;
exports.default = _default;