import type { Accessor } from "solid-js";
import { createContext, useContext } from "solid-js";

export interface CollapsibleContextValue {
  isOpen: Accessor<boolean>;
  setOpen: (open: boolean) => void;
  contentId: Accessor<string>;
  disabled: Accessor<boolean>;
}

const CollapsibleContext = createContext<CollapsibleContextValue>();

/**
 * Returns the Collapsible context. Throws if used outside <Collapsible>.
 */
export function useCollapsibleContext(): CollapsibleContextValue {
  const ctx = useContext(CollapsibleContext);
  if (!ctx) {
    throw new Error(
      "[PettyUI] Collapsible parts must be used inside <Collapsible>.\n" +
        "  Fix: Wrap Collapsible.Trigger and Collapsible.Content inside <Collapsible>.",
    );
  }
  return ctx;
}

export const CollapsibleContextProvider = CollapsibleContext.Provider;
