import VueRouter from 'vue-router';
import { Store } from 'vuex';

export function saveUnauthorizedPath(router: VueRouter, store: Store<unknown>) {
  const href = location.href;
  const isLoggedIn = store.getters['oidcClient/isLoggedIn'];
  const prevUrl = window.localStorage.getItem('prev_url');

  if (!isLoggedIn) {
    // @ts-ignore
    const pathname = location.pathname.replace(process.env.VUE_APP_PROJECT_PATH, '');
    const search = location.search;

    if (pathname !== '/' && pathname !== '/callback') {
      window.localStorage.setItem('prev_url', `${pathname}${search}`);
    }
  }
}
