import{t as e}from"./ofetch-Dl6dVB_5.mjs";import{t}from"./config-BYRrjYC5.mjs";import{t as n}from"./logger-B0fjfmRH.mjs";import{t as r}from"./cache-DUthuMSu.mjs";import{t as i}from"./parse-date-BdYTmLpo.mjs";import{t as a}from"./invalid-parameter-DLv2wPKB.mjs";import{load as o}from"cheerio";const s=e=>e.replace(/-zh-hans$/i,``),c={latest:``,whatsnew:`whatsnew`,"news-brief":`whatsnew`,opinion:`opinion`,international:`international`,mainland:`mainland`,hongkong:`hong-kong`,taiwan:`taiwan`,technology:`technology`,feature:`report`,report:`report`,"daily-brief":`daily-brief`,weekly:`weekly`};function l(e,t){return t===`zh-hans`?`${e}-zh-hans`:e}async function u(t,n={}){let r=new URL(`https://production-initium-media.ghost.io/ghost/api/content/${t}/`);r.searchParams.set(`key`,`a44a0409c222328d39e2c75293`);for(let[e,t]of Object.entries(n))r.searchParams.set(e,t);return await e(r.href)}async function d(t,r){try{let n=o(await e(t,{headers:{Cookie:r},parseResponse:e=>e}))(`article`);return n.length===0||n.find(`.gh-post-upgrade-cta`).length>0?null:n.html()}catch(e){return n.warn(`Failed to scrape Initium article: ${t}`,e),null}}function f(e){let t=o(e,null,!1);return t(`a.kg-bookmark-container, a.kg-bookmark-card`).each((e,n)=>{let r=t(n),i=r.attr(`href`)||``,a=r.find(`.kg-bookmark-title`).text().trim(),o=r.find(`.kg-bookmark-description`).text().trim(),s=a?`<p><a href="${i}">${a}</a>${o?` — ${o}`:``}</p>`:`<p><a href="${i}">${i}</a></p>`;r.replaceWith(s)}),t(`.kg-callout-card`).each((e,n)=>{let r=t(n),i=r.find(`.kg-callout-text`).html()||r.html()||``;r.replaceWith(`<blockquote>${i}</blockquote>`)}),t(`.kg-toggle-card`).each((e,n)=>{let r=t(n),i=r.find(`.kg-toggle-heading-text`).text().trim(),a=r.find(`.kg-toggle-content`).html()||``;r.replaceWith(`${i?`<p><strong>${i}</strong></p>`:``}${a}`)}),t(`.kg-card`).each((e,n)=>{let r=t(n);r.replaceWith(r.html()||``)}),t(`figure.kg-bookmark-card`).each((e,n)=>{let r=t(n);r.replaceWith(r.html()||``)}),t(`p:contains("<!--members-only-->")`).remove(),t.html()}async function p(e){let n=t.initium?.memberCookie;return await Promise.all(e.map(async e=>{let a=e.authors?.map(e=>s(e.name))??[],o=e.tags?.filter(e=>e.visibility===`public`).map(e=>s(e.name))??[],c=e.html?f(e.html):e.html;if(!e.access&&n){let i=await r.tryGet(`theinitium:full:${e.slug}`,()=>d(e.url,n),t.cache.contentExpire);i&&(c=f(i))}return{title:e.title,author:a.join(`, `)||e.primary_author?.name||``,category:o,description:c,link:e.url,pubDate:i(e.published_at),updated:i(e.updated_at),guid:e.uuid,banner:e.feature_image??void 0}}))}const m=async(e,n)=>{let i=n.req.param(`type`)??`latest`,o=n.req.param(`language`)??``,d=``,f=``,m=``;switch(e){case`channel`:{let e=c[i]??i;e===``?(o===`zh-hans`||o===`zh-hant`)&&(d=`tag:hash-${o}`):d=`tag:${o?l(e,o):e}`,f=i===`latest`?`https://theinitium.com/latest/`:`https://theinitium.com/tag/${e}/`,m=i;break}case`tags`:d=`tag:${o?l(i,o):i}`,f=`https://theinitium.com/tag/${i}/`,m=i;break;case`author`:d=`author:${o===`zh-hans`?`${i}-zh-hans`:i}`,f=`https://theinitium.com/author/${i}/`,m=i;break;default:throw new a(`Unsupported model: ${e}`)}let h=`theinitium:ghost:${e}:${i}:${o}`,g=await r.tryGet(h,async()=>{let e={include:`tags,authors`,limit:`20`};return d&&(e.filter=d),await u(`posts`,e)},t.cache.routeExpire,!1),_=await p(g.posts),v=m;if(g.posts.length>0)switch(e){case`channel`:{let e=c[i]??i;if(e){let t=o===`zh-hans`?`${e}-zh-hans`:e,n=g.posts[0].tags?.find(n=>n.slug===t||n.slug===e);n&&(v=s(n.name))}else v=`最新`;break}case`tags`:{let e=o===`zh-hans`?`${i}-zh-hans`:i,t=g.posts[0].tags?.find(t=>t.slug===e||t.slug===i);t&&(v=s(t.name));break}case`author`:{let e=o===`zh-hans`?`${i}-zh-hans`:i,t=g.posts[0].authors?.find(t=>t.slug===e||t.slug===i);t&&(v=s(t.name));break}default:}return{title:`端傳媒 - ${v}`,link:f,icon:`https://theinitium.com/favicon.ico`,language:o===`zh-hans`?`zh-CN`:`zh-TW`,item:_}};export{m as a,p as i,l as n,u as r,c as t};