// AUTO-GENERATED by stele — do not edit.
// Reactive React / React Native bindings over the generated copy.
// Changing the locale via setLocale re-renders every useCopy() consumer.
import {
  createContext,
  createElement,
  useContext,
  useMemo,
  useState,
  type ReactNode,
} from "react";
import { createCopy, type Copy, type Locale } from "./copy.gen";

type CopyContextValue = {
  copy: Copy;
  locale: Locale;
  setLocale: (locale: Locale) => void;
};

const CopyContext = createContext<CopyContextValue | null>(null);

export function CopyProvider(props: { locale: Locale; children: ReactNode }) {
  const [locale, setLocale] = useState<Locale>(props.locale);
  const copy = useMemo(() => createCopy(locale), [locale]);
  const value = useMemo(() => ({ copy, locale, setLocale }), [copy, locale]);
  return createElement(CopyContext.Provider, { value }, props.children);
}

export function useCopy(): Copy {
  const ctx = useContext(CopyContext);
  if (ctx === null) {
    throw new Error("useCopy must be used within a <CopyProvider>");
  }
  return ctx.copy;
}

export function useLocale(): [Locale, (locale: Locale) => void] {
  const ctx = useContext(CopyContext);
  if (ctx === null) {
    throw new Error("useLocale must be used within a <CopyProvider>");
  }
  return [ctx.locale, ctx.setLocale];
}
