import * as RadioGroup from "@radix-ui/react-radio-group";
import { createContext, ReactNode, useContext, useId } from "react";
import { cn } from "@/lib/cn";

type SwitchItemGroupContextValue = {
    value: string;
    layoutId: string;
    disabled: boolean;
};

const SwitchItemGroupContext = createContext<SwitchItemGroupContextValue | null>(null);

export const useSwitchItemGroup = () => {
    const ctx = useContext(SwitchItemGroupContext);
    if (!ctx) {
        throw new Error("SwitchItem must be used inside a SwitchItemGroup");
    }
    return ctx;
};

type Props = {
    value: string;
    onChange: (value: string) => void;
    children: ReactNode;
    className?: string;
    disabled?: boolean;
};

export const SwitchItemGroup = ({
    value,
    onChange,
    children,
    className,
    disabled = false,
}: Props) => {
    const layoutId = useId();

    return (
        <SwitchItemGroupContext.Provider value={{ value, layoutId, disabled }}>
            <RadioGroup.Root
                value={value}
                onValueChange={onChange}
                disabled={disabled}
                className={cn(
                    "flex shrink-0 rounded-lg border border-nb-gray-850 bg-nb-gray-910 p-1 overflow-hidden",
                    disabled && "opacity-50 pointer-events-none",
                    className,
                )}
            >
                {children}
            </RadioGroup.Root>
        </SwitchItemGroupContext.Provider>
    );
};
