const Status = {
  NONE: 0,
  STORING: 1,
  STORED: 2,
  FAILED: 3
};
class IndexedDBScheduler {
  constructor(options) {
    if (!Cesium.defined(options.name)) {
      throw new Cesium.DeveloperError("options.name is required.");
    }
    const deferred = Cesium.when.defer();
    this.dbname = options.name;
    const dbRequest = window.indexedDB.open(this.dbname);
    const that = this;
    dbRequest.onsuccess = (event) => {
      that.db = event.target.result;
      that.version = that.db.version;
      that.cachestatus = that.cachestatus || {};
      deferred.resolve(that);
    };
    dbRequest.onupgradeneeded = (event) => {
      that.db = event.target.result;
      that.version = that.db.version;
      deferred.resolve(that);
    };
    dbRequest.onerror = (event) => {
      that.db = null;
      deferred.reject("create database fail, error code : " + event.target.errorcode);
    };
    this.layer = options.layer || null;
    this.storageType = options.storageType || "arrayBuffer";
    this.creatingTable = false;
    this.cachestatus = {};
    return deferred.promise;
  }
  checkObjectStoreExist(storeName) {
    return Cesium.defined(this.db) ? this.db.objectStoreNames.contains(storeName) : false;
  }
  createObjectStore(storeName) {
    const deferred = Cesium.when.defer();
    if (this.creatingTable) {
      deferred.reject(false);
    } else {
      if (this.db.objectStoreNames.contains(storeName)) {
        deferred.reject(false);
        return deferred.promise;
      }
      this.creatingTable = true;
      const version = parseInt(this.db.version);
      this.db.close();
      const that = this;
      const dbRequest = window.indexedDB.open(this.dbname, version + 1);
      dbRequest.onupgradeneeded = (event) => {
        const db = event.target.result;
        that.db = db;
        const objectStore = db.createObjectStore(storeName, {
          keyPath: "id"
        });
        if (Cesium.defined(objectStore)) {
          objectStore.createIndex("value", "value", {
            unique: false
          });
          that.creatingTable = false;
          that.cachestatus = that.cachestatus || {};
          that.cachestatus[storeName] = {};
          that.db.close();
          const dbRequest2 = window.indexedDB.open(that.dbname);
          dbRequest2.onsuccess = (event2) => {
            that.db = event2.target.result;
            deferred.resolve(true);
          };
        } else {
          that.creatingTable = false;
          deferred.resolve(false);
        }
      };
      dbRequest.onsuccess = (event) => {
        ;
        event.target.result.close();
        deferred.resolve(true);
      };
      dbRequest.onerror = (event) => {
        that.creatingTable = false;
        deferred.reject(false);
      };
    }
    return deferred.promise;
  }
  putElementInDB(storeName, id, value) {
    const deferred = Cesium.when.defer();
    if (!Cesium.defined(this.db)) {
      deferred.reject(false);
      return deferred.promise;
    }
    const { cachestatus, db } = this;
    if (Cesium.defined(cachestatus[storeName]) && Cesium.defined(cachestatus[storeName][id] && (cachestatus[storeName][id] === Status.STORING || cachestatus[storeName][id] === Status.STORED))) {
      deferred.resolve(false);
      return deferred.promise;
    }
    if (db.objectStoreNames.contains(storeName)) {
      cachestatus[storeName] = cachestatus[storeName] || {};
      try {
        const request = db.transaction([storeName], "readwrite").objectStore(storeName).add({
          id,
          value
        });
        cachestatus[storeName][id] = Status.STORING;
        request.onsuccess = (event) => {
          cachestatus[storeName][id] = Status.STORED;
          deferred.resolve(true);
        };
        request.onerror = (event) => {
          cachestatus[storeName][id] = Status.FAILED;
          deferred.resolve(false);
        };
      } catch (error) {
        deferred.reject(null);
        return deferred.promise;
      }
    } else {
      this.createObjectStore(storeName).then(() => {
        const request = db.transaction([storeName], "readwrite").objectStore(storeName).add({
          id,
          value
        });
        request.onsuccess = function(e) {
          deferred.resolve(true);
        };
        request.onerror = function(e) {
          deferred.reject(false);
        };
      }, () => {
        deferred.reject(false);
      });
    }
    return deferred.promise;
  }
  getElementFromDB(storeName, id) {
    const deferred = Cesium.when.defer();
    const { db } = this;
    if (!Cesium.defined(db)) {
      return null;
    }
    if (!db.objectStoreNames.contains(storeName)) {
      return null;
    }
    try {
      const transaction = db.transaction([storeName]);
      const objectStore = transaction.objectStore(storeName);
      const request = objectStore.get(id);
      request.onsuccess = (e) => {
        return Cesium.defined(e.target.result) ? deferred.resolve(e.target.result.value) : deferred.reject(null);
      };
      request.onerror = (e) => {
        deferred.reject(null);
      };
    } catch (error) {
      deferred.reject(null);
    }
    return deferred.promise;
  }
  updateElementInDB(storeName, id, value) {
    const deferred = Cesium.when.defer();
    const { db } = this;
    if (!Cesium.defined(db)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    if (!db.objectStoreNames.contains(storeName)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    try {
      const request = db.transaction([storeName], "readwrite").objectStore(storeName).put({ id, value });
      request.onsuccess = () => {
        deferred.resolve(true);
      };
      request.onerror = () => {
        deferred.resolve(false);
      };
    } catch (e) {
      deferred.resolve(false);
    }
    return deferred.promise;
  }
  removeElementFromDB(storeName, id) {
    const deferred = Cesium.when.defer();
    const { db } = this;
    if (!Cesium.defined(db)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    if (!db.objectStoreNames.contains(storeName)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    try {
      const request = db.transaction([storeName], "readwrite").objectStore(storeName).delete(id);
      request.onsuccess = () => {
        deferred.resolve(true);
      };
      request.onerror = () => {
        deferred.resolve(false);
      };
    } catch (e) {
      deferred.resolve(false);
    }
    return deferred.promise;
  }
  clear(storeName) {
    const deferred = Cesium.when.defer();
    const { db } = this;
    if (!Cesium.defined(db)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    if (!db.objectStoreNames.contains(storeName)) {
      deferred.resolve(false);
      return deferred.promise;
    }
    try {
      const request = db.transaction([storeName], "readwrite").objectStore(storeName).clear();
      request.onsuccess = () => {
        deferred.resolve(true);
      };
      request.onerror = () => {
        deferred.resolve(false);
      };
    } catch (e) {
      deferred.resolve(false);
    }
    return deferred.promise;
  }
}

export { IndexedDBScheduler as default };
//# sourceMappingURL=IndexedDBScheduler.mjs.map
