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

export const useCustomerDocumentValidationStore = defineStore(
  'customerDocumentValidationStore',
  () => {
    const customerDocumentStore = useCustomerDocumentStore()
    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(
        customerDocumentStore.customerDocumentForm,
        invalidTexts,
        'title',
        'Döküman için bir başlık belirtmelisiniz.'
      )

      validationStore.IsFieldEmpty(
        customerDocumentStore.customerDocumentForm,
        invalidTexts,
        'file',
        'Bir döküman eklemelisiniz.'
      )

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

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