// src/components/form/FormElementWrapper.tsx
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/hooks/useOverwritableState.ts
import { useEffect, useState } from "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] = useState(initialValue);
  useEffect(() => {
    setState(initialValue);
  }, [initialValue]);
  const onChangeWrapper = (action) => {
    const resolved = resolveSetState(action, state);
    setState(resolved);
    onChange?.(state);
  };
  return [state, onChangeWrapper];
};

// src/components/form/FormElementWrapper.tsx
import { jsx as jsx2, jsxs } from "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 = 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__ */ jsxs("div", { className: clsx2("relative flex flex-col gap-y-1", containerClassName), children: [
    label && /* @__PURE__ */ jsxs(
      Label,
      {
        ...labelProps,
        id: labelId,
        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,
        id: descriptionId,
        className: clsx2("text-description text-sm", descriptionProps?.className),
        children: description
      }
    ),
    children(bag),
    error && isShowingError && /* @__PURE__ */ jsx2(
      "p",
      {
        ...errorProps,
        id: errorId,
        className: clsx2("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
      }
    )
  ] });
};
export {
  FormElementWrapper
};
//# sourceMappingURL=index.js.map