import assert from 'node:assert/strict'

import {
  buildExcelPreviewTable,
  buildPreviewMetaLine,
  buildPreviewSecondaryMetaLine
} from '../src/views/scripts/policiesPreviewFormatters.js'

function testBuildPreviewMetaLineUsesRealDocumentFields() {
  const document = {
    summary: '财务知识库 · XLSX · 10.9 KB',
    time: '2026-05-09 12:30'
  }

  assert.deepEqual(buildPreviewMetaLine(document), ['财务知识库 · XLSX · 10.9 KB', '2026-05-09 12:30'])
}

function testBuildPreviewSecondaryMetaLineForExcelUsesSubtitleAndStats() {
  const document = {
    previewKind: 'table',
    previewPages: [
      {
        subtitle: '表格内容预览',
        stats: [
          { label: '工作表数量', value: '4' },
          { label: '预览行数', value: '7' },
          { label: '文件大小', value: '10.9 KB' }
        ]
      },
      {
        subtitle: '第二页签预览',
        stats: [
          { label: '工作表数量', value: '4' },
          { label: '预览行数', value: '3' }
        ]
      }
    ]
  }

  assert.deepEqual(buildPreviewSecondaryMetaLine(document, document.previewPages[0]), ['表格内容预览', '工作表数量 4', '预览行数 7'])
  assert.deepEqual(buildPreviewSecondaryMetaLine(document, document.previewPages[1]), ['第二页签预览', '工作表数量 4', '预览行数 3'])
}

function testBuildExcelPreviewTableParsesHeaderAndRows() {
  const page = {
    blocks: [
      { heading: '第 1 行', lines: ['日期 | 部门 | 金额 | 备注'] },
      { heading: '第 2 行', lines: ['2026-05-01 | 财务部 | 300 | 差旅'] },
      { heading: '第 3 行', lines: ['2026-05-02 | 行政部 | 120 | '] }
    ]
  }

  assert.deepEqual(buildExcelPreviewTable(page), {
    headers: ['日期', '部门', '金额', '备注'],
    rows: [
      ['2026-05-01', '财务部', '300', '差旅'],
      ['2026-05-02', '行政部', '120', '']
    ]
  })
}

function run() {
  testBuildPreviewMetaLineUsesRealDocumentFields()
  testBuildPreviewSecondaryMetaLineForExcelUsesSubtitleAndStats()
  testBuildExcelPreviewTableParsesHeaderAndRows()
  console.log('policies preview formatter tests passed')
}

run()
