import{t as e}from"./ofetch-Dl6dVB_5.mjs";import{t}from"./config-BYRrjYC5.mjs";import{t as n}from"./cache-DUthuMSu.mjs";import{t as r}from"./parse-date-BdYTmLpo.mjs";import{t as i}from"./invalid-parameter-DLv2wPKB.mjs";import{t as a}from"./config-not-found-B9AOp828.mjs";import{load as o}from"cheerio";import s from"iconv-lite";function c(e,t){return e&&=(t&&!/^https?:\/\//.test(t)&&(t=t.startsWith(`//`)?`http:`+t:`http://`+t),new URL(e,t).href),e}async function l(t,n){let r=await e.raw(t,{method:`get`,responseType:`arrayBuffer`,headers:n}),i=Buffer.from(r._data);if(s.decode(i,`utf-8`).includes(`document.location.reload()`)){let a=(r.headers.getSetCookie?.()??[]).map(e=>e.split(`;`)[0]).join(`; `);a&&(r=await e.raw(t,{method:`get`,responseType:`arrayBuffer`,headers:{...n,Cookie:[n.Cookie,a].filter(Boolean).join(`; `)}}),i=Buffer.from(r._data))}return{response:r,responseData:i}}async function u(e,t,n){let{responseData:r}=await l(e,n),i=s.decode(r,t??`utf-8`);if(!i)return{description:`获取详细内容失败`};let a=o(i),c=a(`div#postlist div[id^=post] td[id^=postmessage]`).first();return c.find(`img`).each((e,t)=>{t=a(t),t.attr(`src`)?.endsWith(`none.gif`)&&t.attr(`file`)&&(t.attr(`src`,t.attr(`file`)||t.attr(`zoomfile`)),t.removeAttr(`file`),t.removeAttr(`zoomfile`))}),{description:c.html()}}const d={path:[`/:ver{[7x]}/:cid{[0-9]{2}}/:link{.+}`,`/:ver{[7x]}/:link{.+}`,`/:link{.+}`],name:`Unknown`,maintainers:[`pseudoyu`],handler:f};async function f(e){let d=e.req.param(`link`),f=e.req.param(`ver`)?e.req.param(`ver`).toUpperCase():void 0,p=e.req.param(`cid`);d=d.replace(/:\/\//,`:/`).replace(/:\//,`://`);let m=p===void 0?``:t.discuz.cookies[p];if(m===void 0)throw new a(`缺少对应论坛的cookie.`);let h={Cookie:m},{response:g,responseData:_}=await l(d,h),v=g.headers[`content-type`]||``,y=o(s.decode(_,`utf-8`)),b=v.match(/charset=([^;]*)/)?.[1]??y(`meta[charset]`).attr(`charset`)??y(`meta[http-equiv="Content-Type"]`).attr(`content`)?.split(`charset=`)?.[1];b?.toLowerCase()!==`utf-8`&&(y=o(s.decode(_,b??`utf-8`)));let x=f?`DISCUZ! ${f}`:y(`head > meta[name=generator]`).attr(`content`);if(!x)throw new i(`无法检测 Discuz 版本，请在路由中指定版本参数，如 /discuz/x/ 或 /discuz/7/`);let S;if(x.toUpperCase().startsWith(`DISCUZ! 7`)){let t=y(`tbody[id^="normalthread"] > tr`).slice(0,e.req.query(`limit`)?Number.parseInt(e.req.query(`limit`),10):5).toArray().map(e=>{e=y(e);let t=e.find(`span[id^=thread] a`);return{title:t.text().trim(),link:c(t.attr(`href`),d),pubDate:e.find(`td.author em`).length?r(e.find(`td.author em`).text().trim()):void 0,author:e.find(`td.author cite a`).text().trim()}});S=await Promise.all(t.map(e=>n.tryGet(e.link,async()=>{let{description:t}=await u(e.link,b,h);return e.description=t,e})))}else if(x.toUpperCase().startsWith(`DISCUZ! X`)){let t=y(`tbody[id^="normalthread"] > tr`).slice(0,e.req.query(`limit`)?Number.parseInt(e.req.query(`limit`),10):5).toArray().map(e=>{e=y(e);let t=e.find(`a.xst`);return{title:t.text(),link:c(t.attr(`href`),d),pubDate:e.find(`td.by:nth-child(3) em span`).last().length?r(e.find(`td.by:nth-child(3) em span`).last().text().trim()):void 0,author:e.find(`td.by:nth-child(3) cite a`).text().trim()}});S=await Promise.all(t.map(e=>n.tryGet(e.link,async()=>{let{description:t}=await u(e.link,b,h);return e.description=t,e})))}else throw new i(`不支持当前Discuz版本.`);return{title:y(`head > title`).text(),description:y(`head > meta[name=description]`).attr(`content`),link:d,item:S}}export{d as route};