import { Route } from 'vue-router'

export const RouteHelper = {
  saveWidgetLocation(widgetName: string, route: Route): void {
    localStorage.setItem(widgetName, this.getStringifyRoute(route))
  },

  getWidgetLocation(widgetName: string): Route | undefined {
    const savedRoute = localStorage.getItem(widgetName)

    return savedRoute ? JSON.parse(savedRoute) : undefined
  },

  saveLastLocation(route: Route): void {
    localStorage.setItem('last_location', this.getStringifyRoute(route))
  },

  getLastLocation(): Route | undefined {
    const savedRoute = localStorage.getItem('last_location')

    return savedRoute ? JSON.parse(savedRoute) : undefined
  },

  getStringifyRoute(route: Route): string {
    return JSON.stringify({
      path: route.path,
      name: route.name,
      hash: route.hash,
      query: route.query,
      params: route.params,
      fullPath: route.fullPath,
    })
  },
}
