"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

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

var classnames_1 = __importDefault(require("classnames"));

var useClickAway_1 = __importDefault(require("react-use/lib/useClickAway"));

var useRegistInputEnterEvent_1 = __importDefault(require("../../hooks/useRegistInputEnterEvent"));

var IconButton_1 = __importDefault(require("../button/IconButton"));

var icon_1 = __importDefault(require("../icon"));

require("./style/input.css");

var prefixCls = 'dmc-input';
exports.Input = 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_1["default"](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_1["default"](prefixCls + "-input", (_b = {}, _b[prefixCls + "-input-centered"] = centered, _b));
  useRegistInputEnterEvent_1["default"](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_1["default"](wrapperRef, function () {
    setIsFocus(false);
  });
  var prefixNode = prefixAddon ? jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-prefix-addon",
    onClick: onPrefixClick
  }, {
    children: prefixAddon
  }), void 0) : prefixIcon ? jsx_runtime_1.jsx(icon_1["default"], {
    icon: prefixIcon,
    className: prefixCls + "-prefix-icon",
    onClick: onPrefixClick
  }, void 0) : prefix ? jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-prefix",
    onClick: onPrefixClick
  }, {
    children: prefix
  }), void 0) : null;
  var suffixNode = suffixAddon ? jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-suffix-addon",
    onClick: onSuffixClick
  }, {
    children: suffixAddon
  }), void 0) : suffixIcon ? jsx_runtime_1.jsx(icon_1["default"], {
    icon: suffixIcon,
    className: prefixCls + "-suffix-icon",
    onClick: onSuffixClick
  }, void 0) : suffix ? jsx_runtime_1.jsx("div", __assign({
    className: prefixCls + "-suffix",
    onClick: onSuffixClick
  }, {
    children: suffix
  }), void 0) : allowClear && !disabled && val ? jsx_runtime_1.jsx(IconButton_1["default"], {
    icon: "close-2",
    type: "only-icon",
    onClick: handleClearValue
  }, void 0) : null;
  return jsx_runtime_1.jsxs("div", __assign({
    className: wrapperCls,
    style: style,
    ref: wrapperRef
  }, {
    children: [prefixNode, jsx_runtime_1.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);
});
exports.Input.defaultProps = {
  inputType: 'text',
  allowClear: true
};
exports.Input.displayName = 'Input';
exports["default"] = exports.Input;
var InputMemo = React.memo(exports.Input);
exports.InputMemo = InputMemo;
InputMemo.displayName = 'InputMemo';

