export function useTravelReimbursementCreateViewSessionCleanup({
  SESSION_TYPE_EXPENSE,
  activeSessionType,
  applySessionState,
  buildEmptySessionState,
  clearAssistantSessionSnapshot,
  resetFlowRun,
  resetGuidedFlowState,
  resolveActiveClaimId,
  resolveCurrentUserId,
  sessionSnapshots,
  toast
}) {
  function resetCurrentSessionState() {
    const emptyState = buildEmptySessionState(activeSessionType.value)
    sessionSnapshots.value[activeSessionType.value] = emptyState
    resetGuidedFlowState()
    applySessionState(emptyState)
    resetFlowRun({ startedAt: 0, openDrawer: false })
  }

  function clearExpenseSessionForDeletedClaim(claimId) {
    const normalizedClaimId = String(claimId || '').trim()
    if (!normalizedClaimId) {
      return
    }

    const expenseSnapshot = sessionSnapshots.value[SESSION_TYPE_EXPENSE]
    const snapshotMatchesDeletedClaim = String(expenseSnapshot?.draftClaimId || '').trim() === normalizedClaimId
    const currentMatchesDeletedClaim =
      activeSessionType.value === SESSION_TYPE_EXPENSE
      && String(resolveActiveClaimId() || '').trim() === normalizedClaimId
    if (!snapshotMatchesDeletedClaim && !currentMatchesDeletedClaim) {
      return
    }

    clearAssistantSessionSnapshot(resolveCurrentUserId(), SESSION_TYPE_EXPENSE)
    if (currentMatchesDeletedClaim) {
      resetCurrentSessionState()
      toast('该草稿单据已删除，相关财务助手会话已清空。')
      return
    }

    sessionSnapshots.value[SESSION_TYPE_EXPENSE] = buildEmptySessionState(SESSION_TYPE_EXPENSE)
  }

  return {
    clearExpenseSessionForDeletedClaim,
    resetCurrentSessionState
  }
}
