/**
 * 
 * https://github.com/jakearchibald/idb-keyval
 */
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
(function (global, factory) {
  (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.idbKeyval = {}));
})(this, function (exports) {
  'use strict';

  function promisifyRequest(request) {
    return new Promise(function (resolve, reject) {
      // @ts-ignore - file size hacks
      request.oncomplete = request.onsuccess = function () {
        return resolve(request.result);
      };
      // @ts-ignore - file size hacks
      request.onabort = request.onerror = function () {
        return reject(request.error);
      };
    });
  }
  function createStore(dbName, storeName) {
    var dbp;
    var getDB = function getDB() {
      if (dbp) return dbp;
      var request = indexedDB.open(dbName);
      request.onupgradeneeded = function () {
        return request.result.createObjectStore(storeName);
      };
      dbp = promisifyRequest(request);
      dbp.then(function (db) {
        // It seems like Safari sometimes likes to just close the connection.
        // It's supposed to fire this event when that happens. Let's hope it does!
        db.onclose = function () {
          return dbp = undefined;
        };
      }, function () {});
      return dbp;
    };
    return function (txMode, callback) {
      return getDB().then(function (db) {
        return callback(db.transaction(storeName, txMode).objectStore(storeName));
      });
    };
  }
  var defaultGetStoreFunc;
  function defaultGetStore() {
    if (!defaultGetStoreFunc) {
      defaultGetStoreFunc = createStore('keyval-store', 'keyval');
    }
    return defaultGetStoreFunc;
  }
  /**
   * Get a value by its key.
   *
   * @param key
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function get(key) {
    var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
    return customStore('readonly', function (store) {
      return promisifyRequest(store.get(key));
    });
  }
  /**
   * Set a value with a key.
   *
   * @param key
   * @param value
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function set(key, value) {
    var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
    return customStore('readwrite', function (store) {
      store.put(value, key);
      return promisifyRequest(store.transaction);
    });
  }
  /**
   * Set multiple values at once. This is faster than calling set() multiple times.
   * It's also atomic – if one of the pairs can't be added, none will be added.
   *
   * @param entries Array of entries, where each entry is an array of `[key, value]`.
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function setMany(entries) {
    var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
    return customStore('readwrite', function (store) {
      entries.forEach(function (entry) {
        return store.put(entry[1], entry[0]);
      });
      return promisifyRequest(store.transaction);
    });
  }
  /**
   * Get multiple values by their keys
   *
   * @param keys
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function getMany(keys) {
    var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
    return customStore('readonly', function (store) {
      return Promise.all(keys.map(function (key) {
        return promisifyRequest(store.get(key));
      }));
    });
  }
  /**
   * Update a value. This lets you see the old value and update it as an atomic operation.
   *
   * @param key
   * @param updater A callback that takes the old value and returns a new value.
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function update(key, updater) {
    var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
    return customStore('readwrite', function (store) {
      return (
        // Need to create the promise manually.
        // If I try to chain promises, the transaction closes in browsers
        // that use a promise polyfill (IE10/11).
        new Promise(function (resolve, reject) {
          store.get(key).onsuccess = function () {
            try {
              store.put(updater(this.result), key);
              resolve(promisifyRequest(store.transaction));
            } catch (err) {
              reject(err);
            }
          };
        })
      );
    });
  }
  /**
   * Delete a particular key from the store.
   *
   * @param key
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function del(key) {
    var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
    return customStore('readwrite', function (store) {
      store.delete(key);
      return promisifyRequest(store.transaction);
    });
  }
  /**
   * Delete multiple keys at once.
   *
   * @param keys List of keys to delete.
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function delMany(keys) {
    var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
    return customStore('readwrite', function (store) {
      keys.forEach(function (key) {
        return store.delete(key);
      });
      return promisifyRequest(store.transaction);
    });
  }
  /**
   * Clear all values in the store.
   *
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function clear() {
    var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
    return customStore('readwrite', function (store) {
      store.clear();
      return promisifyRequest(store.transaction);
    });
  }
  function eachCursor(store, callback) {
    store.openCursor().onsuccess = function () {
      if (!this.result) return;
      callback(this.result);
      this.result.continue();
    };
    return promisifyRequest(store.transaction);
  }
  /**
   * Get all keys in the store.
   *
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function keys() {
    var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
    return customStore('readonly', function (store) {
      // Fast path for modern browsers
      if (store.getAllKeys) {
        return promisifyRequest(store.getAllKeys());
      }
      var items = [];
      return eachCursor(store, function (cursor) {
        return items.push(cursor.key);
      }).then(function () {
        return items;
      });
    });
  }
  /**
   * Get all values in the store.
   *
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function values() {
    var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
    return customStore('readonly', function (store) {
      // Fast path for modern browsers
      if (store.getAll) {
        return promisifyRequest(store.getAll());
      }
      var items = [];
      return eachCursor(store, function (cursor) {
        return items.push(cursor.value);
      }).then(function () {
        return items;
      });
    });
  }
  /**
   * Get all entries in the store. Each entry is an array of `[key, value]`.
   *
   * @param customStore Method to get a custom store. Use with caution (see the docs).
   */
  function entries() {
    var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
    return customStore('readonly', function (store) {
      // Fast path for modern browsers
      // (although, hopefully we'll get a simpler path some day)
      if (store.getAll && store.getAllKeys) {
        return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) {
          var _ref2 = _slicedToArray(_ref, 2),
            keys = _ref2[0],
            values = _ref2[1];
          return keys.map(function (key, i) {
            return [key, values[i]];
          });
        });
      }
      var items = [];
      return customStore('readonly', function (store) {
        return eachCursor(store, function (cursor) {
          return items.push([cursor.key, cursor.value]);
        }).then(function () {
          return items;
        });
      });
    });
  }
  exports.clear = clear;
  exports.createStore = createStore;
  exports.del = del;
  exports.delMany = delMany;
  exports.entries = entries;
  exports.get = get;
  exports.getMany = getMany;
  exports.keys = keys;
  exports.promisifyRequest = promisifyRequest;
  exports.set = set;
  exports.setMany = setMany;
  exports.update = update;
  exports.values = values;
});
