import FDBCursor from"./FDBCursor.js";import FDBCursorWithValue from"./FDBCursorWithValue.js";import FDBKeyRange from"./FDBKeyRange.js";import FDBRequest from"./FDBRequest.js";import enforceRange from"./lib/enforceRange.js";import{ConstraintError,InvalidStateError,TransactionInactiveError}from"./lib/errors.js";import FakeDOMStringList from"./lib/FakeDOMStringList.js";import valueToKey from"./lib/valueToKey.js";import valueToKeyRange from"./lib/valueToKeyRange.js";const confirmActiveTransaction=index=>{if(index._rawIndex.deleted||index.objectStore._rawObjectStore.deleted)throw new InvalidStateError;if("active"!==index.objectStore.transaction._state)throw new TransactionInactiveError};class FDBIndex{constructor(objectStore,rawIndex){this._rawIndex=rawIndex,this._name=rawIndex.name,this.objectStore=objectStore,this.keyPath=rawIndex.keyPath,this.multiEntry=rawIndex.multiEntry,this.unique=rawIndex.unique}get name(){return this._name}set name(name){const transaction=this.objectStore.transaction;if(!transaction.db._runningVersionchangeTransaction)throw new InvalidStateError;if("active"!==transaction._state)throw new TransactionInactiveError;if(this._rawIndex.deleted||this.objectStore._rawObjectStore.deleted)throw new InvalidStateError;if((name=String(name))===this._name)return;if(this.objectStore.indexNames.contains(name))throw new ConstraintError;const oldName=this._name,oldIndexNames=[...this.objectStore.indexNames];this._name=name,this._rawIndex.name=name,this.objectStore._indexesCache.delete(oldName),this.objectStore._indexesCache.set(name,this),this.objectStore._rawObjectStore.rawIndexes.delete(oldName),this.objectStore._rawObjectStore.rawIndexes.set(name,this._rawIndex),this.objectStore.indexNames=new FakeDOMStringList(...Array.from(this.objectStore._rawObjectStore.rawIndexes.keys()).filter((indexName=>{const index=this.objectStore._rawObjectStore.rawIndexes.get(indexName);return index&&!index.deleted})).sort()),transaction._rollbackLog.push((()=>{this._name=oldName,this._rawIndex.name=oldName,this.objectStore._indexesCache.delete(name),this.objectStore._indexesCache.set(oldName,this),this.objectStore._rawObjectStore.rawIndexes.delete(name),this.objectStore._rawObjectStore.rawIndexes.set(oldName,this._rawIndex),this.objectStore.indexNames=new FakeDOMStringList(...oldIndexNames)}))}openCursor(range,direction){confirmActiveTransaction(this),null===range&&(range=void 0),void 0===range||range instanceof FDBKeyRange||(range=FDBKeyRange.only(valueToKey(range)));const request=new FDBRequest;request.source=this,request.transaction=this.objectStore.transaction;const cursor=new FDBCursorWithValue(this,range,direction,request);return this.objectStore.transaction._execRequestAsync({operation:cursor._iterate.bind(cursor),request:request,source:this})}openKeyCursor(range,direction){confirmActiveTransaction(this),null===range&&(range=void 0),void 0===range||range instanceof FDBKeyRange||(range=FDBKeyRange.only(valueToKey(range)));const request=new FDBRequest;request.source=this,request.transaction=this.objectStore.transaction;const cursor=new FDBCursor(this,range,direction,request,!0);return this.objectStore.transaction._execRequestAsync({operation:cursor._iterate.bind(cursor),request:request,source:this})}get(key){return confirmActiveTransaction(this),key instanceof FDBKeyRange||(key=valueToKey(key)),this.objectStore.transaction._execRequestAsync({operation:this._rawIndex.getValue.bind(this._rawIndex,key),source:this})}getAll(query,count){arguments.length>1&&void 0!==count&&(count=enforceRange(count,"unsigned long")),confirmActiveTransaction(this);const range=valueToKeyRange(query);return this.objectStore.transaction._execRequestAsync({operation:this._rawIndex.getAllValues.bind(this._rawIndex,range,count),source:this})}getKey(key){return confirmActiveTransaction(this),key instanceof FDBKeyRange||(key=valueToKey(key)),this.objectStore.transaction._execRequestAsync({operation:this._rawIndex.getKey.bind(this._rawIndex,key),source:this})}getAllKeys(query,count){arguments.length>1&&void 0!==count&&(count=enforceRange(count,"unsigned long")),confirmActiveTransaction(this);const range=valueToKeyRange(query);return this.objectStore.transaction._execRequestAsync({operation:this._rawIndex.getAllKeys.bind(this._rawIndex,range,count),source:this})}count(key){return confirmActiveTransaction(this),null===key&&(key=void 0),void 0===key||key instanceof FDBKeyRange||(key=FDBKeyRange.only(valueToKey(key))),this.objectStore.transaction._execRequestAsync({operation:()=>this._rawIndex.count(key),source:this})}toString(){return"[object IDBIndex]"}}export default FDBIndex;