/**
 * Markdown style optimization for Feishu interactive cards.
 *
 * Adapted from openclaw-lark's card/markdown-style.ts.
 * Optimizes markdown rendering inside Feishu cards:
 * - Heading downgrade: H1→H4, H2-H6→H5
 * - Table spacing with <br> in v2 mode
 * - Strips invalid image keys (not img_xxx format)
 * - Compresses excessive blank lines
 */

export function optimizeMarkdownStyle(text: string, cardVersion: number = 2): string {
  try {
    let r = _optimizeMarkdownStyle(text, cardVersion)
    r = stripInvalidImageKeys(r)
    return r
  } catch {
    return text
  }
}

function _optimizeMarkdownStyle(text: string, cardVersion: number = 2): string {
  const MARK = '___CB_'
  const codeBlocks: string[] = []
  let r = text.replace(/(^|\n)(`{3,})([^\n]*)\n[\s\S]*?\n\2(?=\n|$)/g, (m, prefix = '') => {
    const block = m.slice(String(prefix).length)
    return `${prefix}${MARK}${codeBlocks.push(block) - 1}___`
  })

  const hasH1toH3 = /^#{1,3} /m.test(text)
  if (hasH1toH3) {
    r = r.replace(/^#{2,6} (.+)$/gm, '##### $1')
    r = r.replace(/^# (.+)$/gm, '#### $1')
  }

  if (cardVersion >= 2) {
    r = r.replace(/^(#{4,5} .+)\n{1,2}(#{4,5} )/gm, '$1\n<br>\n$2')
    r = r.replace(/^([^|\n].*)\n(\|.+\|)/gm, '$1\n\n$2')
    r = r.replace(/\n\n((?:\|.+\|[^\S\n]*\n?)+)/g, '\n\n<br>\n\n$1')
    r = r.replace(/((?:^\|.+\|[^\S\n]*\n?)+)/gm, (m, _table, offset) => {
      const after = r.slice(offset + m.length).replace(/^\n+/, '')
      if (!after || /^(---|#{4,5} |\*\*)/.test(after)) return m
      return m + '\n<br>\n'
    })
    r = r.replace(/^((?!#{4,5} )(?!\*\*).+)\n\n(<br>)\n\n(\|)/gm, '$1\n$2\n$3')
    r = r.replace(/^(\*\*.+)\n\n(<br>)\n\n(\|)/gm, '$1\n$2\n\n$3')
    r = r.replace(/(\|[^\n]*\n)\n(<br>\n)((?!#{4,5} )(?!\*\*))/gm, '$1$2$3')

    codeBlocks.forEach((block, i) => {
      r = r.replace(`${MARK}${i}___`, `\n<br>\n${block}\n<br>\n`)
    })
  } else {
    codeBlocks.forEach((block, i) => {
      r = r.replace(`${MARK}${i}___`, block)
    })
  }

  r = r.replace(/\n{3,}/g, '\n\n')
  return r
}

const IMAGE_RE = /!\[([^\]]*)\]\(([^)\s]+)\)/g

function stripInvalidImageKeys(text: string): string {
  if (!text.includes('![')) return text
  return text.replace(IMAGE_RE, (fullMatch, _alt, value) => {
    if (value.startsWith('img_')) return fullMatch
    return ''
  })
}

export function formatElapsed(ms: number): string {
  const seconds = ms / 1000
  return seconds < 60 ? `${seconds.toFixed(1)}s` : `${Math.floor(seconds / 60)}m ${Math.round(seconds % 60)}s`
}