'use client';
'use strict';

var React2 = require('react');
var reactHookForm = require('react-hook-form');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React2__namespace = /*#__PURE__*/_interopNamespace(React2);

// ../react-form/dist/index.js

// ../shared/dist/index.js
function cn(...inputs) {
  const classes = [];
  for (const input of inputs) {
    if (!input) continue;
    if (typeof input === "string") {
      classes.push(input);
    } else if (typeof input === "number") {
      classes.push(String(input));
    } else if (Array.isArray(input)) {
      const nested = cn(...input);
      if (nested) classes.push(nested);
    } else if (typeof input === "object") {
      for (const [key, value] of Object.entries(input)) {
        if (value) classes.push(key);
      }
    }
  }
  return classes.join(" ");
}
function cva(config) {
  return (props) => {
    const classes = [];
    if (config.base) {
      classes.push(config.base);
    }
    if (config.variants) {
      for (const [variantKey, variantOptions] of Object.entries(config.variants)) {
        const selectedValue = props?.[variantKey] ?? config.defaultVariants?.[variantKey];
        if (selectedValue != null) {
          const variantClass = variantOptions[selectedValue];
          if (variantClass) {
            classes.push(variantClass);
          }
        }
      }
    }
    if (config.compoundVariants) {
      for (const compound of config.compoundVariants) {
        const { class: compoundClass, ...conditions } = compound;
        let matches = true;
        for (const [key, value] of Object.entries(conditions)) {
          const propValue = props?.[key] ?? config.defaultVariants?.[key];
          if (propValue !== value) {
            matches = false;
            break;
          }
        }
        if (matches) {
          classes.push(compoundClass);
        }
      }
    }
    if (props?.className) {
      classes.push(props.className);
    }
    return classes.filter(Boolean).join(" ");
  };
}
var seen = /* @__PURE__ */ new Set();
function isDev() {
  return typeof process === "undefined" || process.env?.NODE_ENV !== "production";
}
function emit(level, code, message, detail) {
  if (!isDev()) return;
  const key = `${level}:${code}`;
  if (seen.has(key)) return;
  seen.add(key);
  const text = `[refraction-ui] ${code}: ${message}`;
  {
    console.warn(text, "");
  }
}
function devWarn(code, message, detail) {
  emit("warn", code, message);
}
var Slot = React2__namespace.forwardRef(function Slot2(props, forwardedRef) {
  const { children, ...slotProps } = props;
  if (!React2__namespace.isValidElement(children)) {
    return null;
  }
  const childProps = children.props ?? {};
  const mergedProps = mergeProps(slotProps, childProps);
  let childRef = childProps.ref;
  if (childRef === void 0 && !("ref" in childProps)) {
    childRef = children.ref;
  }
  const mergedRef = composeRefs(forwardedRef, childRef);
  return React2__namespace.cloneElement(
    children,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    { ...mergedProps, ref: mergedRef }
  );
});
Slot.displayName = "Slot";
function mergeProps(slotProps, childProps) {
  const merged = { ...slotProps };
  for (const key of Object.keys(childProps)) {
    const slotValue = slotProps[key];
    const childValue = childProps[key];
    if (/^on[A-Z]/.test(key) && typeof slotValue === "function" && typeof childValue === "function") {
      merged[key] = (...args) => {
        const child = childValue;
        const slot = slotValue;
        child(...args);
        slot(...args);
      };
    } else if (key === "className") {
      merged[key] = [slotValue, childValue].filter(Boolean).join(" ");
    } else if (key === "style") {
      merged[key] = { ...slotValue, ...childValue };
    } else if (childValue !== void 0) {
      merged[key] = childValue;
    }
  }
  return merged;
}
function composeRefs(...refs) {
  return (node) => {
    for (const ref of refs) {
      if (typeof ref === "function") {
        ref(node);
      } else if (ref != null) {
        const objectRef = ref;
        objectRef.current = node;
      }
    }
  };
}
var Form = reactHookForm.FormProvider;
var FormFieldContext = React2__namespace.createContext(
  void 0
);
var FormItemContext = React2__namespace.createContext(
  void 0
);
function FormField(props) {
  return /* @__PURE__ */ jsxRuntime.jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsxRuntime.jsx(reactHookForm.Controller, { ...props }) });
}
function useFormField() {
  const fieldContext = React2__namespace.useContext(FormFieldContext);
  const itemContext = React2__namespace.useContext(FormItemContext);
  const formContext = reactHookForm.useFormContext();
  const { errors } = reactHookForm.useFormState({
    control: formContext?.control,
    name: fieldContext?.name,
    exact: true
  });
  if (!fieldContext) {
    devWarn(
      "react-form/use-form-field-outside-form-field",
      "useFormField (via FormLabel/FormControl/FormDescription/FormMessage) must be used inside a <FormField>. The missing FormFieldContext makes this throw."
    );
    throw new Error("useFormField must be used within a <FormField>");
  }
  if (!itemContext) {
    devWarn(
      "react-form/use-form-field-outside-form-item",
      "useFormField (via FormLabel/FormControl/FormDescription/FormMessage) must be used inside a <FormItem>. The missing FormItemContext makes this throw."
    );
    throw new Error("useFormField must be used within a <FormItem>");
  }
  const { id } = itemContext;
  const error = getNestedError(errors, fieldContext.name);
  return {
    id,
    name: fieldContext.name,
    formItemId: `${id}-form-item`,
    formDescriptionId: `${id}-form-item-description`,
    formMessageId: `${id}-form-item-message`,
    error,
    invalid: Boolean(error)
  };
}
function getNestedError(errors, name) {
  if (!errors) return void 0;
  const segments = name.replace(/\[(\w+)\]/g, ".$1").split(".");
  let cursor = errors;
  for (const seg of segments) {
    if (cursor && typeof cursor === "object" && seg in cursor) {
      cursor = cursor[seg];
    } else {
      return void 0;
    }
  }
  return cursor;
}
var formItemVariants = cva({ base: "space-y-2" });
var FormItem = React2__namespace.forwardRef(
  ({ className, ...props }, ref) => {
    const id = React2__namespace.useId();
    return /* @__PURE__ */ jsxRuntime.jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref, className: cn(formItemVariants(), className), ...props }) });
  }
);
FormItem.displayName = "FormItem";
var formLabelVariants = cva({
  base: "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",
  variants: {
    invalid: {
      true: "text-destructive",
      false: ""
    }
  },
  defaultVariants: {
    invalid: "false"
  }
});
var FormLabel = React2__namespace.forwardRef(
  ({ className, ...props }, ref) => {
    const { invalid, formItemId } = useFormField();
    return /* @__PURE__ */ jsxRuntime.jsx(
      "label",
      {
        ref,
        htmlFor: formItemId,
        "data-invalid": invalid ? "" : void 0,
        className: cn(formLabelVariants({ invalid: invalid ? "true" : "false" }), className),
        ...props
      }
    );
  }
);
FormLabel.displayName = "FormLabel";
var FormControl = React2__namespace.forwardRef(
  ({ ...props }, ref) => {
    const { invalid, formItemId, formDescriptionId, formMessageId } = useFormField();
    return /* @__PURE__ */ jsxRuntime.jsx(
      Slot,
      {
        ref,
        id: formItemId,
        "aria-describedby": invalid ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,
        "aria-invalid": invalid || void 0,
        ...props
      }
    );
  }
);
FormControl.displayName = "FormControl";
var FormDescription = React2__namespace.forwardRef(({ className, ...props }, ref) => {
  const { formDescriptionId } = useFormField();
  return /* @__PURE__ */ jsxRuntime.jsx(
    "p",
    {
      ref,
      id: formDescriptionId,
      className: cn("text-sm text-muted-foreground", className),
      ...props
    }
  );
});
FormDescription.displayName = "FormDescription";
var FormMessage = React2__namespace.forwardRef(
  ({ className, children, ...props }, ref) => {
    const { error, formMessageId } = useFormField();
    const body = error?.message ? String(error.message) : children;
    if (!body) return null;
    return /* @__PURE__ */ jsxRuntime.jsx(
      "p",
      {
        ref,
        id: formMessageId,
        className: cn("text-sm font-medium text-destructive", className),
        ...props,
        children: body
      }
    );
  }
);
FormMessage.displayName = "FormMessage";

