/*
    Facility to cache test results in localStorage.

    USAGE:
      cache.get('key');
      cache.set('key', 'value');
 */

function readLocalStorage(key) {
  if (!document.hasFocus()) {
    // if the document does not have focus when tests are executed, focus() may
    // not be handled properly and eventy may not be dispatched immediately.
    // This can happen when a document is reloaded while Developer Tools have focus.
    /*eslint-disable no-console */
    window.console && window.console.warn && console.warn('document requires focus for a11y support tests');
    /*eslint-enable no-console */
    return {};
  }

  let data;

  try {
    data = window.localStorage && window.localStorage.getItem(key);
    data = data ? JSON.parse(data) : {};
  } catch (e) {
    data = {};
  }

  return data;
}

function writeLocalStorage(key, value) {
  if (!document.hasFocus()) {
    // if the document does not have focus when tests are executed, focus() may
    // not be handled properly and eventy may not be dispatched immediately.
    // This can happen when a document is reloaded while Developer Tools have focus.
    try {
      window.localStorage && window.localStorage.removeItem(key);
    } catch(e) {
      // ignore
    }

    return;
  }

  try {
    window.localStorage && window.localStorage.setItem(key, JSON.stringify(value));
  } catch (e) {
    // ignore
  }
}

const userAgent = window.navigator.userAgent;
const cacheKey = 'ally-supports-cache';
let cache = readLocalStorage(cacheKey);

// update the cache if the user agent changes (newer version, etc)
if (cache.userAgent !== userAgent) {
  cache = {};
}

cache.userAgent = userAgent;

export default {
  get: function getCacheValue(key) {
    return cache[key];
  },
  set: function setCacheValue(key, value) {
    cache[key] = value;
    writeLocalStorage(cacheKey, cache);
  },
};

@@ -161,475 +161,123 @@
 %7B%0A  
-if (!document.hasFocus()) %7B%0A    // if the document does not have focus when tests are executed, focus() may%0A    // not be handled properly and eventy may not be dispatched immediately.%0A    // This can happen when a document is reloaded while Developer Tools have focus.%0A    /*eslint-disable no-console */%0A    window.console && window.console.warn && console.warn('document requires focus for a11y support tests');%0A    /*eslint-enable no-console */%0A    return %7B%7D;%0A  %7D%0A
+// allow reading from storage to retrieve previous support results%0A  // even while the document does not have focus
 %0A  l
@@ -648,17 +648,17 @@
 nd event
-y
+s
  may not

allow reading while document does not have focus