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

export const usePiyangoDocumentValidationStore = defineStore(
  'piyangoDocumentValidationStore',
  () => {
    const piyangoDocumentStore = usePiyangoDocumentStore()
    const validationStore = useValidationStore()

    const formChanged = ref<boolean>(false)
    const isFormValid = ref<boolean>(true)
    const invalidTexts = reactive<Record<string, any>>({})

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

      validationStore.IsFieldEmpty(
        piyangoDocumentStore.piyangoDocumentForm,
        invalidTexts,
        'dokumanAdi',
        'Döküman için bir başlık belirtmelisiniz.'
      )

      validationStore.IsFieldEmpty(
        piyangoDocumentStore.piyangoDocumentForm,
        invalidTexts,
        'dosya',
        'Bir döküman eklemelisiniz.'
      )

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

    return {
      formChanged,
      isFormValid,
      invalidTexts,
      FormCheck
    }
  }
)
