{
  "mappings": "AACA,YAAY,WAAW;AAEvB,cAAc,iCAAiC;KAK1C;CAAS;CAAG,gBAAgB;KAC/B,MAAM,GACN,UAAU;AAGZ,YAAY;CACV;CACA,gBAAgB,4BAA4B;IAC1C;EACD,MAAM,MAAM,eAAe;EAC3B,MAAM,MAAM,eAAe,IAAI,SAAS;;AAK3C,OAAO,iBAAS;CACd;CACA,gBAAgB,4BAA4B;EAC5C,EACA,MACA,aACA,UACA,UACA,eACA,cACC;CACD,OAAO;CACP,aAAa;CACb,WAAW,SAAS,GAAG;CACvB,WAAW,cAAc;CACzB;CACA;KACG,GAAG,wBAAwB,GAAG",
  "names": [],
  "sources": [
    "src/useControllableState.ts"
  ],
  "version": 3,
  "sourcesContent": [
    "import { useEvent } from '@tamagui/use-event'\nimport * as React from 'react'\nimport { startTransition } from '@tamagui/start-transition'\nimport type { TamaguiChangeEventDetails } from '@tamagui/core'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T, Details extends TamaguiChangeEventDetails> = (\n  next: T,\n  details?: Details\n) => void\n\nexport type ControllableStateSetter<\n  T,\n  Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,\n> = {\n  (next: React.SetStateAction<T>): void\n  (next: React.SetStateAction<T>, details: Details): void\n}\n\nconst emptyCallbackFn = (_) => _()\n\nexport function useControllableState<\n  T,\n  Details extends TamaguiChangeEventDetails = TamaguiChangeEventDetails,\n>({\n  prop,\n  defaultProp,\n  onChange,\n  strategy = 'prop-wins',\n  preventUpdate,\n  transition,\n}: {\n  prop?: T | undefined\n  defaultProp: T\n  onChange?: ChangeCb<T, Details>\n  strategy?: 'prop-wins' | 'most-recent-wins'\n  preventUpdate?: boolean\n  transition?: boolean\n}): [T, ControllableStateSetter<T, Details>] {\n  const [state, setState] = React.useState(prop ?? defaultProp)\n  const previous = React.useRef<any>(state)\n  const propWins = strategy === 'prop-wins' && prop !== undefined\n  const value = propWins ? prop : state\n  const onChangeCb = useEvent(onChange || idFn)\n\n  const transitionFn = transition ? startTransition : emptyCallbackFn\n\n  React.useEffect(() => {\n    if (prop === undefined) return\n    previous.current = prop\n    transitionFn(() => {\n      setState(prop)\n    })\n  }, [prop])\n\n  React.useEffect(() => {\n    if (propWins) return\n    if (state !== previous.current) {\n      previous.current = state\n      onChangeCb(state)\n    }\n  }, [onChangeCb, state, propWins])\n\n  const setter = useEvent((next, details?: Details) => {\n    if (preventUpdate) return\n    if (details?.isCanceled) return\n\n    if (details) {\n      const nextValue = typeof next === 'function' ? next(previous.current) : next\n      onChangeCb(nextValue, details)\n      if (details.isCanceled) return\n\n      if (propWins) return\n\n      previous.current = nextValue\n      transitionFn(() => {\n        setState(nextValue)\n      })\n      return\n    }\n\n    if (propWins) {\n      const nextValue = typeof next === 'function' ? next(previous.current) : next\n      onChangeCb(nextValue)\n    } else {\n      transitionFn(() => {\n        setState(next)\n      })\n    }\n  })\n\n  return [value as T, setter as ControllableStateSetter<T, Details>]\n}\n\nconst idFn = () => {}\n"
  ]
}