import { CallbackModel } from '../models/callback-model';

export abstract class GenericHelper {
  static lStorage = window.localStorage;
  // Local Storage Helpers

  public static SetLocalStorage(key: string, value: any): void {
    // const x: string = JSON.stringify(value);
    // console.log(x);
    const a = {
      obj: value
    };
    this.lStorage.setItem(key, JSON.stringify(a));
  }

  public static GetLocalStorage(key: string): any {
    const abc = JSON.parse(localStorage.getItem(key));
    console.log(abc);
    console.log(abc['obj']);
    // const x =  JSON.parse(abc['obj']);
    return abc['obj'];
  }

  public static RemoveLocalStorage(key: string): void {
    this.lStorage.removeItem(key);
  }

  public static ClearLocalStorage(): void {
    this.lStorage.clear();
  }

  // Method Finder.
  public static CallReturnMethod<T>(returnVal: CallbackModel, cpt: T) {
    // check method with params
    if (cpt[returnVal.methodName]) {
      // method exists on the component
      if (returnVal.returnValue) {
        cpt[returnVal.methodName](returnVal);
      } else {
        cpt[returnVal.methodName]();
      }
    }
  }
}
