import { defineStore } from 'pinia'
import { reactive } from 'vue'

export const useValidationStore = defineStore('validationStore', () => {
  const panelInvalidText = reactive<Record<string, any>>({})
  const pageInvalidText = reactive<Record<string, any>>({})

  const onlyDel = (e: KeyboardEvent): boolean => {
    return e.key !== 'Backspace' && e.key !== 'Delete'
  }
  const onlyTab = (e: KeyboardEvent): boolean => {
    return e.key !== 'Tab'
  }
  const onlyArrows = (e: KeyboardEvent): boolean => {
    return e.key !== 'ArrowLeft' && e.key !== 'ArrowRight'
  }
  const onlyNumbers = (e: KeyboardEvent): boolean => {
    return isNaN(Number(e.key))
  }
  const onlySpace = (e: KeyboardEvent): boolean => {
    return e.code === 'Space' || e.key === ' '
  }
  const onlyCopyPaste = (e: KeyboardEvent): boolean => {
    return (
      (e.ctrlKey && e.key === 'c') ||
      (e.ctrlKey && e.key === 'C') ||
      (e.ctrlKey && e.key === 'v') ||
      (e.ctrlKey && e.key === 'V')
    )
  }
  const onlyLetters = (e: KeyboardEvent): boolean => {
    return /[a-zA-Z\p{L}]/u.test(e.key)
  }
  const allowNumbers = (e: KeyboardEvent) => {
    if ((onlyNumbers(e) && onlyTab(e)) || onlySpace(e)) e.preventDefault()
  }
  const checkOnlyNumber = (s: string): boolean => {
    return /^\d+$/.test(s)
  }
  const checkEmail = (s: string): boolean => {
    return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
      s
    )
  }
  const checkEmpty = (s: any): boolean => {
    return s === '' || s === undefined || s === null
  }
  const checkMinLength = (s: string, min: number): boolean => {
    return s.length < min
  }
  const allowNumbersWithKeys = (e: KeyboardEvent) => {
    if (
      (onlyNumbers(e) &&
        !onlyCopyPaste(e) &&
        onlyDel(e) &&
        onlyArrows(e) &&
        onlyTab(e)) ||
      onlySpace(e)
    )
      e.preventDefault()
  }
  const allowPrice = (e: KeyboardEvent) => {
    if (
      (onlyNumbers(e) &&
        !onlyCopyPaste(e) &&
        onlyDel(e) &&
        onlyArrows(e) &&
        onlyTab(e) &&
        !/,/.test(e.key)) ||
      onlySpace(e)
    )
      e.preventDefault()
  }
  const allowNumbersWithKeysExtra = (e: KeyboardEvent, reg: RegExp) => {
    if (
      onlyNumbers(e) &&
      !onlyCopyPaste(e) &&
      onlyDel(e) &&
      onlyArrows(e) &&
      onlyTab(e) &&
      !reg.test(e.key)
    )
      e.preventDefault()
  }
  const allowLettersWithKeys = (e: KeyboardEvent) => {
    if (
      (!onlyLetters(e) &&
        !onlyCopyPaste(e) &&
        onlyDel(e) &&
        onlyArrows(e) &&
        onlyTab(e)) ||
      onlySpace(e)
    )
      e.preventDefault()
  }
  const allowLettersWithKeysExtra = (e: KeyboardEvent, reg: RegExp) => {
    if (
      !onlyLetters(e) &&
      !onlyCopyPaste(e) &&
      onlyDel(e) &&
      onlyArrows(e) &&
      onlyTab(e) &&
      !reg.test(e.key)
    )
      e.preventDefault()
  }
  const IsFieldEmpty = (
    formData: Record<string, any>,
    validData: Record<string, any>,
    field: string,
    text: string
  ) => {
    if (checkEmpty(formData[field])) {
      validData[field] = text
    } else {
      delete validData[field]
    }
  }

  return {
    panelInvalidText,
    pageInvalidText,
    onlyDel,
    onlyTab,
    onlyArrows,
    onlyNumbers,
    onlySpace,
    onlyCopyPaste,
    onlyLetters,
    allowNumbers,
    checkOnlyNumber,
    checkEmail,
    checkEmpty,
    checkMinLength,
    allowNumbersWithKeys,
    allowPrice,
    allowNumbersWithKeysExtra,
    allowLettersWithKeys,
    allowLettersWithKeysExtra,
    IsFieldEmpty
  }
})
