// Viewer HTML render — serves a single paste row as a complete HTML page.
// Mirrors agim-viewer's render.js so the look/feel is identical whether
// the operator runs the standalone service or this in-process route.
function escapeHtml(s) {
    if (s == null)
        return '';
    return String(s)
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;');
}
function fmtDate(unix) {
    return new Date(unix * 1000).toISOString().replace('T', ' ').replace(/\..+$/, ' UTC');
}
export function renderPasteHtml(row) {
    const isMd = row.content_type !== 'plain';
    const title = row.title || (isMd ? 'agim · markdown' : 'agim · plaintext');
    // Intentionally do NOT render `source` (thread_key). It contains the IM
    // user id (wxid / telegram id), and the /v/:id URL is anyone-with-link.
    // Source stays in the DB for the operator's own dashboard.
    const created = fmtDate(row.created_at);
    const body = isMd
        ? `<pre id="md-src" hidden>${escapeHtml(row.content)}</pre><article id="rendered" class="prose"></article>`
        : `<pre class="plain">${escapeHtml(row.content)}</pre>`;
    return `<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<meta name="robots" content="noindex,nofollow">
<meta name="referrer" content="no-referrer">
<title>${escapeHtml(title)}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@5.6.1/github-markdown.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.10.0/styles/github.min.css" media="(prefers-color-scheme: light)" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.10.0/styles/github-dark.min.css" media="(prefers-color-scheme: dark)" crossorigin="anonymous">
<style>
:root{color-scheme:light dark}
*{box-sizing:border-box}
html,body{margin:0;padding:0;background:#fafbfc;color:#1f2328;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"}
.wrap{max-width:860px;margin:0 auto;padding:16px}
header{display:flex;align-items:center;gap:12px;padding:10px 12px;background:#fff;border:1px solid #d0d7de;border-radius:8px;margin-bottom:16px;font-size:13px;color:#656d76}
header .title{flex:1;font-weight:600;color:#1f2328;font-size:14px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
header .source{color:#0969da;font-size:12px}
header .meta{font-size:12px;color:#656d76}
header button{background:#f6f8fa;border:1px solid #d0d7de;border-radius:6px;padding:5px 10px;font-size:12px;cursor:pointer;color:#1f2328}
header button:hover{background:#eef0f3}
/* github-markdown.css already styles .markdown-body for both color schemes.
   We only override what doesn't fit the page chrome: transparent bg (the
   page already has its own bg), horizontal-scroll for wide tables on
   mobile, image cap, and a light vs dark plain-text container. */
.markdown-body{background:transparent;padding:0;font-size:15px;word-wrap:break-word}
.markdown-body table{display:block;overflow-x:auto;white-space:nowrap;font-size:13px}
.markdown-body img{max-width:100%;height:auto}
.plain{background:#fff;border:1px solid #d0d7de;border-radius:8px;padding:16px;white-space:pre-wrap;word-break:break-word;font-family:ui-monospace,SFMono-Regular,"SF Mono",Menlo,monospace;font-size:13px;line-height:1.6}
footer{margin-top:20px;padding:12px;text-align:center;color:#8b949e;font-size:12px}
footer a{color:#0969da;text-decoration:none}
@media (prefers-color-scheme: dark){
  html,body{background:#0d1117;color:#e6edf3}
  header{background:#161b22;border-color:#30363d;color:#8d96a0}
  header .title{color:#e6edf3}
  header button{background:#21262d;border-color:#30363d;color:#e6edf3}
  header button:hover{background:#30363d}
  .plain{background:#161b22;border-color:#30363d;color:#e6edf3}
}
@media (max-width:600px){
  .wrap{padding:8px}
  header{flex-wrap:wrap;font-size:12px}
  header .meta{flex-basis:100%}
}
</style>
</head>
<body>
<div class="wrap">
  <header>
    <div class="title">${escapeHtml(title)}</div>
    <span class="meta">${created}</span>
    <button id="copy-btn" title="copy raw">📋 Copy</button>
  </header>
  ${body}
  <footer>
    Generated by <a href="https://github.com/benking007/agim" target="_blank" rel="noopener">agim</a>
  </footer>
</div>
${isMd
        ? `
<script src="https://cdn.jsdelivr.net/npm/marked@14.1.3/marked.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify@3.1.7/dist/purify.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/highlight.js@11.10.0/lib/core.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/highlight.js@11.10.0/lib/common.min.js" crossorigin="anonymous"></script>
<script>
(function(){
  var src = document.getElementById('md-src').textContent;
  var rendered = document.getElementById('rendered');
  rendered.classList.add('markdown-body');
  marked.setOptions({ gfm:true, breaks:false, mangle:false });
  rendered.innerHTML = DOMPurify.sanitize(marked.parse(src));
  rendered.querySelectorAll('pre code').forEach(function(b){
    try { hljs.highlightElement(b); } catch(e){}
  });
})();
</script>
`
        : ''}
<script>
document.getElementById('copy-btn').addEventListener('click', function(){
  var src = ${isMd ? "document.getElementById('md-src').textContent" : "document.querySelector('.plain').textContent"};
  navigator.clipboard.writeText(src).then(function(){
    var b = document.getElementById('copy-btn');
    var orig = b.textContent;
    b.textContent = '✓ Copied';
    setTimeout(function(){ b.textContent = orig; }, 1500);
  });
});
</script>
</body>
</html>`;
}
export function renderNotFoundHtml() {
    return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex,nofollow"><title>Not Found · agim</title><style>body{font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;max-width:680px;margin:60px auto;padding:0 20px;color:#333;line-height:1.6}h1{font-size:24px}.muted{color:#888}</style></head><body><h1>内容不存在</h1><p class="muted">This paste does not exist (or was deleted).</p></body></html>`;
}
//# sourceMappingURL=viewer-render.js.map