export interface AsyncStorage {
	getItem(key: string): Promise<string | null>;
	setItem(key: string, val: string): Promise<void>;
}

export class LocalStorage implements AsyncStorage {
	async getItem(key: string) {
		return window.localStorage.getItem(key);
	}
	async setItem(key: string, val: string): Promise<void> {
		window.localStorage.setItem(key, val);
	}
}

export class ServerSideStorage implements AsyncStorage {
	private store = new Map<string, string>();
	async getItem(key: string): Promise<string | null> {
		return this.store.get(key) || null;
	}
	async setItem(key: string, val: string): Promise<void> {
		this.store.set(key, val);
	}
}

const theStorage: AsyncStorage = typeof window === 'undefined' ? new ServerSideStorage() : new LocalStorage();

export default theStorage;
