import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { useAuthStore } from './authStore'
import { useValidationStore } from '@/stores/validationStore'

export const useAuthValidationStore = defineStore('authValidationStore', () => {
  const authStore = useAuthStore()
  const validationStore = useValidationStore()

  const isFormValid = ref<boolean>(true)
  const isLoginMessage = ref<boolean>(false)
  const loginMessageType = ref<string>('success')
  const loginMessage = ref<string>('')
  const registerMessage = ref<string>('')
  const invalidTexts = reactive<Record<string, any>>({})

  const FormCheck = (): boolean => {
    Object.assign(invalidTexts, {})

    validationStore.IsFieldEmpty(
      authStore.loginData,
      invalidTexts,
      'username',
      'Kullanıcı adı alanını doldurmalısınız. Örn: isim@alanadi.td'
    )
    validationStore.IsFieldEmpty(
      authStore.loginData,
      invalidTexts,
      'password',
      'Lütfen şifrenizi giriniz.'
    )

    isFormValid.value = Object.keys(invalidTexts).length === 0
    return isFormValid.value
  }

  return {
    isFormValid,
    invalidTexts,
    isLoginMessage,
    loginMessageType,
    loginMessage,
    registerMessage,
    FormCheck
  }
})
