export const getLocalStorage = <T>(key: string): T | null => {
  let data = localStorage.getItem(key);
  try {
    data = JSON.parse(data ?? "");
  } catch (err) {
    data = localStorage.getItem(key);
  }

  return data as T;
};

export const setLocalStorage = (key: string, value: any) => {
  let data = value;
  if (Array.isArray(value) || typeof value === "object") {
    data = JSON.stringify(value);
  }
  localStorage.setItem(key, data);
};

export const getSessionStorage = <T>(key: string): T | null => {
  let data = sessionStorage.getItem(key);
  try {
    data = JSON.parse(data ?? "");
  } catch (err) {
    data = sessionStorage.getItem(key);
  }
  return data as T;
};

export const setSessionStorage = (key: string, value: any) => {
  let data = value;
  if (Array.isArray(value) || typeof value === "object") {
    data = JSON.stringify(value);
  }
  sessionStorage.setItem(key, data);
};

export const removeLocalStorage = (key: string) => {
  localStorage.removeItem(key);
};

export const removeSessionStorage = (key: string) => {
  sessionStorage.removeItem(key);
};
