import { articleTreeMock, paragraphRowsMock } from '../mock'
import { fetchLayoutStyleResult, mapLayoutStyleResult } from './layoutStyleService'

/**
 * 对比页接口占位 service
 * 后续后端接入时，只需要替换这里的数据获取方式和字段映射逻辑。
 */
export async function fetchDocumentComparePageData(params = {}) {
  const { fileName = '' } = params
  const response = await fetchDocumentCompareRawData({ fileName })
  return mapDocumentComparePageData(response)
}

/**
 * 原始接口占位：模拟后端可能返回的结构
 * 这里保留为后续分接口时的聚合入口。
 */
export async function fetchDocumentCompareRawData(_params = {}) {
  const layoutStyleRaw = await fetchLayoutStyleResult()

  return Promise.resolve({
    file_info: {
      file_name: '测试文档.pdf',
      file_type: 'pdf'
    },
    translation_progress: 12,
    article_tree: articleTreeMock,
    paragraph_rows: paragraphRowsMock,
    layout_style_result: layoutStyleRaw
  })
}

/**
 * 将后端返回结构映射为页面可直接使用的数据结构。
 */
export function mapDocumentComparePageData(rawData) {
  const articleTree = Array.isArray(rawData?.article_tree) ? rawData.article_tree : []
  const paragraphRows = Array.isArray(rawData?.paragraph_rows) ? rawData.paragraph_rows : []
  const layoutStyleResult = rawData?.layout_style_result
    ? mapLayoutStyleResult(rawData.layout_style_result)
    : null

  return {
    fileName: rawData?.file_info?.file_name || '',
    fileType: rawData?.file_info?.file_type || 'pdf',
    translationProgress: Number(rawData?.translation_progress) || 0,
    articleTree,
    paragraphRows,
    layoutStyleResult
  }
}
