function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }

/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides';
import { getSharedProps } from './utils';
import BaseInput from './base-input';
import { Root as StyledRoot, InputEnhancer as StyledInputEnhancer } from './styled-components';
import { SIZE, ADJOINED, ENHANCER_POSITION } from './constants';

class Input extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      isFocused: this.props.autoFocus && !this.props.readOnly || false
    });

    _defineProperty(this, "onFocus", e => {
      if (!this.props.readOnly) {
        this.setState({
          isFocused: true
        });
        this.props.onFocus(e);
      }
    });

    _defineProperty(this, "onBlur", e => {
      if (!this.props.readOnly) {
        this.setState({
          isFocused: false
        });
        this.props.onBlur(e);
      }
    });
  }

  render() {
    const {
      startEnhancer,
      endEnhancer,
      overrides: {
        Root: RootOverride,
        StartEnhancer: StartEnhancerOverride,
        EndEnhancer: EndEnhancerOverride,
        ...restOverrides
      },
      ...restProps
    } = this.props;
    const [Root, rootProps] = getOverrides(RootOverride, StyledRoot);
    const [StartEnhancer, startEnhancerProps] = getOverrides(StartEnhancerOverride, StyledInputEnhancer);
    const [EndEnhancer, endEnhancerProps] = getOverrides(EndEnhancerOverride, StyledInputEnhancer);
    const sharedProps = getSharedProps(this.props, this.state);

    if (process.env.NODE_ENV !== "production") {
      if (this.props.error && this.props.positive) {
        // eslint-disable-next-line no-console
        console.warn(`[Input] \`error\` and \`positive\` are both set to \`true\`. \`error\` will take precedence but this may not be what you want.`);
      }
    }

    return /*#__PURE__*/React.createElement(Root, _extends({
      "data-baseweb": "input"
    }, sharedProps, rootProps, {
      $adjoined: getAdjoinedProp(startEnhancer, endEnhancer),
      $hasIconTrailing: this.props.clearable || this.props.type == 'password'
    }), isEnhancer(startEnhancer) && /*#__PURE__*/React.createElement(StartEnhancer, _extends({}, sharedProps, startEnhancerProps, {
      $position: ENHANCER_POSITION.start
    }), typeof startEnhancer === 'function' ? startEnhancer(sharedProps) : startEnhancer), /*#__PURE__*/React.createElement(BaseInput, _extends({}, restProps, {
      overrides: restOverrides,
      adjoined: getAdjoinedProp(startEnhancer, endEnhancer),
      onFocus: this.onFocus,
      onBlur: this.onBlur
    })), isEnhancer(endEnhancer) && /*#__PURE__*/React.createElement(EndEnhancer, _extends({}, sharedProps, endEnhancerProps, {
      $position: ENHANCER_POSITION.end
    }), typeof endEnhancer === 'function' ? endEnhancer(sharedProps) : endEnhancer));
  }

}

_defineProperty(Input, "defaultProps", {
  autoComplete: 'on',
  autoFocus: false,
  disabled: false,
  name: '',
  onBlur: () => {},
  onFocus: () => {},
  overrides: {},
  required: false,
  size: SIZE.default,
  startEnhancer: null,
  endEnhancer: null,
  clearable: false,
  type: 'text',
  readOnly: false
});

function getAdjoinedProp(startEnhancer, endEnhancer) {
  if (isEnhancer(startEnhancer) && isEnhancer(endEnhancer)) {
    return ADJOINED.both;
  } else if (isEnhancer(startEnhancer)) {
    return ADJOINED.left;
  } else if (isEnhancer(endEnhancer)) {
    return ADJOINED.right;
  }

  return ADJOINED.none;
}

function isEnhancer(enhancer) {
  return Boolean(enhancer || enhancer === 0);
}

export default Input;
export { default as StatefulContainer } from './stateful-container';