// hooks/useNotification.js
import { toRefs, reactive } from 'vue'

const feedbacks = reactive({
  errors: [],
  warnings: [],
  successes: []
})

const errorHandler = (error) => {
  console.error(error)
  let errorMessage = ''

  if(Array.isArray(error)){
    for (let i = 0; i < error.length; i++) {
      if (error[i].message) {
        errorMessage += error[i].message
      } else {
        errorMessage += error[i]
      }
    }
  } else {
    if (error.message) {
      errorMessage = error.message
    } else {
      errorMessage = error
    }
  }

  feedbacks.errors.push(errorMessage)
}

const warnHandler = (warning) => {
  feedbacks.warnings.push(warning)
}

const successHandler = (message) => {
  feedbacks.successes.push(message)
}


export default function useFeedback() {
  return {
    ...toRefs(feedbacks),
    errorHandler,
    warnHandler,
    successHandler
  }
}
