import{_ as B,S as j,o as r,c as d,a,t as c,F as M,b as $,d as f,f as D,n as I}from"./index-e263e3d6.js";class R{constructor(){this.chunkSize=1024*1024,this.maxRetries=3,this.downloads=new Map}async checkFileSize(t){try{const s=await fetch(t,{method:"HEAD"});if(!s.ok)throw new Error("无法获取文件信息");const l=s.headers.get("Content-Length"),o=l?parseInt(l):0,i=s.headers.get("Accept-Ranges")==="bytes";return{fileSize:o,supportsRanges:i,needsChunking:o>this.chunkSize&&i}}catch(s){return console.warn("无法检查文件大小,将使用普通下载:",s),{fileSize:0,supportsRanges:!1,needsChunking:!1}}}async downloadChunk(t,s,l,o=0){try{const i=await fetch(t,{headers:{Range:`bytes=${s}-${l}`}});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);return await i.arrayBuffer()}catch(i){if(o<this.maxRetries)return console.warn(`分片下载失败,重试 ${o+1}/${this.maxRetries}:`,i),await new Promise(n=>setTimeout(n,1e3*(o+1))),this.downloadChunk(t,s,l,o+1);throw i}}async downloadFileInChunks(t,s,l=null){const o=Date.now().toString();try{const i=await this.checkFileSize(t);if(!i.needsChunking)return console.log("文件不需要分片下载,使用普通下载"),await this.downloadFileDirect(t,s,l);console.log("开始分片下载:",{url:t,fileName:s,fileSize:this.formatFileSize(i.fileSize),chunkSize:this.formatFileSize(this.chunkSize)});const n=Math.ceil(i.fileSize/this.chunkSize),u=[];this.downloads.set(o,{url:t,fileName:s,totalChunks:n,downloadedChunks:0,cancelled:!1});const y=Date.now();for(let h=0;h<n;h++){const w=this.downloads.get(o);if(w!=null&&w.cancelled)throw new Error("下载已取消");const g=h*this.chunkSize,S=Math.min(g+this.chunkSize-1,i.fileSize-1);if(l){const z=Math.round(h/n*100),_=(Date.now()-y)/1e3,p=h>0?this.formatFileSize(h*this.chunkSize/_)+"/s":"";l({progress:z,currentChunk:h+1,totalChunks:n,status:`下载分片 ${h+1}/${n}`,speed:p,elapsed:_})}const m=await this.downloadChunk(t,g,S);u[h]=m,w&&(w.downloadedChunks=h+1)}l&&l({progress:95,status:"正在合并文件...",currentChunk:n,totalChunks:n});const k=this.mergeChunks(u);return l&&l({progress:100,status:"下载完成",currentChunk:n,totalChunks:n}),this.saveFile(k,s),this.downloads.delete(o),{success:!0,downloadId:o,fileSize:i.fileSize,method:"chunked"}}catch(i){throw console.error("分片下载失败:",i),this.downloads.delete(o),i}}async downloadFileDirect(t,s,l=null){try{l&&l({progress:0,status:"开始下载..."});const o=await fetch(t);if(!o.ok)throw new Error(`HTTP ${o.status}: ${o.statusText}`);l&&l({progress:50,status:"正在下载..."});const i=await o.blob();return l&&l({progress:100,status:"下载完成"}),this.saveFile(i,s),{success:!0,fileSize:i.size,method:"direct"}}catch(o){throw console.error("直接下载失败:",o),o}}mergeChunks(t){const s=t.reduce((i,n)=>i+n.byteLength,0),l=new Uint8Array(s);let o=0;for(const i of t)l.set(new Uint8Array(i),o),o+=i.byteLength;return new Blob([l])}saveFile(t,s){const l=URL.createObjectURL(t),o=document.createElement("a");o.href=l,o.download=s,o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o),setTimeout(()=>URL.revokeObjectURL(l),100)}cancelDownload(t){const s=this.downloads.get(t);return s?(s.cancelled=!0,console.log("下载已取消:",t),!0):!1}getDownloadStatus(t){return this.downloads.get(t)||null}formatFileSize(t){if(!t||t===0)return"0 B";const s=1024,l=["B","KB","MB","GB","TB"],o=Math.floor(Math.log(t)/Math.log(s));return parseFloat((t/Math.pow(s,o)).toFixed(2))+" "+l[o]}async downloadFile(t,s,l=null){try{console.log("开始下载文件:",{url:t,fileName:s});const o=await this.checkFileSize(t);return o.needsChunking?(console.log(`文件大小 ${this.formatFileSize(o.fileSize)} 需要分片下载`),await this.downloadFileInChunks(t,s,l)):(console.log("文件适合直接下载"),await this.downloadFileDirect(t,s,l))}catch(o){throw console.error("下载文件失败:",o),o}}}const T=new R;const U={name:"PublicationsView",data(){return{publications:[],loading:!0,error:null,downloadStates:{}}},async created(){await this.loadPublications()},methods:{async loadPublications(){try{this.loading=!0,this.error=null,console.log("开始加载出版物数据...");let e=await j.getPublications({status:"published",orderBy:"published_at",ascending:!1});if(console.log("获取到的出版物数据:",e),!e||e.length===0){console.log("没有published状态的数据,尝试获取所有数据...");const t=await j.getPublications({orderBy:"created_at",ascending:!1,limit:100});console.log("所有状态的出版物数据:",t),e=t}this.publications=e||[],this.publications.length===0?console.log("数据库中没有出版物数据"):(console.log(`成功加载 ${this.publications.length} 个出版物`),console.log("第一个出版物的数据结构:",this.publications[0]))}catch(e){console.error("加载出版物失败:",e),this.error=this.$i18n.locale==="zh"?"加载出版物失败,请稍后重试":"Failed to load publications, please try again later"}finally{this.loading=!1}},getTitle(e){if(console.log("获取标题:",e.title,typeof e.title),!e.title)return"无标题";if(typeof e.title=="object"){if(console.log("标题是对象,详细结构:",JSON.stringify(e.title,null,2)),e.title.en&&typeof e.title.en=="object"&&e.title.zh&&typeof e.title.zh=="object"){console.log("检测到双重嵌套结构");const s=e.title[this.$i18n.locale]||e.title.zh||e.title.en;if(console.log("选择的外层语言数据:",s),s&&typeof s=="object"){const l=s[this.$i18n.locale]||s.zh||s.en;return console.log("解析的双重嵌套标题:",l),l||"无标题"}}if(e.title.en&&typeof e.title.en=="object"){console.log("检测到单层嵌套结构");const s=e.title[this.$i18n.locale]||e.title.zh||e.title.en;if(console.log("选择的语言数据:",s),s&&typeof s=="object"){const l=s.title||s.name||s.text||Object.values(s)[0];return console.log("解析的嵌套标题:",l),l||"无标题"}}const t=e.title[this.$i18n.locale]||e.title.zh||e.title.en||e.title.title||"无标题";if(console.log("解析的简单标题:",t),typeof t=="object"){console.log("标题仍然是对象,尝试进一步解析:",t);const s=t.title||t.name||t.text||Object.values(t)[0];return console.log("最终解析的标题:",s),s||"无标题"}return t}return e.title},getDescription(e){if(console.log("获取描述:",e.description,typeof e.description),!e.description)return"";if(typeof e.description=="object"){if(console.log("描述是对象,详细结构:",JSON.stringify(e.description,null,2)),e.description.en&&typeof e.description.en=="object"&&e.description.zh&&typeof e.description.zh=="object"){console.log("检测到描述双重嵌套结构");const s=e.description[this.$i18n.locale]||e.description.zh||e.description.en;if(console.log("选择的外层描述语言数据:",s),s&&typeof s=="object"){const l=s[this.$i18n.locale]||s.zh||s.en;return console.log("解析的双重嵌套描述:",l),(l||"").toString().trim()}}if(e.description.en&&typeof e.description.en=="object"){console.log("检测到描述单层嵌套结构");const s=e.description[this.$i18n.locale]||e.description.zh||e.description.en;if(console.log("选择的描述语言数据:",s),s&&typeof s=="object"){const l=s.description||s.desc||s.text||s.content||Object.values(s)[0];return console.log("解析的嵌套描述:",l),(l||"").toString().trim()}}const t=e.description[this.$i18n.locale]||e.description.zh||e.description.en||e.description.description||"";if(console.log("解析的简单描述:",t),typeof t=="object"){console.log("描述仍然是对象,尝试进一步解析:",t);const s=t.description||t.desc||t.text||t.content||Object.values(t)[0];return console.log("最终解析的描述:",s),(s||"").toString().trim()}return t.toString().trim()}return e.description.toString().trim()},formatDate(e){if(!e)return"";const t=new Date(e);return new Intl.DateTimeFormat(this.$i18n.locale==="zh"?"zh-CN":"en-US",{year:"numeric",month:"long",day:"numeric",timeZone:"Asia/Shanghai"}).format(t)},formatFileSize(e){if(!e)return"";const t=["Bytes","KB","MB","GB"],s=Math.floor(Math.log(e)/Math.log(1024));return Math.round(e/Math.pow(1024,s)*100)/100+" "+t[s]},getFileName(e){if(e.file_name)return e.file_name;let t="";if(!e.title)t="untitled";else if(typeof e.title=="object")if(e.title.en&&typeof e.title.en=="object"){const o=e.title[this.$i18n.locale]||e.title.zh||e.title.en;o&&typeof o=="object"&&(t=Object.values(o)[0]||"untitled")}else t=e.title[this.$i18n.locale]||e.title.zh||e.title.en||"untitled";else t=e.title;const s=t.replace(/[^\w\s-]/g,"").replace(/\s+/g,"-"),l=e.file_type||"pdf";return`${s}.${l}`},getDownloadText(e){return e.file_type&&e.file_type.toUpperCase(),this.$i18n.locale==="zh"?"下载":"Download"},isValidDownloadUrl(e){if(!e||typeof e!="string")return!1;try{return new URL(e),!0}catch{return e.startsWith("/")||e.startsWith("./")||e.startsWith("../")?!0:(!e.includes("/")&&!e.includes("\\")&&!e.startsWith("http"),!1)}},async downloadFile(e){const t=e.id;try{this.downloadStates[t]={isDownloading:!0,progress:0,status:"准备下载...",error:null,completed:!1,downloadId:null,chunks:!1,currentChunk:0,totalChunks:0,speed:""};const s=this.getFileName(e);console.log("开始下载:",{url:e.file_url,fileName:s});const l=await T.downloadFile(e.file_url,s,o=>{this.downloadStates[t]={...this.downloadStates[t],progress:o.progress,status:o.status||"下载中...",chunks:o.totalChunks>1,currentChunk:o.currentChunk||0,totalChunks:o.totalChunks||0,speed:o.speed||""}});this.downloadStates[t]={...this.downloadStates[t],isDownloading:!1,completed:!0,progress:100,status:"下载完成",downloadId:l.downloadId},console.log("下载完成:",l)}catch(s){console.error("下载失败:",s),this.downloadStates[t]={...this.downloadStates[t],isDownloading:!1,error:s.message,progress:0}}},cancelDownload(e){const t=this.downloadStates[e];t&&t.downloadId&&T.cancelDownload(t.downloadId),this.resetDownloadState(e)},resetDownloadState(e){delete this.downloadStates[e]}}},V={class:"publications"},L={class:"page-header"},x={class:"publications-section"},O={class:"container"},E={key:0,class:"loading-message"},A={key:1,class:"error-message"},N={key:2,class:"empty-message"},W={key:3,class:"publications-list"},H={class:"publication-content"},G={class:"publication-info"},J={class:"publication-title"},K={class:"publication-meta"},Z={key:0,class:"author"},q={class:"date"},P={key:1,class:"category"},Q={key:2,class:"file-type"},X={key:3,class:"file-size"},Y={key:0,class:"description"},b={class:"publication-actions"},ee={key:0,class:"download-section"},te=["onClick"],se={key:1,class:"download-progress"},oe={class:"progress-header"},ne={class:"progress-text"},le={class:"progress-percent"},ie={class:"progress-bar"},ae={key:0,class:"chunk-details"},re={key:0},de=["onClick"],ce={key:2,class:"download-completed"},he=["onClick"],fe={key:3,class:"download-error"},we=["onClick"],ue={key:1,class:"file-info"},ge={class:"file-header"},_e={class:"file-name"},ye={class:"file-note"},ke={key:2,class:"no-download"},Se={class:"submit-publication"},me={class:"container"},ze={key:0},pe={key:1},ve={href:"mailto:[email protected]",class:"contact-button"};function De(e,t,s,l,o,i){return r(),d("div",V,[a("header",L,[a("h1",null,c(e.$t("nav.publications")),1)]),a("section",x,[a("div",O,[o.loading?(r(),d("div",E,c(e.$i18n.locale==="zh"?"正在加载出版物...":"Loading publications..."),1)):o.error?(r(),d("div",A,c(o.error),1)):o.publications.length===0?(r(),d("div",N,c(e.$i18n.locale==="zh"?"暂无出版物":"No publications available"),1)):(r(),d("div",W,[(r(!0),d(M,null,$(o.publications,n=>{var u,y,k,h,w,g,S,m,z,_,p,C,F;return r(),d("div",{key:n.id,class:"publication-card"},[f("",!0),a("div",H,[a("div",G,[a("h2",J,c(i.getTitle(n)),1),a("div",K,[n.author?(r(),d("span",Z,c(n.author),1)):f("",!0),a("span",q,c(i.formatDate(n.published_date||n.published_at||n.created_at)),1),n.category?(r(),d("span",P,c(n.category),1)):f("",!0),n.file_type?(r(),d("span",Q,c(n.file_type.toUpperCase()),1)):f("",!0),n.file_size?(r(),d("span",X,c(i.formatFileSize(n.file_size)),1)):f("",!0)]),i.getDescription(n)?(r(),d("p",Y,c(i.getDescription(n)),1)):f("",!0)]),a("div",b,[i.isValidDownloadUrl(n.file_url)?(r(),d("div",ee,[(u=o.downloadStates[n.id])!=null&&u.isDownloading?f("",!0):(r(),d("button",{key:0,onClick:v=>i.downloadFile(n),class:"download-button"},[t[17]||(t[17]=a("i",{class:"download-icon"},"📥",-1)),D(" "+c(i.getDownloadText(n)),1)],8,te)),(y=o.downloadStates[n.id])!=null&&y.isDownloading?(r(),d("div",se,[a("div",oe,[a("span",ne,c(((k=o.downloadStates[n.id])==null?void 0:k.status)||"准备下载..."),1),a("span",le,c(((h=o.downloadStates[n.id])==null?void 0:h.progress)||0)+"%",1)]),a("div",ie,[a("div",{class:"progress-fill",style:I({width:(((w=o.downloadStates[n.id])==null?void 0:w.progress)||0)+"%"})},null,4)]),(g=o.downloadStates[n.id])!=null&&g.chunks?(r(),d("div",ae,[a("span",null,"分片 "+c(((S=o.downloadStates[n.id])==null?void 0:S.currentChunk)||0)+" / "+c(((m=o.downloadStates[n.id])==null?void 0:m.totalChunks)||0),1),(z=o.downloadStates[n.id])!=null&&z.speed?(r(),d("span",re,c((_=o.downloadStates[n.id])==null?void 0:_.speed),1)):f("",!0)])):f("",!0),a("button",{onClick:v=>i.cancelDownload(n.id),class:"cancel-download-btn"}," 取消下载 ",8,de)])):f("",!0),(p=o.downloadStates[n.id])!=null&&p.completed?(r(),d("div",ce,[t[18]||(t[18]=D(" ✅ 下载完成! ")),a("button",{onClick:v=>i.resetDownloadState(n.id),class:"reset-download-btn"}," 重新下载 ",8,he)])):f("",!0),(C=o.downloadStates[n.id])!=null&&C.error?(r(),d("div",fe,[D(" ❌ 下载失败: "+c((F=o.downloadStates[n.id])==null?void 0:F.error)+" ",1),a("button",{onClick:v=>i.resetDownloadState(n.id),class:"retry-download-btn"}," 重试 ",8,we)])):f("",!0)])):n.file_url&&n.file_url.trim()!==""?(r(),d("div",ue,[a("div",ge,[t[19]||(t[19]=a("i",{class:"file-icon"},"📄",-1)),a("span",_e,c(n.file_url),1)]),a("small",ye,c(e.$i18n.locale==="zh"?"(文件未上传到服务器,仅存储文件名)":"(File not uploaded to server, filename only)"),1)])):(r(),d("div",ke,c(e.$i18n.locale==="zh"?"暂无文件下载":"No file available for download"),1))])])])}),128))]))])]),a("section",Se,[a("div",me,[a("h2",null,c(e.$i18n.locale==="zh"?"提交出版物":"Submit a Publication"),1),e.$i18n.locale==="zh"?(r(),d("p",ze," 如果您有想要分享的关于公民权利的研究、文章或其他资源,欢迎与我们联系。我们很乐意审阅您的材料并考虑将其添加到我们的出版物库中。 ")):(r(),d("p",pe," If you have research, articles, or other resources about citizens' rights that you would like to share, we welcome you to contact us. We would be happy to review your materials and consider adding them to our publications library. ")),a("a",ve,c(e.$i18n.locale==="zh"?"联系我们":"Contact Us"),1)])])])}const Fe=B(U,[["render",De],["__scopeId","data-v-a5a1dad9"]]);export{Fe as default};