var l=(r,s,o)=>new Promise((t,a)=>{var e=u=>{try{y(o.next(u))}catch(c){a(c)}},i=u=>{try{y(o.throw(u))}catch(c){a(c)}},y=u=>u.done?t(u.value):Promise.resolve(u.value).then(e,i);y((o=o.apply(r,s)).next())});function d(r){return r.replace(/[A-Z]/g,s=>"_"+s.toLowerCase()).replace(/^_/,"")}function A(r){let s={};for(let t in r)t.startsWith("on")&&(s[d(t.slice(2))]=r[t],s[t]=r[t]);let o=a=>l(this,[a],function*({data:t}){if(s[t.type])try{let e=yield s[t.type](t.data);if(e!=null&&e.$transfer&&Array.isArray(e.$transfer)){let i=e.$transfer;delete e.$transfer,self.postMessage({type:`${t.type}_result`,key:t.key,data:e},i)}else self.postMessage({type:`${t.type}_result`,key:t.key,data:e})}catch(e){self.postMessage({type:`${t.type}_result`,key:t.key,error:e})}else self.postMessage({type:`${t.type}_result`,key:t.key,error:"no_event"})});return self.addEventListener("message",o),()=>{self.removeEventListener("message",o)}}var _;function T(r="aibee_map",s=6){return new Promise((o,t)=>{if(_)return o({db:_,type:"success"});let a=indexedDB.open(r,s);a.onupgradeneeded=function(e){_=e.target.result,console.log("[open db is onupgradeneeded]"),f("map_version"),f("route_version"),f("facility_version"),f("map_data"),f("road_network_data"),f("facility_data"),f("poi_text"),f("lazy_texture"),f("lazy_poi")},a.onsuccess=e=>{_=e.target.result,o({db:_,type:"success"})},a.onerror=e=>{console.log("[open db is onerror]"),t(e)}})}function x(r,s,o=_){return new Promise((t,a)=>{o||a("\u6CA1\u6709db");let i=o.transaction([r],"readonly").objectStore(r).get(s);i.onsuccess=()=>{i.result?t(i.result.value):t(null)},i.onerror=y=>{console.log("[getVersionByKey]",y),t(null)}})}function f(r,s=_){s&&(s.objectStoreNames.contains(r)||s.createObjectStore(r,{keyPath:"key"}))}A({onGetTextureByTexts(s){return l(this,arguments,function*({texts:r}){let{db:o}=yield T(),a=o.transaction(["lazy_poi"],"readonly").objectStore("lazy_poi").openCursor(),e={},i=new Set,y={};return new Promise(u=>{let c=()=>l(this,null,function*(){yield Promise.all(Object.values(y).map(n=>l(this,null,function*(){if(e[n.textureKey])e[n.textureKey].map[n.key]=n;else{let p=yield x("lazy_texture",n.textureKey);if(p){let{width:g,height:b,data:m}=p;e[n.textureKey]={width:g,height:b,data:Uint8Array.from(m),map:{[n.key]:n}}}else i.add(n.textureKey)}}))),u({textureKeyMap:e,$transfer:Object.values(e).map(n=>n.data.buffer)})});a.onsuccess=n=>l(this,null,function*(){var g;let p=(g=n.target)==null?void 0:g.result;if(p){let b=p.key,m=p.value.value;r.includes(b)&&(y[b]=m),p.continue()}else console.log("\u6240\u6709\u6570\u636E\u8BFB\u53D6\u5B8C\u6BD5"),c()})})})},onSaveTextTextures(s){return l(this,arguments,function*({data:r}){return new Promise(o=>l(this,null,function*(){let{db:t}=yield T(),a=t.transaction(["lazy_texture"],"readwrite"),e=t.transaction(["lazy_poi"],"readwrite"),i=a.objectStore("lazy_texture"),y=e.objectStore("lazy_poi"),u=[];for(let c in r){let{textureData:n,res:p}=r[c];i.add({key:c,value:n}),p.forEach(g=>{y.add({key:g.key,value:g})})}u.push(new Promise((c,n)=>{a.oncomplete=c,a.onerror=n}),new Promise((c,n)=>{e.oncomplete=c,e.onerror=n})),Promise.all(u).then(()=>{o({status:!0})}).catch(c=>{console.error(c),a.abort(),e.abort()})}))})}});
