import { apiRequest } from './api.js'
import { normalizeAttachmentAssociationJob } from '../utils/attachmentAssociationJobModel.js'

export { normalizeAttachmentAssociationJob } from '../utils/attachmentAssociationJobModel.js'

function normalizeJobId(jobId) {
  return String(jobId || '').trim()
}

export async function createAttachmentAssociationJob(payload = {}) {
  const job = await apiRequest('/reimbursements/attachment-association-jobs', {
    method: 'POST',
    body: JSON.stringify(payload || {})
  })
  return normalizeAttachmentAssociationJob(job)
}

export async function fetchAttachmentAssociationJob(jobId) {
  const normalizedJobId = normalizeJobId(jobId)
  if (!normalizedJobId) {
    throw new Error('附件关联任务不存在或已失效。')
  }
  const job = await apiRequest(`/reimbursements/attachment-association-jobs/${encodeURIComponent(normalizedJobId)}`)
  return normalizeAttachmentAssociationJob(job)
}
