module WolfCore::Barton::Parsing

def split_address(address_string)

def split_address(address_string)
  address_string = address_string&.dup
  address_string ||= ''
  address = { street: nil, city: nil, state: nil, zip: nil }
  city_and_state_regex_found = false
  city_and_state_regex = /\b([A-Za-z\s]+),\s*([A-Za-z]{2})\b/
  zip_regex = /\d{5}(?:-\d{4})?$/
  street_regex = /\A([^,]+)/
  state_regex = /\b[A-Za-z]{2}\b/
  city_regex = /\b[a-zA-Z0-9\s]{2,}\b/
  if match_data = address_string.match(city_and_state_regex)
    address[:city] = match_data[1].strip
    address[:state] = match_data[2].strip
    address_string.sub!(city_and_state_regex, '')
    city_and_state_regex_found = true
  end
  if zip_match = address_string.match(zip_regex)
    address[:zip] = zip_match[0].strip
    address_string.sub!(zip_regex, '')
  end
  
  if street_match = address_string.match(street_regex)
    address[:street] = street_match[0].strip
    address[:street] = nil if address[:street].empty?
    address_string.sub!(street_regex, '')
  end
  return address if city_and_state_regex_found
  if state_match = address_string.match(state_regex)
    address[:state] = state_match[0].strip
    address_string.sub!(state_regex, '')
  end
  if city_match = address_string.match(city_regex)
    address[:city] = city_match[0].strip
    address_string.sub!(city_regex, '')
  end
  return address
end