var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import useClickAway from 'react-use/lib/useClickAway';
import useRegistInputEnterEvent from "../../hooks/useRegistInputEnterEvent";
import IconButton from "../button/IconButton";
import Icon from "../icon";
import "./style/input.css";
var prefixCls = 'dmc-input';
export var Input = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _a, _b;

  var type = props.type,
      size = props.size,
      status = props.status,
      value = props.value,
      inputType = props.inputType,
      defaultValue = props.defaultValue,
      disabled = props.disabled,
      className = props.className,
      block = props.block,
      onChange = props.onChange,
      onPressEnter = props.onPressEnter,
      onFocus = props.onFocus,
      onBlur = props.onBlur,
      allowClear = props.allowClear,
      suffix = props.suffix,
      prefix = props.prefix,
      prefixIcon = props.prefixIcon,
      suffixIcon = props.suffixIcon,
      prefixAddon = props.prefixAddon,
      suffixAddon = props.suffixAddon,
      centered = props.centered,
      autoFocus = props.autoFocus,
      style = props.style,
      inputStyle = props.inputStyle,
      onPrefixClick = props.onPrefixClick,
      onSuffixClick = props.onSuffixClick,
      restProps = __rest(props, ["type", "size", "status", "value", "inputType", "defaultValue", "disabled", "className", "block", "onChange", "onPressEnter", "onFocus", "onBlur", "allowClear", "suffix", "prefix", "prefixIcon", "suffixIcon", "prefixAddon", "suffixAddon", "centered", "autoFocus", "style", "inputStyle", "onPrefixClick", "onSuffixClick"]);

  var _c = React.useState(defaultValue !== null && defaultValue !== void 0 ? defaultValue : ''),
      unControlVal = _c[0],
      setUnControlVal = _c[1];

  var isControl = ('value' in props);
  var val = isControl ? value !== null && value !== void 0 ? value : '' : unControlVal;

  var _d = React.useState(false),
      isFocus = _d[0],
      setIsFocus = _d[1];

  var wrapperRef = React.useRef(null);
  var inputRef = React.useRef(null);
  React.useImperativeHandle(ref, function () {
    return {
      dom: wrapperRef.current,
      input: inputRef.current,
      focus: function focus() {
        inputRef.current && inputRef.current.focus();
      },
      blur: function blur() {
        inputRef.current && inputRef.current.blur();
      },
      select: function select() {
        inputRef.current && inputRef.current.select();
      }
    };
  });
  var wrapperCls = classNames(prefixCls, (_a = {}, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-" + size] = size, _a[prefixCls + "-" + status + "-status"] = status, _a[prefixCls + "-block"] = block, _a[prefixCls + "-focused"] = isFocus, _a[prefixCls + "-disabled"] = disabled, _a), className);
  var inputCls = classNames(prefixCls + "-input", (_b = {}, _b[prefixCls + "-input-centered"] = centered, _b));
  useRegistInputEnterEvent(inputRef, onPressEnter);

  var handleClearValue = function handleClearValue() {
    var _a;

    !isControl && setUnControlVal('');

    if (onChange) {
      var event_1 = {
        target: {},
        currentTarget: {}
      };
      event_1.target.value = '';
      event_1.currentTarget.value = '';
      onChange(event_1);
    }

    (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
  };

  React.useEffect(function () {
    if (autoFocus) {
      inputRef.current && inputRef.current.focus();
    }
  }, [autoFocus]);

  var handleChange = function handleChange(e) {
    !isControl && setUnControlVal(e.target.value);
    onChange && onChange(e);
  };

  var handleFocus = function handleFocus(e) {
    setIsFocus(true);
    onFocus && onFocus(e);
  };

  var handleBlur = function handleBlur(e) {
    onBlur && onBlur(e);
  };

  useClickAway(wrapperRef, function () {
    setIsFocus(false);
  });
  var prefixNode = prefixAddon ? _jsx("div", __assign({
    className: prefixCls + "-prefix-addon",
    onClick: onPrefixClick
  }, {
    children: prefixAddon
  }), void 0) : prefixIcon ? _jsx(Icon, {
    icon: prefixIcon,
    className: prefixCls + "-prefix-icon",
    onClick: onPrefixClick
  }, void 0) : prefix ? _jsx("div", __assign({
    className: prefixCls + "-prefix",
    onClick: onPrefixClick
  }, {
    children: prefix
  }), void 0) : null;
  var suffixNode = suffixAddon ? _jsx("div", __assign({
    className: prefixCls + "-suffix-addon",
    onClick: onSuffixClick
  }, {
    children: suffixAddon
  }), void 0) : suffixIcon ? _jsx(Icon, {
    icon: suffixIcon,
    className: prefixCls + "-suffix-icon",
    onClick: onSuffixClick
  }, void 0) : suffix ? _jsx("div", __assign({
    className: prefixCls + "-suffix",
    onClick: onSuffixClick
  }, {
    children: suffix
  }), void 0) : allowClear && !disabled && val ? _jsx(IconButton, {
    icon: "close-2",
    type: "only-icon",
    onClick: handleClearValue
  }, void 0) : null;
  return _jsxs("div", __assign({
    className: wrapperCls,
    style: style,
    ref: wrapperRef
  }, {
    children: [prefixNode, _jsx("input", __assign({}, restProps, {
      type: inputType,
      ref: inputRef,
      disabled: disabled,
      value: val,
      onChange: handleChange,
      onFocus: handleFocus,
      onBlur: handleBlur,
      className: inputCls,
      style: inputStyle
    }), void 0), suffixNode]
  }), void 0);
});
Input.defaultProps = {
  inputType: 'text',
  allowClear: true
};
Input.displayName = 'Input';
export default Input;
var InputMemo = /*#__PURE__*/React.memo(Input);
InputMemo.displayName = 'InputMemo';
export { InputMemo };

