1import{i as c,p as g}from"./index-B0Ia7pcb.js";import"./react-vendor-Bmxl947x.js";import"./utils-vendor-YwL75HfN.js";import"./i18n-vendor-T6WqKCYf.js";const b="/imports/weread",k="1.0.4",y=35e3;async function A(e,s){const i=(await v(e,s)).filter(I);if(i.length===0)throw new Error(c("errors.noWereadExportableNotes"));const n=[];for(let r=0;r<i.length;r++){const t=i[r];s?.({current:r,total:i.length,message:c("progress.wereadBook",{title:t.book?.title??t.bookId})});const a=!f(t.noteCount)||t.noteCount>0,o=!f(t.reviewCount)||t.reviewCount>0,[u,p]=await Promise.all([a?h(e,"/book/bookmarklist",{bookId:t.bookId}):Promise.resolve({book:t.book}),o?E(e,t.bookId):Promise.resolve([])]),d=C(t,u,p);d.content.length>0&&n.push(d)}if(n.length===0)throw new Error(c("errors.noWereadExportableNotes"));return s?.({current:n.length,total:n.length,message:c("progress.wereadComplete",{count:n.length})}),{books:n}}async function v(e,s){const i=[];let n,r;do{s?.({current:i.length,total:null,message:c("progress.wereadNotebooks")});const t=await h(e,"/user/notebooks",{count:100,...n===void 0?{}:{lastSort:n}}),a=Array.isArray(t.books)?t.books:[];if(i.push(...a.filter(o=>typeof o.bookId=="string")),r=t.hasMore===1&&a.length>0,r){const o=a[a.length-1].sort;if(typeof o!="number"||o===n)throw new Error(c("errors.wereadNotebookCursor"));n=o}}while(r);if(i.length===0)throw new Error(c("errors.noWereadNotebooks"));return i}function I(e){return!f(e.noteCount)||!f(e.reviewCount)?!0:e.noteCount>0||e.reviewCount>0}async function E(e,s){const i=[];let n=0,r;do{const t=await h(e,"/review/list/mine",{bookid:s,count:100,synckey:n});if(i.push(...(t.reviews??[]).flatMap(a=>a.review?[a.review]:[])),r=t.hasMore===1,r){if(typeof t.synckey!="number"||t.synckey===n)throw new Error(c("errors.wereadReviewCursor"));n=t.synckey}}while(r);return i}function C(e,s,i){const n=s.book??e.book,r=new Map;for(const o of s.chapters??[])o.chapterUid!==void 0&&typeof o.title=="string"&&r.set(String(o.chapterUid),o.title);const t=new Map,a=(o,u,p)=>{const d=o===void 0?"book":String(o),l=t.get(d)??{chapterUid:typeof o=="string"||typeof o=="number"?o:void 0,chapterTitle:typeof u=="string"?u:r.get(d)??"",items:[]};l.items.push(p),t.set(d,l)};for(const o of s.updated??[])typeof o.markText=="string"&&a(o.chapterUid,void 0,{type:"highlight",bookmarkId:w(o.bookmarkId),markText:o.markText,createTime:m(o.createTime)});for(const o of i)typeof o.content!="string"&&typeof o.abstract!="string"||a(o.chapterUid,o.chapterName,{type:"review",reviewId:w(o.reviewId),content:w(o.content),abstract:w(o.abstract),createTime:m(o.createTime)});return{meta:n?{...n,bookId:n.bookId??e.bookId}:{title:e.bookId,bookId:e.bookId},content:[...t.values()]}}async function h(e,s,i){let n;try{n=await g(b,{api_name:s,...i,skill_version:k},{timeout:y,headers:{"X-WeRead-API-Key":e.trim()}})}catch(t){const a=_(t);throw a==="weread_invalid_key"?new Error(c("errors.wereadUnauthorized")):a==="weread_timeout"?new Error(c("errors.wereadTimeout")):a==="weread_api_forbidden"?new Error(c("errors.wereadApiForbidden")):new Error(c("errors.wereadUnreachable"))}const r=n.data;if(r.upgrade_info)throw new Error(r.upgrade_info.message??c("errors.wereadUpgrade"));if(typeof r.errcode=="number"&&r.errcode!==0)throw new Error(r.errmsg||c("errors.wereadApiError",{code:r.errcode}));return r}function _(e){if(e&&typeof e=="object"){const s=e.response;if(typeof s?.data?.message=="string")return s.data.message}return e instanceof Error?e.message:""}function w(e){return typeof e=="string"?e:void 0}function m(e){return typeof e=="string"||typeof e=="number"?e:void 0}function f(e){return typeof e=="number"&&Number.isFinite(e)&&e>=0}export{A as fetchWereadFromApi};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.