import { ref } from 'vue'
import type { KbxConflictSnapshot, KbxProblem, KbxSystemProblem } from '@kbx/contracts'

export function useKbxProblemHandler() {
  const conflict = ref<KbxConflictSnapshot | null>(null)
  const systemProblem = ref<KbxSystemProblem | null>(null)

  function handle(problem: KbxProblem) {
    if (problem.type === 'conflict') {
      conflict.value = {
        code: problem.code,
        title: problem.title,
        currentVersion: problem.currentVersion,
        correlationId: problem.correlationId,
      }
      return
    }
    if (problem.type === 'system') {
      systemProblem.value = problem
    }
  }

  function clear() {
    conflict.value = null
    systemProblem.value = null
  }

  return { conflict, systemProblem, handle, clear }
}
