// import { atom } from 'jotai';
// import {
//   atomWithLocalStorage,
//   getLocalStorageItem,
//   setLocalStorageItem,
// } from './utils/atomWithLocalStorage';

export type Session = {
  baseUrl: string;
  userId: string;
  deviceId: string;
  accessToken: string;
  expiresInMs?: number;
  refreshToken?: string;
  fallbackSdkStores?: boolean;
};

export type Sessions = Session[];
export type SessionStoreName = {
  sync: string;
  crypto: string;
};

/**
 * Migration code for old session
 */
// const FALLBACK_STORE_NAME: SessionStoreName = {
//   sync: 'web-sync-store',
//   crypto: 'crypto-store',
// } as const;

const LEGACY_CINNY_KEYS = {
  access_token: 'cinny_access_token',
  device_id: 'cinny_device_id',
  user_id: 'cinny_user_id',
  hs_base_url: 'cinny_hs_base_url',
} as const;

function migrateLegacyCinnySession() {
  if (localStorage.getItem('vojo_access_token') !== null) return;

  const accessToken = localStorage.getItem(LEGACY_CINNY_KEYS.access_token);
  const deviceId = localStorage.getItem(LEGACY_CINNY_KEYS.device_id);
  const userId = localStorage.getItem(LEGACY_CINNY_KEYS.user_id);
  const baseUrl = localStorage.getItem(LEGACY_CINNY_KEYS.hs_base_url);

  if (accessToken && deviceId && userId && baseUrl) {
    localStorage.setItem('vojo_access_token', accessToken);
    localStorage.setItem('vojo_device_id', deviceId);
    localStorage.setItem('vojo_user_id', userId);
    localStorage.setItem('vojo_hs_base_url', baseUrl);
  }

  Object.values(LEGACY_CINNY_KEYS).forEach((key) => localStorage.removeItem(key));
}

export function setFallbackSession(
  accessToken: string,
  deviceId: string,
  userId: string,
  baseUrl: string
) {
  localStorage.setItem('vojo_access_token', accessToken);
  localStorage.setItem('vojo_device_id', deviceId);
  localStorage.setItem('vojo_user_id', userId);
  localStorage.setItem('vojo_hs_base_url', baseUrl);
}
export const removeFallbackSession = () => {
  localStorage.removeItem('vojo_hs_base_url');
  localStorage.removeItem('vojo_user_id');
  localStorage.removeItem('vojo_device_id');
  localStorage.removeItem('vojo_access_token');
};
export const getFallbackSession = (): Session | undefined => {
  migrateLegacyCinnySession();
  const baseUrl = localStorage.getItem('vojo_hs_base_url');
  const userId = localStorage.getItem('vojo_user_id');
  const deviceId = localStorage.getItem('vojo_device_id');
  const accessToken = localStorage.getItem('vojo_access_token');

  if (baseUrl && userId && deviceId && accessToken) {
    const session: Session = {
      baseUrl,
      userId,
      deviceId,
      accessToken,
      fallbackSdkStores: true,
    };

    return session;
  }

  return undefined;
};
/**
 * End of migration code for old session
 */

// export const getSessionStoreName = (session: Session): SessionStoreName => {
//   if (session.fallbackSdkStores) {
//     return FALLBACK_STORE_NAME;
//   }

//   return {
//     sync: `sync${session.userId}`,
//     crypto: `crypto${session.userId}`,
//   };
// };

// export const MATRIX_SESSIONS_KEY = 'matrixSessions';
// const baseSessionsAtom = atomWithLocalStorage<Sessions>(
//   MATRIX_SESSIONS_KEY,
//   (key) => {
//     const defaultSessions: Sessions = [];
//     const sessions = getLocalStorageItem(key, defaultSessions);

//     // Before multi account support session was stored
//     // as multiple item in local storage.
//     // So we need these migration code.
//     const fallbackSession = getFallbackSession();
//     if (fallbackSession) {
//       removeFallbackSession();
//       sessions.push(fallbackSession);
//       setLocalStorageItem(key, sessions);
//     }
//     return sessions;
//   },
//   (key, value) => {
//     setLocalStorageItem(key, value);
//   }
// );

// export type SessionsAction =
//   | {
//       type: 'PUT';
//       session: Session;
//     }
//   | {
//       type: 'DELETE';
//       session: Session;
//     };

// export const sessionsAtom = atom<Sessions, [SessionsAction], undefined>(
//   (get) => get(baseSessionsAtom),
//   (get, set, action) => {
//     if (action.type === 'PUT') {
//       const sessions = [...get(baseSessionsAtom)];
//       const sessionIndex = sessions.findIndex(
//         (session) => session.userId === action.session.userId
//       );
//       if (sessionIndex === -1) {
//         sessions.push(action.session);
//       } else {
//         sessions.splice(sessionIndex, 1, action.session);
//       }
//       set(baseSessionsAtom, sessions);
//       return;
//     }
//     if (action.type === 'DELETE') {
//       const sessions = get(baseSessionsAtom).filter(
//         (session) => session.userId !== action.session.userId
//       );
//       set(baseSessionsAtom, sessions);
//     }
//   }
// );
