import { apiRequest } from './api.js'

function clampWindowHours(value) {
  const parsed = Number(value || 24)
  if (!Number.isFinite(parsed)) return 24
  return Math.max(1, Math.min(720, Math.round(parsed)))
}

export function fetchFinancialConnectorObservability(options = {}) {
  const search = new URLSearchParams()
  search.set('window_hours', String(clampWindowHours(options.windowHours)))
  return apiRequest(`/financial-connectors/observability?${search.toString()}`, {
    timeoutMs: Number(options.timeoutMs || 5000),
    timeoutMessage: '财务连接器运行指标加载超时，请稍后重试。'
  })
}

export function fetchFinancialPaymentEvidence(claimId, options = {}) {
  const normalizedClaimId = String(claimId || '').trim()
  if (!normalizedClaimId) {
    return Promise.reject(new Error('报销单 ID 不能为空。'))
  }
  return apiRequest(`/financial-connectors/payment-evidence/${encodeURIComponent(normalizedClaimId)}`, {
    timeoutMs: Number(options.timeoutMs || 5000),
    timeoutMessage: '付款证据等级加载超时，请稍后重试。'
  })
}
