import React__default, { useContext, useState, useRef, useImperativeHandle, useMemo } from "react";
import cs from "../_util/classNames.js";
import Search from "./search.js";
import TextArea from "./textarea.js";
import Password from "./password.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import { isUndefined, isString, isObject } from "../_util/is.js";
import useMergeValue from "../_util/hooks/useMergeValue.js";
import InputComponent from "./input-element.js";
import Group from "./group.js";
import { contains } from "../_util/dom.js";
import useMergeProps from "../_util/hooks/useMergeProps.js";
import { jsxs, jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__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 __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var keepFocus = function(e) {
  e.target.tagName !== "INPUT" && e.preventDefault();
};
var inputAddon = function(className, node, style, onClick) {
  if (style === void 0) {
    style = {};
  }
  return node ? /* @__PURE__ */ jsx("span", {
    style,
    className,
    onClick,
    children: node
  }) : null;
};
function formatValue(value, maxLength) {
  var str = value !== null && !isUndefined(value) && !isString(value) ? String(value) : value || "";
  if (maxLength) {
    return str.slice(0, maxLength);
  }
  return str;
}
function Input(baseProps, ref) {
  var _a, _b, _c;
  var _d = useContext(ConfigContext), getPrefixCls = _d.getPrefixCls, ctxSize = _d.size, componentConfig = _d.componentConfig, rtl = _d.rtl;
  var props = useMergeProps(baseProps, {}, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Input);
  var className = props.className, style = props.style, addBefore = props.addBefore, addAfter = props.addAfter, suffix = props.suffix, prefix = props.prefix, beforeStyle = props.beforeStyle, afterStyle = props.afterStyle, height = props.height, disabled = props.disabled, maxLength = props.maxLength, showWordLimit = props.showWordLimit, allowClear = props.allowClear;
  var trueMaxLength = isObject(maxLength) ? maxLength.length : maxLength;
  var mergedMaxLength = isObject(maxLength) && maxLength.errorOnly ? void 0 : trueMaxLength;
  var _e = __read(useState(false), 2), focus = _e[0], setFocus = _e[1];
  var inputRef = useRef();
  var inputWrapperRef = useRef();
  var _f = __read(useMergeValue("", {
    defaultValue: "defaultValue" in props ? formatValue(props.defaultValue, mergedMaxLength) : void 0,
    value: "value" in props ? formatValue(props.value, mergedMaxLength) : void 0
  }), 2), value = _f[0], setValue = _f[1];
  useImperativeHandle(ref, function() {
    return inputRef.current;
  }, []);
  var onChange = function(value2, e) {
    if (!("value" in props)) {
      setValue(value2);
    }
    props.onChange && props.onChange(value2, e);
  };
  var prefixCls = getPrefixCls("input");
  var size = props.size || ctxSize;
  var isCustomHeight = "height" in props;
  var suffixElement = suffix;
  var valueLength = value ? value.length : 0;
  var lengthError = useMemo(function() {
    if (!mergedMaxLength && trueMaxLength) {
      return valueLength > trueMaxLength;
    }
    return false;
  }, [valueLength, trueMaxLength, mergedMaxLength]);
  if (trueMaxLength && showWordLimit) {
    var _g = __read(rtl ? [trueMaxLength, valueLength] : [valueLength, trueMaxLength], 2), leftWord = _g[0], rightWord = _g[1];
    suffixElement = /* @__PURE__ */ jsxs("span", {
      className: cs(prefixCls + "-word-limit", (_a = {}, _a[prefixCls + "-word-limit-error"] = lengthError, _a)),
      children: [leftWord, "/", rightWord]
    });
  }
  var classnames = cs(prefixCls + "-group-wrapper", prefixCls + "-group-wrapper-" + size, (_b = {}, _b[prefixCls + "-custom-height"] = isCustomHeight, _b[prefixCls + "-has-suffix"] = suffixElement, _b[prefixCls + "-group-wrapper-disabled"] = disabled, _b[prefixCls + "-group-wrapper-rtl"] = rtl, _b), className);
  var needWrapper = addBefore || addAfter || suffixElement || prefix;
  var inputElement = /* @__PURE__ */ jsx(InputComponent, {
    ...__assign({
      ref: inputRef
    }, props, {
      onFocus: function(e) {
        setFocus(true);
        props.onFocus && props.onFocus(e);
      },
      onBlur: function(e) {
        setFocus(false);
        props.onBlur && props.onBlur(e);
      },
      onChange,
      prefixCls,
      value,
      hasParent: !!needWrapper || allowClear,
      size
    })
  });
  var innerWrapperClassnames = cs(prefixCls + "-inner-wrapper", (_c = {}, _c[prefixCls + "-inner-wrapper-error"] = props.error || lengthError, _c[prefixCls + "-inner-wrapper-disabled"] = disabled, _c[prefixCls + "-inner-wrapper-focus"] = focus, _c[prefixCls + "-inner-wrapper-has-prefix"] = prefix, _c[prefixCls + "-inner-wrapper-" + size] = size, _c[prefixCls + "-clear-wrapper"] = allowClear, _c[prefixCls + "-inner-wrapper-rtl"] = rtl, _c));
  return needWrapper ? /* @__PURE__ */ jsx("div", {
    className: classnames,
    style: __assign(__assign({}, style), isCustomHeight ? {
      height
    } : {}),
    children: /* @__PURE__ */ jsxs("span", {
      className: prefixCls + "-group",
      children: [inputAddon(prefixCls + "-group-addbefore", addBefore, beforeStyle), /* @__PURE__ */ jsxs("span", {
        className: innerWrapperClassnames,
        ref: inputWrapperRef,
        onMouseDown: function(e) {
          if (e.target.tagName !== "INPUT") {
            if (inputWrapperRef.current && contains(inputWrapperRef.current, e.target)) {
              e.preventDefault();
            }
          }
        },
        onClick: function(e) {
          if (inputWrapperRef.current && contains(inputWrapperRef.current, e.target)) {
            inputRef.current && inputRef.current.focus();
          }
        },
        children: [inputAddon(prefixCls + "-group-prefix", prefix), inputElement, inputAddon(prefixCls + "-group-suffix", suffixElement)]
      }), inputAddon(prefixCls + "-group-addafter", addAfter, afterStyle)]
    })
  }) : allowClear ? /* @__PURE__ */ jsx("span", {
    className: cs(className, innerWrapperClassnames),
    style: __assign(__assign({}, style), isCustomHeight ? {
      height
    } : {}),
    onMouseDown: keepFocus,
    onClick: function() {
      inputRef.current && inputRef.current.focus();
    },
    children: inputElement
  }) : inputElement;
}
var InputElement = React__default.forwardRef(Input);
InputElement.displayName = "Input";
InputElement.Search = Search;
InputElement.TextArea = TextArea;
InputElement.Password = Password;
InputElement.Group = Group;
const Input$1 = InputElement;
export {
  Input$1 as default,
  formatValue
};
//# sourceMappingURL=input.js.map
