import { jsx } from "react/jsx-runtime";
import { createElement } from "react";
import { ErrorMessage } from "@hookform/error-message";
import { useFormContext, Controller } from "react-hook-form";
/* empty css                                                                                                                                                                                       */
/* empty css                                                                                                                                                                                        */
/* empty css                                                                                                                                                                                                */
/* empty css                                                                                                                                                                                           */
/* empty css                                                                                                                                                                                           */
import { Checkbox } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Checkbox/Checkbox.mjs";
import { Flex } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Flex/Flex.mjs";
const FormCheckbox = ({ name, rules, onChange, ...rest }) => {
  const { control, formState, getFieldState } = useFormContext();
  const { error } = getFieldState(name, formState);
  return /* @__PURE__ */ jsx(
    Controller,
    {
      control,
      name,
      rules,
      render: ({ field }) => {
        const { onChange: handleChange, value, ...restField } = field;
        return /* @__PURE__ */ jsx(
          Checkbox,
          {
            checked: value,
            ...rest,
            onChange: (e) => {
              handleChange(e.currentTarget.checked);
              onChange == null ? void 0 : onChange(e);
            },
            ...restField,
            error: error ? /* @__PURE__ */ jsx(ErrorMessage, { errors: formState.errors, name }) : void 0
          }
        );
      }
    }
  );
};
const FormCheckboxGroup = ({
  name,
  rules,
  data,
  onChange,
  direction,
  gap = "sm",
  label,
  ...rest
}) => {
  const { control, formState, getFieldState } = useFormContext();
  const { error } = getFieldState(name, formState);
  return /* @__PURE__ */ jsx(
    Controller,
    {
      control,
      name,
      rules,
      render: ({ field }) => {
        const { onChange: handleChange, value, ...restField } = field;
        return /* @__PURE__ */ jsx(
          Checkbox.Group,
          {
            ...rest,
            label,
            value,
            onChange: (val) => {
              handleChange(val);
              onChange == null ? void 0 : onChange(val);
            },
            ...restField,
            error: error ? /* @__PURE__ */ jsx(ErrorMessage, { errors: formState.errors, name }) : void 0,
            children: /* @__PURE__ */ jsx(Flex, { direction, gap, mt: label ? "xs" : void 0, children: data.map((i) => /* @__PURE__ */ createElement(Checkbox, { ...i, key: i.value })) })
          }
        );
      }
    }
  );
};
export {
  FormCheckbox,
  FormCheckboxGroup
};
