module Turbo::Native::Navigation

def hotwire_native_app?

Legacy Turbo Native applications use the "Turbo Native" string.
Hotwire Native applications are identified by having the string "Hotwire Native" as part of their user agent.
def hotwire_native_app?
  request.user_agent.to_s.match?(/(Turbo|Hotwire) Native/)
end

def recede_or_redirect_back_or_to(url, **options)

Same as recede_or_redirect_to but redirects to the previous page or provided fallback location if the Turbo Native app is not present.
def recede_or_redirect_back_or_to(url, **options)
  turbo_native_action_or_redirect url, :recede, :back, options
end

def recede_or_redirect_to(url, **options)

Tell the Turbo Native app to dismiss a modal (if presented) or pop a screen off of the navigation stack. Otherwise redirect to the given URL if Turbo Native is not present.
def recede_or_redirect_to(url, **options)
  turbo_native_action_or_redirect url, :recede, :to, options
end

def refresh_or_redirect_back_or_to(url, **options)

Same as refresh_or_redirect_to but redirects to the previous page or provided fallback location if the Turbo Native app is not present.
def refresh_or_redirect_back_or_to(url, **options)
  turbo_native_action_or_redirect url, :refresh, :back, options
end

def refresh_or_redirect_to(url, **options)

Tell the Turbo Native app to refresh the current screen, otherwise redirect to the given URL if Turbo Native is not present.
def refresh_or_redirect_to(url, **options)
  turbo_native_action_or_redirect url, :refresh, :to, options
end

def resume_or_redirect_back_or_to(url, **options)

Same as resume_or_redirect_to but redirects to the previous page or provided fallback location if the Turbo Native app is not present.
def resume_or_redirect_back_or_to(url, **options)
  turbo_native_action_or_redirect url, :resume, :back, options
end

def resume_or_redirect_to(url, **options)

Tell the Turbo Native app to ignore this navigation, otherwise redirect to the given URL if Turbo Native is not present.
def resume_or_redirect_to(url, **options)
  turbo_native_action_or_redirect url, :resume, :to, options
end

def turbo_native_action_or_redirect(url, action, redirect_type, options = {})

:nodoc:
def turbo_native_action_or_redirect(url, action, redirect_type, options = {})
  native_params = options.delete(:native_params) || {}
  if turbo_native_app?
    redirect_to send("turbo_#{action}_historical_location_url", notice: options[:notice], **native_params)
  elsif redirect_type == :back
    redirect_back fallback_location: url, **options
  else
    redirect_to url, options
  end
end