"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const React = require("react");
const classNames = require("../_util/classNames.js");
const search = require("./search.js");
const textarea = require("./textarea.js");
const password = require("./password.js");
const index = require("../ConfigProvider/index.js");
const is = require("../_util/is.js");
const useMergeValue = require("../_util/hooks/useMergeValue.js");
const inputElement = require("./input-element.js");
const group = require("./group.js");
const dom = require("../_util/dom.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
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__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
    style,
    className,
    onClick,
    children: node
  }) : null;
};
function formatValue(value, maxLength) {
  var str = value !== null && !is.isUndefined(value) && !is.isString(value) ? String(value) : value || "";
  if (maxLength) {
    return str.slice(0, maxLength);
  }
  return str;
}
function Input(baseProps, ref) {
  var _a, _b, _c;
  var _d = React.useContext(index.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 = is.isObject(maxLength) ? maxLength.length : maxLength;
  var mergedMaxLength = is.isObject(maxLength) && maxLength.errorOnly ? void 0 : trueMaxLength;
  var _e = __read(React.useState(false), 2), focus = _e[0], setFocus = _e[1];
  var inputRef = React.useRef();
  var inputWrapperRef = React.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];
  React.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 = React.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__ */ emotionReactJsxRuntime_browser_esm.jsxs("span", {
      className: classNames(prefixCls + "-word-limit", (_a = {}, _a[prefixCls + "-word-limit-error"] = lengthError, _a)),
      children: [leftWord, "/", rightWord]
    });
  }
  var classnames = classNames(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$1 = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(inputElement, {
    ...__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 = classNames(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__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: classnames,
    style: __assign(__assign({}, style), isCustomHeight ? {
      height
    } : {}),
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("span", {
      className: prefixCls + "-group",
      children: [inputAddon(prefixCls + "-group-addbefore", addBefore, beforeStyle), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("span", {
        className: innerWrapperClassnames,
        ref: inputWrapperRef,
        onMouseDown: function(e) {
          if (e.target.tagName !== "INPUT") {
            if (inputWrapperRef.current && dom.contains(inputWrapperRef.current, e.target)) {
              e.preventDefault();
            }
          }
        },
        onClick: function(e) {
          if (inputWrapperRef.current && dom.contains(inputWrapperRef.current, e.target)) {
            inputRef.current && inputRef.current.focus();
          }
        },
        children: [inputAddon(prefixCls + "-group-prefix", prefix), inputElement$1, inputAddon(prefixCls + "-group-suffix", suffixElement)]
      }), inputAddon(prefixCls + "-group-addafter", addAfter, afterStyle)]
    })
  }) : allowClear ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
    className: classNames(className, innerWrapperClassnames),
    style: __assign(__assign({}, style), isCustomHeight ? {
      height
    } : {}),
    onMouseDown: keepFocus,
    onClick: function() {
      inputRef.current && inputRef.current.focus();
    },
    children: inputElement$1
  }) : inputElement$1;
}
var InputElement = React__default.default.forwardRef(Input);
InputElement.displayName = "Input";
InputElement.Search = search;
InputElement.TextArea = textarea;
InputElement.Password = password;
InputElement.Group = group;
const Input$1 = InputElement;
exports.default = Input$1;
exports.formatValue = formatValue;
//# sourceMappingURL=input.js.map
