"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const useBindingState = require("../../form/hooks/useBindingState.cjs");
const classNames = require("../../utils/classNames.cjs");
require("../../utils/globals.cjs");
require("zod");
const useDebounce = require("../../hooks/useDebounce.cjs");
const useDidUpdateEffect = require("../../hooks/useDidUpdateEffect.cjs");
const radixDialog = require("../../workarounds/radixDialog.cjs");
const config_context = require("../config/config.context.cjs");
const inputWrapper_component = require("../inputWrapper/inputWrapper.component.cjs");
require("./textArea.theme.css.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
const DebounceTextAreaBase = React__namespace.forwardRef(({ milliseconds, value, onValueChange, onChange, ...nativeProps }, ref) => {
  const [actualValue, setActualValue] = useDebounce.useDebounce(milliseconds, value, onValueChange);
  const onChangeEvent = React__namespace.useCallback((e) => {
    setActualValue(e.currentTarget.value);
    onChange == null ? void 0 : onChange(e);
  }, [setActualValue, onChange]);
  return jsxRuntime.jsx("textarea", { ref, value: actualValue, onChange: onChangeEvent, ...nativeProps });
});
DebounceTextAreaBase.displayName = "DebounceInput";
const TextArea = React__namespace.forwardRef(
  ({ bind, onChange, value, className, validationErrorMessages, validationMode, pending, disabled, disableOnPending, onValueChange, scrollValidationErrorsIntoView, delay, validationErrorsClassName, statusClassName, textAreaClassName, label, required, requiredIndicator, displaySize, labelClassName, labelId, testId, errorIcon, leftOverlay, rightOverlay, hideIconOnStatus, statusPosition, autoValidate, ...nativeProps }, ref) => {
    const reactId = React__namespace.useId();
    const id = nativeProps.id ?? reactId;
    const [boundValue, setBoundValue, bindConfig] = useBindingState.useBindingState(bind, {
      value: value == null ? void 0 : value.toString(),
      validationErrorMessages,
      validationErrorIcon: errorIcon,
      validationMode,
      autoValidate
    });
    const globals = config_context.useArmstrongConfig({
      validationMode: bindConfig.validationMode,
      disableControlOnPending: disableOnPending,
      inputStatusPosition: statusPosition,
      inputDisplaySize: displaySize,
      requiredIndicator,
      scrollValidationErrorsIntoView,
      validationErrorIcon: bindConfig.validationErrorIcon,
      hideInputErrorIconOnStatus: hideIconOnStatus,
      autoValidate: bindConfig.autoValidate
    });
    const onBindValueChange = React__namespace.useCallback((currentValue) => {
      var _a, _b, _c;
      const formattedValue = ((_c = (_b = (_a = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a.format) == null ? void 0 : _b.toData) == null ? void 0 : _c.call(_b, currentValue)) || currentValue;
      setBoundValue(formattedValue);
    }, [setBoundValue, bind]);
    const onChangeEvent = React__namespace.useCallback((event) => {
      onChange == null ? void 0 : onChange(event);
      const currentValue = event.currentTarget.value;
      onBindValueChange(currentValue);
      onValueChange == null ? void 0 : onValueChange(currentValue);
    }, [onBindValueChange, onChange, onValueChange]);
    const onValueChangeEvent = React__namespace.useCallback((currentValue) => {
      onBindValueChange(currentValue);
      onValueChange == null ? void 0 : onValueChange(currentValue);
    }, [onValueChange, onBindValueChange]);
    useDidUpdateEffect.useDidUpdateEffect(() => {
      if (globals.autoValidate && bindConfig.isTouched) {
        bindConfig.validate();
      }
    }, [boundValue]);
    const onBlurEvent = React__namespace.useCallback((event) => {
      var _a;
      if (globals.autoValidate && !bindConfig.isTouched) {
        bindConfig.validate();
      }
      bindConfig.setTouched(true);
      radixDialog.onBlurWorkaround(event);
      return (_a = nativeProps.onBlur) == null ? void 0 : _a.call(nativeProps, event);
    }, [bindConfig, globals.autoValidate, nativeProps]);
    const textAreaProps = {
      id,
      className: classNames.concat("arm-text-area", textAreaClassName),
      /** fallback to an empty string if bind is passed in but bound value is undefined to avoid React warning */
      value: (boundValue == null ? void 0 : boundValue.toString()) ?? (bind && ""),
      disabled,
      onBlur: onBlurEvent
    };
    return jsxRuntime.jsxs(inputWrapper_component.InputWrapper, { "data-size": globals.inputDisplaySize, className: classNames.concat(className, "arm-text-area-wrapper"), statusClassName: classNames.concat(statusClassName, "arm-text-area-status"), validationErrorsClassName: classNames.concat(validationErrorsClassName, "arm-text-area-validation"), validationErrorMessages: bindConfig.validationErrorMessages, errorIcon: bindConfig.validationErrorIcon, validationMode: bindConfig.validationMode, pending, disabled, statusPosition: globals.inputStatusPosition, scrollValidationErrorsIntoView: globals.scrollValidationErrorsIntoView, disableOnPending: globals.disableControlOnPending, hideIconOnStatus: globals.hideInputErrorIconOnStatus, label, labelId: labelId ?? id, labelClassName: classNames.concat(labelClassName, "arm-text-area-label"), required, requiredIndicator: globals.requiredIndicator, "data-testid": testId, leftOverlay, rightOverlay, children: [!!delay && jsxRuntime.jsx(DebounceTextAreaBase, { ...nativeProps, ...textAreaProps, milliseconds: delay, onChange, onValueChange: onValueChangeEvent, ref }), !delay && jsxRuntime.jsx("textarea", { className: "arm-text-area", ...nativeProps, ...textAreaProps, onChange: onChangeEvent, ref, disabled: disabled || pending })] });
  }
  // type assertion to ensure generic works with RefForwarded component
  // DO NOT CHANGE TYPE WITHOUT CHANGING THIS, FIND TYPE BY INSPECTING React.forwardRef
);
TextArea.displayName = "Text Area";
exports.TextArea = TextArea;
//# sourceMappingURL=textArea.component.cjs.map
