var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/form/FormElementWrapper.tsx
var FormElementWrapper_exports = {};
__export(FormElementWrapper_exports, {
  FormElementWrapper: () => FormElementWrapper
});
module.exports = __toCommonJS(FormElementWrapper_exports);
var import_react2 = require("react");
var import_clsx2 = require("clsx");

// src/components/user-action/Label.tsx
var import_clsx = __toESM(require("clsx"), 1);
var import_jsx_runtime = require("react/jsx-runtime");
var styleMapping = {
  md: "typography-label-md color-label-text",
  lg: "typography-label-lg color-label-text"
};
var Label = ({
  children,
  size = "md",
  className,
  ...props
}) => {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { ...props, className: (0, import_clsx.default)(styleMapping[size], className), children });
};

// src/hooks/useOverwritableState.ts
var import_react = require("react");

// src/utils/resolveSetState.ts
function resolveSetState(action, prev) {
  return typeof action === "function" ? action(prev) : action;
}

// src/hooks/useOverwritableState.ts
var useOverwritableState = (initialValue, onChange) => {
  const [state, setState] = (0, import_react.useState)(initialValue);
  (0, import_react.useEffect)(() => {
    setState(initialValue);
  }, [initialValue]);
  const onChangeWrapper = (action) => {
    const resolved = resolveSetState(action, state);
    setState(resolved);
    onChange?.(state);
  };
  return [state, onChangeWrapper];
};

// src/components/form/FormElementWrapper.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var FormElementWrapper = ({
  children,
  id,
  required = false,
  disabled = false,
  isShowingError: initialIsShowingError = false,
  onIsShowingError,
  label,
  labelProps,
  errorShowBehaviour = "whenTouched",
  error,
  errorProps,
  description,
  descriptionProps,
  containerClassName
}) => {
  const [touched, setTouched] = useOverwritableState(initialIsShowingError, onIsShowingError);
  const generatedId = (0, import_react2.useId)();
  const usedId = id ?? generatedId;
  const errorId = `${usedId}-description`;
  const labelId = `${usedId}-label`;
  const descriptionId = `${usedId}-description`;
  const describedBy = [
    description ? descriptionId : void 0,
    error ? errorId : void 0
  ].filter(Boolean).join(" ");
  const labelledBy = label ? `${usedId}-label` : void 0;
  const isShowingError = errorShowBehaviour === "always" || touched;
  const bag = {
    "isShowingError": touched,
    "setIsShowingError": (isTouched) => {
      const newValue = isTouched ?? true;
      setTouched(newValue);
      onIsShowingError?.(isTouched);
    },
    "disabled": disabled,
    "invalid": !!error && isShowingError,
    "required": required,
    "id": usedId,
    "aria-required": required,
    "aria-disabled": disabled,
    "aria-describedby": describedBy,
    "aria-labelledby": labelledBy,
    "aria-invalid": !!error,
    "aria-errormessage": error ? errorId : void 0
  };
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: (0, import_clsx2.clsx)("relative flex flex-col gap-y-1", containerClassName), children: [
    label && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
      Label,
      {
        ...labelProps,
        id: labelId,
        htmlFor: usedId,
        size: "lg",
        className: (0, import_clsx2.clsx)("flex-row-1 items-start", labelProps?.className),
        children: [
          label,
          required && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { role: "none", className: "bg-primary w-2 h-2 rounded-full" })
        ]
      }
    ),
    description && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
      "p",
      {
        ...descriptionProps,
        id: descriptionId,
        className: (0, import_clsx2.clsx)("text-description text-sm", descriptionProps?.className),
        children: description
      }
    ),
    children(bag),
    error && isShowingError && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
      "p",
      {
        ...errorProps,
        id: errorId,
        className: (0, import_clsx2.clsx)("absolute top-[calc(100%_+_0.25rem)] left-0 text-negative text-sm font-medium", errorProps?.className),
        role: "alert",
        "aria-hidden": !error,
        "aria-live": "polite",
        children: error
      }
    )
  ] });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FormElementWrapper
});
//# sourceMappingURL=FormElementWrapper.cjs.map