import Index from"./index.js";import Document from"./document.js";import{KeystoreMap,KeystoreSet}from"./keystore.js";import{is_string}from"./common.js";const chunk_size_reg=250000,chunk_size_map=5000,chunk_size_ctx=1000;function map_to_json(a,b=0){let c=[],d=[];b&&(b=0|chunk_size_map*(chunk_size_reg/b));for(const e of a.entries())d.push(e),d.length===b&&(c.push(d),d=[]);return d.length&&c.push(d),c}function json_to_map(a,b){b||(b=new Map);for(let c,d=0;d<a.length;d++)c=a[d],b.set(c[0],c[1]);return b}function ctx_to_json(a,b=0){let c=[],d=[];b&&(b=0|chunk_size_ctx*(chunk_size_reg/b));for(const e of a.entries()){const a=e[0],f=e[1];d.push([a,map_to_json(f)[0]]),d.length===b&&(c.push(d),d=[])}return d.length&&c.push(d),c}function json_to_ctx(a,b){b||(b=new Map);for(let c,d,e=0;e<a.length;e++)c=a[e],d=b.get(c[0]),b.set(c[0],json_to_map(c[1],d));return b}function reg_to_json(a){let b=[],c=[];for(const d of a.keys())c.push(d),c.length===chunk_size_reg&&(b.push(c),c=[]);return c.length&&b.push(c),b}function json_to_reg(a,b){b||(b=new Set);for(let c=0;c<a.length;c++)b.add(a[c]);return b}function save(a,b,c,d,e,f,g=0){const h=d&&d.constructor===Array,i=h?d.shift():d;if(!i)return this.export(a,b,e,f+1);const j=a((b?b+".":"")+(g+1)+"."+c,JSON.stringify(i));if(j&&j.then){const i=this;return j.then(function(){return save.call(i,a,b,c,h?d:null,e,f,g+1)})}return save.call(this,a,b,c,h?d:null,e,f,g+1)}export function exportIndex(a,b,c=0,d=0){let e,f;switch(d){case 0:e="reg",f=reg_to_json(this.reg);break;case 1:e="cfg",f=null;break;case 2:e="map",f=map_to_json(this.map,this.reg.size);break;case 3:e="ctx",f=ctx_to_json(this.ctx,this.reg.size);break;default:return;}return save.call(this,a,b,e,f,c,d)}export function importIndex(a,b){if(b){"string"==typeof b&&(b=JSON.parse(b));const c=a.split(".");switch("json"===c[c.length-1]&&c.pop(),3===c.length&&c.shift(),a=1<c.length?c[1]:c[0],a){case"cfg":break;case"reg":this.fastupdate=!1,this.reg=json_to_reg(b,this.reg);break;case"map":this.map=json_to_map(b,this.map);break;case"ctx":this.ctx=json_to_ctx(b,this.ctx);}}}export function exportDocument(a,b,c=0,d=0){if(c<this.field.length){const b=this.field[c],e=this.index.get(b),f=e.export(a,b,c,d=1);if(f&&f.then){const d=this;return f.then(function(){return d.export(a,b,c+1)})}return this.export(a,b,c+1)}else{let e,f;switch(d){case 0:e="reg",f=reg_to_json(this.reg),b=null;break;case 1:e="tag",f=this.tag&&ctx_to_json(this.tag,this.reg.size),b=null;break;case 2:e="doc",f=this.store&&map_to_json(this.store),b=null;break;default:return;}return save.call(this,a,b,e,f||null,c,d)}}export function importDocument(a,b){const c=a.split(".");"json"===c[c.length-1]&&c.pop();const d=2<c.length?c[0]:"",e=2<c.length?c[2]:c[1];if(this.worker&&d)return this.index.get(d).import(a);if(b)if("string"==typeof b&&(b=JSON.parse(b)),!d)switch(e){case"reg":this.fastupdate=!1,this.reg=json_to_reg(b,this.reg);for(let a,b=0;b<this.field.length;b++)a=this.index.get(this.field[b]),a.fastupdate=!1,a.reg=this.reg;if(this.worker){const b=[],c=this;for(const c of this.index.values())b.push(c.import(a));return Promise.all(b)}break;case"tag":this.tag=json_to_ctx(b,this.tag);break;case"doc":this.store=json_to_map(b,this.store);break;case"cfg":}else return this.index.get(d).import(e,b)}export function serialize(a=!0){let b="",c="",d="";if(this.reg.size){let a;for(const c of this.reg.keys())a||(a=typeof c),b+=(b?",":"")+("string"===a?"\""+c+"\"":c);b="index.reg=new Set(["+b+"]);",c=parse_map(this.map,a),c="index.map=new Map(["+c+"]);";for(const b of this.ctx.entries()){const c=b[0],e=b[1];let f=parse_map(e,a);f="new Map(["+f+"])",f="[\""+c+"\","+f+"]",d+=(d?",":"")+f}d="index.ctx=new Map(["+d+"]);"}return a?"function inject(index){"+b+c+d+"}":b+c+d}function parse_map(a,b){let c="";for(const d of a.entries()){const a=d[0],e=d[1];let f="";for(let a,c=0;c<e.length;c++){a=e[c]||[""];let d="";for(let c=0;c<a.length;c++)d+=(d?",":"")+("string"===b?"\""+a[c]+"\"":a[c]);d="["+d+"]",f+=(f?",":"")+d}f="[\""+a+"\",["+f+"]]",c+=(c?",":"")+f}return c}