import includes from 'lodash/includes'
import every from 'lodash/every'

import { formIds } from 'config/form'

import type { FormState } from './hooks/useFormState'

const ignoredFields = [formIds.initialCountryId, formIds.country]

export const isFormStateEqual = (state1: FormState, state2: FormState) => (
  every(state1, (_, key) => {
    if (includes(ignoredFields, key)) return true

    const cityId1 = state1[formIds.cityId]?.value
    const cityId2 = state2[formIds.cityId]?.value
    if (key === formIds.city && cityId1 && cityId2) {
      return cityId1 === cityId2
    }

    return state1[key]?.value === state2[key]?.value
  })
)