Object.defineProperty(exports, "Controller", {
  enumerable: true,
  get: function () { return reactHookForm.Controller; }
});
Object.defineProperty(exports, "FormProvider", {
  enumerable: true,
  get: function () { return reactHookForm.FormProvider; }
});
Object.defineProperty(exports, "useController", {
  enumerable: true,
  get: function () { return reactHookForm.useController; }
});
Object.defineProperty(exports, "useForm", {
  enumerable: true,
  get: function () { return reactHookForm.useForm; }
});
Object.defineProperty(exports, "useFormContext", {
  enumerable: true,
  get: function () { return reactHookForm.useFormContext; }
});
Object.defineProperty(exports, "useFormState", {
  enumerable: true,
  get: function () { return reactHookForm.useFormState; }
});
Object.defineProperty(exports, "useWatch", {
  enumerable: true,
  get: function () { return reactHookForm.useWatch; }
});
exports.Form = Form;
exports.FormControl = FormControl;
exports.FormDescription = FormDescription;
exports.FormField = FormField;
exports.FormItem = FormItem;
exports.FormLabel = FormLabel;
exports.FormMessage = FormMessage;
exports.Slot = Slot;
exports.formItemVariants = formItemVariants;
exports.formLabelVariants = formLabelVariants;
exports.useFormField = useFormField;
//# sourceMappingURL=form.cjs.map
//# sourceMappingURL=form.cjs.map