function createStorageWrapper(storage) {
  return {
    getItem(key) {
      try {
        return storage?.getItem(key) ?? null;
      } catch (error) {
        return null;
      }
    },
    setItem(key, value) {
      try {
        storage?.setItem(key, value);
      } catch (error) {
        /* noop */
      }
    },
    removeItem(key) {
      try {
        storage?.removeItem(key);
      } catch (error) {
        /* noop */
      }
    },
    clear() {
      try {
        storage?.clear();
      } catch (error) {
        /* noop */
      }
    }
  };
}

export const safeSessionStorage = createStorageWrapper(typeof window !== 'undefined' ? window.sessionStorage : undefined);
export const safeLocalStorage = createStorageWrapper(typeof window !== 'undefined' ? window.localStorage : undefined);
