// src/components/form/FormElementWrapper.tsx
import { useEffect } from "react";
import { useState } from "react";
import { useId } from "react";
import { clsx as clsx2 } from "clsx";

// src/components/user-action/Label.tsx
import clsx from "clsx";
import { jsx } from "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__ */ jsx("label", { ...props, className: clsx(styleMapping[size], className), children });
};

// src/components/form/FormElementWrapper.tsx
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
var FormElementWrapper = ({
  children,
  id,
  required = false,
  disabled = false,
  isTouched: initialIsTouched = false,
  label,
  labelProps,
  errorShowBehaviour = "whenTouched",
  error,
  errorProps,
  description,
  descriptionProps,
  containerClassName
}) => {
  const [touched, setTouched] = useState(initialIsTouched);
  const generatedId = useId();
  const usedId = id ?? generatedId;
  useEffect(() => {
    setTouched(initialIsTouched);
  }, [initialIsTouched]);
  const describedBy = [
    description ? `${usedId}-description` : void 0,
    error ? `${usedId}-error` : void 0
  ].filter(Boolean).join(" ");
  const labeledBy = label ? `${usedId}-label` : void 0;
  const isShowingError = errorShowBehaviour === "always" || touched;
  const bag = {
    "touched": touched,
    "onTouched": (isTouched) => setTouched(isTouched ?? true),
    "disabled": disabled,
    "invalid": !!error && isShowingError,
    "required": required,
    "id": usedId,
    "aria-describedby": describedBy,
    "aria-labelby": labeledBy
  };
  return /* @__PURE__ */ jsxs("div", { className: clsx2("relative flex flex-col gap-y-1", containerClassName), children: [
    label && /* @__PURE__ */ jsxs(Label, { htmlFor: usedId, size: "lg", className: clsx2("flex-row-1 items-start", labelProps?.className), children: [
      label,
      required && /* @__PURE__ */ jsx2("div", { role: "none", className: "bg-primary w-2 h-2 rounded-full" })
    ] }),
    description && /* @__PURE__ */ jsx2("p", { ...descriptionProps, className: clsx2("text-description text-sm", descriptionProps?.className), children: description }),
    children(bag),
    error && isShowingError && /* @__PURE__ */ jsx2("p", { ...errorProps, role: "alert", className: clsx2("absolute top-[calc(100%_+_0.25rem)] left-0 text-negative text-sm font-medium", errorProps), children: error })
  ] });
};
export {
  FormElementWrapper
};
//# sourceMappingURL=index.mjs.map