import { Injectable } from '@angular/core';
import { StorageService } from './storage.service';

@Injectable({ providedIn: 'root' })
export class SessionStorageService extends StorageService {
  constructor() {
    super();
  }

  get<T>(key: string): T {
    let result: T;
    try {
      result = JSON.parse(sessionStorage.getItem(key));
    } catch (error) {
      console.error(`Error getting ${key}`);
    }
    return result;
  }

  set<T>(key: string, data: T): void {
    try {
      sessionStorage.setItem(key, JSON.stringify(data));
    } catch (error) {
      console.error(`Error saving ${key}`);
    }
  }

  delete(key: string): void {
    try {
      sessionStorage.removeItem(key);
    } catch (error) {
      console.error(`Error deleting ${key}`);
    }
  }
}
