{"version":3,"file":"useColorScheme.cjs","names":[],"sources":["../../src/useColorScheme/useColorScheme.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'react';\nimport { createMatchScheme } from '@pastweb/tools';\nimport type { ColorSchemeInfo, MatchScheme, SchemeOptions } from '@pastweb/tools';\n\n/**\n * React hook for reading and updating the current color scheme.\n *\n * The signature mirrors `useColorScheme` from `@pastweb/tools`: pass options to\n * create an internal `MatchScheme`, or pass a pre-created `matchScheme` as the\n * second argument. The React wrapper subscribes to mode/system changes and\n * cleans those listeners up when the component unmounts.\n *\n * @param options - Options passed to `createMatchScheme` when `matchScheme` is not provided.\n * @param matchScheme - Optional pre-created `MatchScheme` instance.\n * @returns A tuple with the current color scheme info and the mode setter.\n *\n * @example\n * ```tsx\n * import { useColorScheme } from '@pastweb/react';\n *\n * function ThemeButton() {\n *   const [scheme, setMode] = useColorScheme({ defaultMode: 'auto' });\n *\n *   return (\n *     <button onClick={() => setMode(scheme.selected === 'dark' ? 'light' : 'dark')}>\n *       Current theme: {scheme.selected}\n *     </button>\n *   );\n * }\n * ```\n */\nexport function useColorScheme(options: SchemeOptions = {}, matchScheme?: MatchScheme): [ColorSchemeInfo, (mode: string) => void] {\n  const scheme = useMemo(\n    () => matchScheme ?? createMatchScheme(options),\n    [matchScheme, options.defaultMode, options.datasetName],\n  );\n  const [current, setCurrent] = useState<ColorSchemeInfo>(() => scheme.getInfo());\n\n  useEffect(() => {\n    const updateCurrent = () => setCurrent(scheme.getInfo());\n    const modeListener = scheme.onModeChange(updateCurrent);\n    const systemListener = scheme.onSysSchemeChange(updateCurrent);\n\n    updateCurrent();\n\n    return () => {\n      modeListener.removeListener();\n      systemListener.removeListener();\n    };\n  }, [scheme]);\n\n  return [current, scheme.setMode];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,eAAe,UAAyB,CAAC,GAAG,aAAsE;CAChI,MAAM,UAAA,GAAA,MAAA,QAAA,OACE,gBAAA,GAAA,eAAA,kBAAA,CAAiC,OAAO,GAC9C;EAAC;EAAa,QAAQ;EAAa,QAAQ;CAAW,CACxD;CACA,MAAM,CAAC,SAAS,eAAA,GAAA,MAAA,SAAA,OAA8C,OAAO,QAAQ,CAAC;CAE9E,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,MAAM,sBAAsB,WAAW,OAAO,QAAQ,CAAC;EACvD,MAAM,eAAe,OAAO,aAAa,aAAa;EACtD,MAAM,iBAAiB,OAAO,kBAAkB,aAAa;EAE7D,cAAc;EAEd,aAAa;GACX,aAAa,eAAe;GAC5B,eAAe,eAAe;EAChC;CACF,GAAG,CAAC,MAAM,CAAC;CAEX,OAAO,CAAC,SAAS,OAAO,OAAO;AACjC"}