import{o as S,a as f}from"./axios-CvdON-CO.js";import{k as i,s as O}from"./index-BBhBboPS.js";const h="ClassworksDB",E=2,N=async()=>S(h,E,{upgrade(e){e.objectStoreNames.contains("kv")||e.createObjectStore("kv"),e.objectStoreNames.contains("system")||e.createObjectStore("system")}}),D={async loadData(e){try{const s=await(await N()).get("kv",e);return s?d(JSON.parse(s)):n("数据不存在","NOT_FOUND")}catch(r){return n("读取本地数据失败："+r)}},async saveData(e,r){try{return await(await N()).put("kv",JSON.stringify(r),e),d(!0)}catch(s){return n("保存本地数据失败："+s)}},async loadKeys(e={}){try{const t=await(await N()).transaction(["kv"],"readonly").objectStore("kv").getAllKeys(),{sortDir:c="asc",limit:l=100,skip:u=0}=e,v=t.sort((o,m)=>c==="desc"?m.localeCompare(o):o.localeCompare(m)),y=v.length,p=v.slice(u,u+l),g={keys:p,total_rows:y,current_page:{limit:l,skip:u,count:p.length},load_more:null};return d(g)}catch(r){return n("获取本地键名列表失败："+r.message)}}},k=()=>{const e={Accept:"application/json"},r=i("server.kvToken"),s=i("server.siteKey");return r?e["x-app-token"]=r:s&&(e["x-site-key"]=s),e},R={async loadNamespaceInfo(){var e,r;try{const s=i("server.domain"),a=await f.get(`${s}/kv/_info`,{headers:k()});return d(a.data)}catch(s){return console.error("获取命名空间信息失败:",s),n(((r=(e=s.response)==null?void 0:e.data)==null?void 0:r.message)||"获取命名空间信息失败","NAMESPACE_ERROR")}},async updateNamespaceInfo(e){var r,s;try{const a=i("server.domain");return await f.put(`${a}/kv/_info`,e,{headers:k()})}catch(a){return n(((s=(r=a.response)==null?void 0:r.data)==null?void 0:s.message)||"更新命名空间信息失败","NAMESPACE_ERROR")}},async loadData(e){var r,s,a;try{const t=i("server.domain"),c=await f.get(`${t}/kv/${e}`,{headers:k()});return d(c.data)}catch(t){return((r=t.response)==null?void 0:r.status)===404?n("数据不存在","NOT_FOUND"):(console.log(t),n(((a=(s=t.response)==null?void 0:s.data)==null?void 0:a.message)||"服务器连接失败","NETWORK_ERROR"))}},async saveData(e,r){var s,a;try{const t=i("server.domain");return await f.post(`${t}/kv/${e}`,r,{headers:k()}),d(!0)}catch(t){return console.log(t),n(((a=(s=t.response)==null?void 0:s.data)==null?void 0:a.message)||"保存失败","SAVE_ERROR")}},async loadKeys(e={}){var r,s,a,t,c;try{const l=i("server.domain"),{sortBy:u="key",sortDir:v="asc",limit:y=100,skip:p=0}=e,g=new URLSearchParams({sortBy:u,sortDir:v,limit:y.toString(),skip:p.toString()}),o=await f.get(`${l}/kv/_keys?${g}`,{headers:k()});return d(o.data)}catch(l){return((r=l.response)==null?void 0:r.status)===404?n("命名空间不存在","NOT_FOUND"):((s=l.response)==null?void 0:s.status)===403?n("无权限访问此命名空间","PERMISSION_DENIED"):((a=l.response)==null?void 0:a.status)===401?n("认证失败","UNAUTHORIZED"):(console.log(l),n(((c=(t=l.response)==null?void 0:t.data)==null?void 0:c.message)||"获取键名列表失败","NETWORK_ERROR"))}}},d=e=>e,n=(e,r="UNKNOWN_ERROR")=>({success:!1,error:{code:r,message:e}}),U={loadData:async e=>{const r=i("server.provider");return r==="kv-server"||r==="classworkscloud"?R.loadData(e):D.loadData(e)},saveData:async(e,r)=>{const s=i("server.provider");return s==="kv-server"||s==="classworkscloud"?R.saveData(e,r):D.saveData(e,r)},loadKeys:async(e={})=>{const r=i("server.provider");return r==="kv-server"||r==="classworkscloud"?R.loadKeys(e):D.loadKeys(e)},async getKeyCloudUrl(e,r={}){var t;const{migrateFromLocal:s=!0,autoConfigureCloud:a=!0}=r;try{let c=i("server.domain"),l=i("server.siteKey");const u=i("device.uuid");let v=!1;if(!c||!u)if(a){const o={"server.domain":"https://kv.wuyuan.dev","server.siteKey":""};c||(O("server.domain",o["server.domain"]),c=o["server.domain"],v=!0),l||(O("server.siteKey",o["server.siteKey"]),l=o["server.siteKey"]),O("server.provider","classworkscloud")}else return n("云端配置无效，请检查服务器域名和设备UUID","CONFIG_ERROR");let y=!1;if(s)try{const o=await D.loadData(e);if(o&&o.success!==!1){const m=await R.loadData(e);if(m&&m.success===!1&&((t=m.error)==null?void 0:t.code)==="NOT_FOUND"){const w=await R.saveData(e,o);w&&w.success!==!1&&(y=!0,console.log(`已成功将键 ${e} 的数据从本地迁移到云端`))}}}catch(o){console.warn(`迁移键 ${e} 的数据时出错:`,o)}const p=i("server.kvToken");return{success:!0,url:`${c}/kv/${e}?token=${p}`,migrated:y,configured:v}}catch(c){return console.error("获取键云端地址时出错:",c),n(c.message||"获取键云端地址失败","CLOUD_URL_ERROR")}}};export{U as d,R as k};
