import { jsxs, jsx } from 'react/jsx-runtime';
import { forwardRef, useRef, useState, useMemo, useId, isValidElement } from 'react';
import { w as warnDeprecatedErrorUsage } from './utils-CM48ODEJ.js';
import { S as Spinner } from './Spinner-B7tTWcP6.js';
import { t as textFieldStyles } from './TextField.module-DklyLNKz.js';
import { c as cx } from './index-De1g9FRV.js';
import { F as FieldLabel } from './FieldLabel-Cm5A1Jcx.js';
import { c as childrenToString } from './childrenToString-Bz9MqbHb.js';
import { useTrackingId } from './useTrackingId.js';
import { u as useLayoutPropsUtil } from './useLayoutPropsUtil-DjqeAJZ9.js';
import { u as useOptionallyControlledState } from './useOptionallyControlledState-DbDuos5L.js';
import { w as warnOnce } from './warnOnce-Y9PRHcU4.js';
import { s as stripInlineMarkdown } from './stripInlineMarkdown-BZzgG-Zm.js';
import { u as useMergeRefs } from './useMergeRefs-Dfmtq9cI.js';
import { H as Helper } from './Helper-B_VAFgGd.js';

const TextField = forwardRef(
  (props, ref) => {
    const { aiMark, ...labelPropsWithoutAiMark } = props.labelProps || {};
    const data = {
      label: childrenToString(props.label),
      labelProps: labelPropsWithoutAiMark,
      prefix: childrenToString(props.prefix),
      hint: childrenToString(props.hint),
      description: childrenToString(props.description),
      size: props.size,
      type: props.type
    };
    const trackingId = useTrackingId({
      name: "TextField",
      data,
      hasOverride: !!props["data-tracking-id"]
    });
    const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
    const {
      className,
      error,
      errorAriaLive: _errorAriaLive,
      defaultValue: defaultValueProp,
      value: valueProp,
      label,
      moreInfo,
      prefix,
      suffix,
      size,
      type = "text",
      required,
      onChange,
      onFocus,
      onBlur,
      id: idProp,
      description,
      hint,
      showCounter,
      maxLength,
      loading,
      disabled,
      labelProps,
      style,
      placeholder,
      warning,
      "aria-label": ariaLabelProp,
      hideLabel = false,
      isHighlighted = false,
      ...rest
    } = componentProps;
    const inputRef = useRef(null);
    const [isTyping, setIsTyping] = useState(false);
    const [value, setValue] = useOptionallyControlledState({
      controlledValue: valueProp,
      defaultValue: defaultValueProp
    });
    const length = useMemo(() => {
      if (typeof value === "string" || typeof value === "number" || Array.isArray(value)) {
        return String(value).length;
      }
      return 0;
    }, [value]);
    const onChangeHandler = (e) => {
      setValue(e.target.value);
      onChange?.(e);
      if (!isTyping) {
        setIsTyping(true);
      }
    };
    const blurHandler = (e) => {
      onBlur?.(e);
      if (isTyping) {
        setIsTyping(false);
      }
    };
    const labelUid = useId();
    const id = idProp ?? labelUid;
    const prefixUid = useId();
    const suffixUid = useId();
    const helperUid = useId();
    const moreInfoUid = useId();
    warnDeprecatedErrorUsage("TextField", error);
    if (label === void 0) {
      warnOnce(
        "TextField: label will be required in v4.0.0. Use hideLabel to visually hide the label."
      );
    } else if (typeof label !== "string") {
      warnOnce(
        "TextField: Passing ReactNode to label is deprecated. Use a plain string with inline markdown instead."
      );
    }
    const errorMessages = typeof error === "boolean" || error === void 0 ? void 0 : error;
    const styleCombined = { ...style, ...layoutStyles };
    const hasHelperText = hint || showCounter || errorMessages || warning || description;
    const ariaDescribedBy = [hasHelperText && helperUid, moreInfo && moreInfoUid].filter(Boolean).join(" ") || void 0;
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: cx(textFieldStyles["textfield"], className),
        "data-anv": "textfield",
        style: styleCombined,
        children: [
          label && !hideLabel && /* @__PURE__ */ jsx(
            FieldLabel,
            {
              moreInfo,
              moreInfoId: moreInfo ? moreInfoUid : void 0,
              required,
              htmlFor: id,
              ...labelProps,
              children: label
            }
          ),
          /* @__PURE__ */ jsxs(
            "div",
            {
              className: cx(textFieldStyles["input-wrapper"], {
                [textFieldStyles["small"]]: size === "small",
                [textFieldStyles["large"]]: size === "large",
                [textFieldStyles["highlight"]]: isHighlighted
              }),
              role: "presentation",
              onClick: (e) => {
                inputRef.current?.focus();
                if (e.target !== inputRef.current) {
                  inputRef.current?.click();
                }
              },
              children: [
                prefix ? /* @__PURE__ */ jsx(
                  "div",
                  {
                    "aria-hidden": true,
                    className: textFieldStyles["prefix"],
                    id: `prefix${prefixUid}`,
                    children: prefix
                  }
                ) : null,
                /* @__PURE__ */ jsx(
                  "input",
                  {
                    id,
                    className: cx(textFieldStyles["input"], {
                      [textFieldStyles["error"]]: error
                    }),
                    onChange: onChangeHandler,
                    onFocus,
                    onBlur: blurHandler,
                    ref: useMergeRefs([ref, inputRef]),
                    required,
                    maxLength,
                    placeholder,
                    type,
                    disabled,
                    value: value ?? "",
                    "data-tracking-id": trackingId,
                    ...rest,
                    "aria-label": (
                      // TODO(v4.0.0): remove typeof check — label will always be string
                      hideLabel && typeof label === "string" ? stripInlineMarkdown(label) : ariaLabelProp || (label ? void 0 : placeholder)
                    ),
                    "aria-describedby": ariaDescribedBy,
                    "aria-invalid": error ? !!error : void 0
                  }
                ),
                loading ? /* @__PURE__ */ jsx("div", { className: textFieldStyles["loading-spinner-wrapper"], children: /* @__PURE__ */ jsx(Spinner, { size: "small" }) }) : null,
                suffix ? /* @__PURE__ */ jsx(
                  "div",
                  {
                    className: textFieldStyles["suffix"],
                    "aria-hidden": true,
                    id: `suffix${suffixUid}`,
                    children: suffix
                  }
                ) : null
              ]
            }
          ),
          hasHelperText ? /* @__PURE__ */ jsx(
            Helper,
            {
              id: helperUid,
              hint,
              maxLength,
              inputLength: length,
              isTyping,
              showCounter,
              error: typeof errorMessages === "string" || Array.isArray(errorMessages) ? errorMessages : void 0,
              warning,
              errorMessage: isValidElement(errorMessages) ? errorMessages : void 0,
              description
            }
          ) : null
        ]
      }
    );
  }
);
TextField.displayName = "InternalTextField";

export { TextField as T };
//# sourceMappingURL=TextField-CUVsDfWo.js.map
