"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 config_context = require("../config/config.context.cjs");
require("./characterLimit.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 CharacterLimit = React__namespace.forwardRef(
  ({ bind, limit, shouldEnforce, value, className, validationErrorIcon, validationErrorsClassName, validationErrorsTitle, validationMode, ...nativeProps }, ref) => {
    const globals = config_context.useArmstrongConfig({
      validationErrorIcon,
      validationMode
    });
    const [boundValue, setBoundValue] = useBindingState.useBindingState(bind, { value });
    const exceeded = boundValue && boundValue.length > limit;
    React__namespace.useLayoutEffect(() => {
      if (shouldEnforce && exceeded) {
        setBoundValue == null ? void 0 : setBoundValue(boundValue == null ? void 0 : boundValue.slice(0, limit));
      }
    }, [boundValue, exceeded, limit, setBoundValue, shouldEnforce]);
    return jsxRuntime.jsxs("div", { ref, className: classNames.concat("arm-character-limit", className), "data-exceeded": exceeded, ...nativeProps, children: [jsxRuntime.jsxs("div", { className: "arm-character-limit-text", children: [boundValue == null ? void 0 : boundValue.length, "/", limit] }), exceeded && jsxRuntime.jsx("div", { className: classNames.concat("arm-character-limit-icon", validationErrorsClassName), title: validationErrorsTitle ?? "Character limit exceeded", children: (globals.validationMode === "both" || globals.validationMode === "icon") && globals.validationErrorIcon })] });
  }
  // type assertion to ensure generic works with RefForwarded component
  // DO NOT CHANGE TYPE WITHOUT CHANGING THIS, FIND TYPE BY INSPECTING React.forwardRef
);
CharacterLimit.displayName = "CharacterLimit";
exports.CharacterLimit = CharacterLimit;
//# sourceMappingURL=characterLimit.component.cjs.map
