import type {PlaceRef} from '@crumbs/core/schemas'
import {useRouter} from 'expo-router'

import {
  clearPendingDraft,
  setPendingDraft,
  setPendingPlace,
} from '@/hooks/use-editable-draft'
import {coordsToDraft, emptyDraft} from '@/lib/draft'

export const useCrumbComposer = () => {
  const router = useRouter()

  return {
    open: () => {
      clearPendingDraft()
      router.push('/nearby')
    },
    // a crumb with no place — the escape hatch when nothing nearby fits or
    // location is unavailable
    composeNote: () => {
      setPendingDraft(emptyDraft)
      router.replace('/compose')
    },
    openAt: (coordinate: {latitude: number; longitude: number}) => {
      setPendingDraft(coordsToDraft(coordinate))
      router.push('/compose')
    },
    pickPlace: (place: PlaceRef) => {
      setPendingPlace(place)
      router.push('/compose')
    },
  }
}
