import { useMessage } from 'naive-ui'

export function useAlert() {
  let message: ReturnType<typeof useMessage> | undefined
  try {
    message = useMessage()
  } catch {
    // No outer <n-message-provider />, fall back to window.alert in browser.
  }

  return (content: string) => {
    if (message) {
      message.error(content)
    } else if (typeof window !== 'undefined') {
      window.alert(content)
    }
  }
}
