import{create_object}from"./common.js";function _slice(a,b,c,d){let e=[];for(let f,g=0;g<a.index.length;g++)if(f=a.index[g],b>=f.length)b-=f.length;else{const g=f[d?"splice":"slice"](b,c),h=g.length;if(h&&(e=e.length?e.concat(g):g,c-=h,d&&(a.length-=h),!c))break;b=0}return e}export function KeystoreArray(a){if(!this||this.constructor!==KeystoreArray)return new KeystoreArray(a);this.index=a?[a]:[],this.length=a?a.length:0;const b=this;return new Proxy([],{get(a,c){if("length"===c)return b.length;if("push"===c)return function(a){b.index[b.index.length-1].push(a),b.length++};if("pop"===c)return function(){if(b.length)return b.length--,b.index[b.index.length-1].pop()};if("indexOf"===c)return function(a){let c=0;for(let d,e,f=0;f<b.index.length;f++){if(d=b.index[f],e=d.indexOf(a),0<=e)return c+e;c+=d.length}return-1};if("includes"===c)return function(a){for(let c=0;c<b.index.length;c++)if(b.index[c].includes(a))return!0;return!1};if("slice"===c)return function(a,c){return _slice(b,a||0,c||b.length,!1)};if("splice"===c)return function(a,c){return _slice(b,a||0,c||b.length,!0)};if("constructor"===c)return Array;if("symbol"!=typeof c){const a=b.index[0|c/2147483648];return a&&a[c]}},set(a,c,d){const e=0|c/2147483648,f=b.index[e]||(b.index[e]=[]);return f[c]=d,b.length++,!0}})}KeystoreArray.prototype.clear=function(){this.index.length=0},KeystoreArray.prototype.push=function(){};function Keystore(){this.index,this.refs,this.size,this.crc,this.bit}export function KeystoreMap(a=8){return this&&this.constructor===KeystoreMap?void(this.index=create_object(),this.refs=[],this.size=0,32<a?(this.crc=lcg64,this.bit=BigInt(a)):(this.crc=lcg,this.bit=a)):new KeystoreMap(a)}KeystoreMap.prototype.get=function(a){const b=this.crc(a),c=this.index[b];return c&&c.get(a)},KeystoreMap.prototype.set=function(a,b){const c=this.crc(a);let d=this.index[c];if(d){let c=d.size;d.set(a,b),c-=d.size,c&&this.size++}else this.index[c]=d=new Map([[a,b]]),this.refs.push(d),this.size++};export function KeystoreSet(a=8){return this&&this.constructor===KeystoreSet?void(this.index=create_object(),this.refs=[],this.size=0,32<a?(this.crc=lcg64,this.bit=BigInt(a)):(this.crc=lcg,this.bit=a)):new KeystoreSet(a)}KeystoreSet.prototype.add=function(a){const b=this.crc(a);let c=this.index[b];if(c){let b=c.size;c.add(a),b-=c.size,b&&this.size++}else this.index[b]=c=new Set([a]),this.refs.push(c),this.size++},KeystoreMap.prototype.has=KeystoreSet.prototype.has=function(a){const b=this.crc(a),c=this.index[b];return c&&c.has(a)},KeystoreMap.prototype.delete=KeystoreSet.prototype.delete=function(a){const b=this.crc(a),c=this.index[b];c&&c.delete(a)&&this.size--},KeystoreMap.prototype.clear=KeystoreSet.prototype.clear=function(){this.index=create_object(),this.refs=[],this.size=0},KeystoreMap.prototype.values=KeystoreSet.prototype.values=function*(){for(let a=0;a<this.refs.length;a++)for(let b of this.refs[a].values())yield b},KeystoreMap.prototype.keys=KeystoreSet.prototype.keys=function*(){for(let a=0;a<this.refs.length;a++)for(let b of this.refs[a].keys())yield b},KeystoreMap.prototype.entries=KeystoreSet.prototype.entries=function*(){for(let a=0;a<this.refs.length;a++)for(let b of this.refs[a].entries())yield b};function lcg(a){let b=2**this.bit-1;if("number"==typeof a)return a&b;let c=0,d=this.bit+1;for(let e=0;e<a.length;e++)c=(c*d^a.charCodeAt(e))&b;return 32===this.bit?c+2147483648:c}function lcg64(a){let b=BigInt(2)**this.bit-BigInt(1),c=typeof a;if("bigint"==c)return a&b;if("number"==c)return BigInt(a)&b;let d=BigInt(0),e=this.bit+BigInt(1);for(let c=0;c<a.length;c++)d=(d*e^BigInt(a.charCodeAt(c)))&b;return d}