AI_dutch_province_select_elected_overlord = {
	every_country = {
		limit = {
			has_country_flag = elected_as_dutch_province_overlord_by_@ROOT
		}
		clr_country_flag = elected_as_dutch_province_overlord_by_@ROOT
	}
	every_country = {
		limit = {
			OR = {
				has_country_flag = election_value_of_dutch_province_1_@ROOT
				has_country_flag = election_value_of_dutch_province_2_@ROOT
				has_country_flag = election_value_of_dutch_province_3_@ROOT
				has_country_flag = election_value_of_dutch_province_4_@ROOT
				has_country_flag = election_value_of_dutch_province_5_@ROOT
				has_country_flag = election_value_of_dutch_province_6_@ROOT
			}
		}
		clr_country_flag = election_value_of_dutch_province_1_@ROOT
		clr_country_flag = election_value_of_dutch_province_2_@ROOT
		clr_country_flag = election_value_of_dutch_province_3_@ROOT
		clr_country_flag = election_value_of_dutch_province_4_@ROOT
		clr_country_flag = election_value_of_dutch_province_5_@ROOT
		clr_country_flag = election_value_of_dutch_province_6_@ROOT
	}
	if = {
		limit = {
			has_subject_of_type = dutch_province_subject
		}
		set_country_flag = election_value_of_dutch_province_1_@ROOT
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
			}
			set_country_flag = election_value_of_dutch_province_2_@ROOT
		}
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
			}
			set_country_flag = election_value_of_dutch_province_3_@ROOT
		}
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
			}
			set_country_flag = election_value_of_dutch_province_4_@ROOT
		}
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
			}
			set_country_flag = election_value_of_dutch_province_5_@ROOT
		}
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
				NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
			}
			set_country_flag = election_value_of_dutch_province_6_@ROOT
		}
	}
	else = {
		overlord = {
			set_country_flag = election_value_of_dutch_province_1_@ROOT
			random_subject_country = {
				limit = {
					is_subject_of_type = dutch_province_subject
					NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
				}
				set_country_flag = election_value_of_dutch_province_2_@ROOT
			}
			random_subject_country = {
				limit = {
					is_subject_of_type = dutch_province_subject
					NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
				}
				set_country_flag = election_value_of_dutch_province_3_@ROOT
			}
			random_subject_country = {
				limit = {
					is_subject_of_type = dutch_province_subject
					NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
				}
				set_country_flag = election_value_of_dutch_province_4_@ROOT
			}
			random_subject_country = {
				limit = {
					is_subject_of_type = dutch_province_subject
					NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
				}
				set_country_flag = election_value_of_dutch_province_5_@ROOT
			}
			random_subject_country = {
				limit = {
					is_subject_of_type = dutch_province_subject
					NOT = { has_country_flag = election_value_of_dutch_province_1_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_2_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_3_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_4_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_5_@ROOT }
					NOT = { has_country_flag = election_value_of_dutch_province_6_@ROOT }
				}
				set_country_flag = election_value_of_dutch_province_6_@ROOT
			}
		}
	}

	set_dutch_province_value_for_target = { target = 1 }
	set_dutch_province_value_for_target = { target = 2 }
	set_dutch_province_value_for_target = { target = 3 }
	set_dutch_province_value_for_target = { target = 4 }
	set_dutch_province_value_for_target = { target = 5 }
	set_dutch_province_value_for_target = { target = 6 }

	select_dutch_province_country_to_vote_for = yes
}

set_dutch_province_value_for_target = {
	# Marriage: +10
	# Same Dynasty: +20
	# One Province Minor: -50
	# Between 10 and 20 Provinces: +25
	# More than 20 Provinces: +50
	# Same Culture: +10
	# Different Culture: -10
	# Same Religion: +15
	# Different Religion: -30
	# Different Religion Group: -100
	# Prestige: Amount / 2 (-50 to + 50)
	# Legitimcay: Amount - 50 (-50 to +50)
	# Diplomatic Reputation: Amount * 5
	# Corruption: Amount * -5
	# Allied to Rival: -25
	# Overextension: -5 (50%), -10 (100%), -15 (125%), -20 (150%)
	# Stability: Amount * 10
	# Neighbors: +5
	# Has any Disaster: -100
	# Opinion: Amount / 4
	# Power Projection: Amount / 4
	# Is subject and LD over 50: -1000
	# Is subject and LD under 50: +5
	# Is overlord themselves: +25

	set_variable = { which = election_value_of_dutch_province_$target$ value = 0 }
	every_country = {
		limit = {
			has_country_flag = election_value_of_dutch_province_$target$_@ROOT
		}
		ROOT = {
			# Marriage
			if = {
				limit = {
					marriage_with = PREV
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 10 }
			}
			# Same Dynasty
			if = {
				limit = {
					NOT = { tag = PREV }
					dynasty = PREV
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 20 }
			}
			# One Province Minor
			if = {
				limit = {
					PREV = {
						NOT = { num_of_cities = 2 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -50 }
			}
			# Between 10 and 20 Provinces
			if = {
				limit = {
					PREV = {
						num_of_cities = 10
						NOT = { num_of_cities = 20 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 25 }
			}
			# More than 20 Provinces
			if = {
				limit = {
					PREV = {
						num_of_cities = 20
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 50 }
			}
			# Same Culture
			if = {
				limit = {
					NOT = { tag = PREV }
					primary_culture = PREV
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 10 }
			}
			# Different Culture
			if = {
				limit = {
					NOT = { primary_culture = PREV }
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -10 }
			}
			# Same Religion
			if = {
				limit = {
					NOT = { tag = PREV }
					religion = PREV
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 15 }
			}
			# Different Religion
			if = {
				limit = {
					NOT = { tag = PREV }
					religion_group = PREV
					NOT = { religion = PREV }
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -30 }
			}
			# Different Religion Group
			if = {
				limit = {
					NOT = { religion_group = PREV }
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -100 }
			}
			# Prestige
			export_to_variable = {
				which = their_prestige
				value = trigger_value:prestige
				who = PREV
			}
			divide_variable = { which = their_prestige value = 2 }
			change_variable = { which = election_value_of_dutch_province_$target$ which = their_prestige }
			set_variable = { which = their_prestige value = 0 }
			# Legitimcay
			export_to_variable = {
				which = their_legitimacy
				value = trigger_value:legitimacy_equivalent
				who = PREV
			}
			subtract_variable = { which = their_legitimacy value = 51 }
			change_variable = { which = election_value_of_dutch_province_$target$ which = their_legitimacy }
			set_variable = { which = their_legitimacy value = 0 }
			# Diplomatic Reputation
			export_to_variable = {
				which = their_diplo_rep
				value = modifier:diplomatic_reputation
				who = PREV
			}
			multiply_variable = { which = their_diplo_rep value = 5 }
			change_variable = { which = election_value_of_dutch_province_$target$ which = their_diplo_rep }
			set_variable = { which = their_diplo_rep value = 0 }
			# Corruption
			export_to_variable = {
				which = their_corruption
				value = trigger_value:corruption
				who = PREV
			}
			if = {
				limit = {
					NOT = { check_variable = { which = their_corruption value = 0 } }
				}
				set_variable = { which = their_corruption value = 0 }
			}
			multiply_variable = { which = their_corruption value = -5 }
			change_variable = { which = election_value_of_dutch_province_$target$ which = their_corruption }
			set_variable = { which = their_corruption value = 0 }
			# Allied to Rival
			if = {
				limit = {
					PREV = {
						any_ally = {
							is_rival = ROOT
						}
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -25 }
			}
			# Overextension
			if = {
				limit = {
					PREV = {
						overextension_percentage = 0.5
						NOT = { overextension_percentage = 1 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -5 }
			}
			if = {
				limit = {
					PREV = {
						overextension_percentage = 1
						NOT = { overextension_percentage = 1.25 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -10 }
			}
			if = {
				limit = {
					PREV = {
						overextension_percentage = 1.25
						NOT = { overextension_percentage = 1.5 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -15 }
			}
			if = {
				limit = {
					PREV = {
						overextension_percentage = 1.5
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -20 }
			}
			# Stability
			if = {
				limit = {
					PREV = {
						NOT = { stability = -2 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -30 }
			}
			else_if = {
				limit = {
					PREV = {
						NOT = { stability = -1 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -20 }
			}
			else_if = {
				limit = {
					PREV = {
						NOT = { stability = 0 }
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -10 }
			}
			else_if = {
				limit = {
					PREV = {
						stability = 1
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 10 }
			}
			else_if = {
				limit = {
					PREV = {
						stability = 2
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 20 }
			}
			else_if = {
				limit = {
					PREV = {
						stability = 3
					}
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 30 }
			}
			# Neighbors
			if = {
				limit = {
					NOT = { tag = PREV }
					is_neighbor_of = PREV
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 5 }
			}
			# Has any Disaster
			if = {
				limit = {
					has_any_disaster = yes
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -100 }
			}
			# Opinion
			if = {
				limit = {
					NOT = { tag = PREV }
				}
				export_to_variable = {
					which = our_opinion
					value = opinion
					who = ROOT
					with = PREV
				}
				divide_variable = { which = our_opinion value = 4 }
				change_variable = { which = election_value_of_dutch_province_$target$ which = our_opinion }
				set_variable = { which = our_opinion value = 0 }
			}
			# Power Projection
			export_to_variable = {
				which = their_power_projection
				value = trigger_value:power_projection
				who = PREV
			}
			divide_variable = { which = their_power_projection value = 4 }
			change_variable = { which = election_value_of_dutch_province_$target$ which = their_power_projection }
			set_variable = { which = their_power_projection value = 0 }
			# Is subject and LD over 50
			if = {
				limit = {
					is_subject_of = PREV
					liberty_desire = 50
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = -1000 }
			}
			# Is subject and LD under 50
			if = {
				limit = {
					is_subject_of = PREV
					NOT = { liberty_desire = 50 }
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 5 }
			}
			# Is overlord themselves
			if = {
				limit = {
					tag = PREV
					has_subject_of_type = dutch_province_subject
				}
				change_variable = { which = election_value_of_dutch_province_$target$ value = 25 }
			}
		}
	}
	change_variable = { which = election_value_of_dutch_province_$target$ value = 0.5 }
	divide_variable = { which = election_value_of_dutch_province_$target$ value = 1000 }
	multiply_variable = { which = election_value_of_dutch_province_$target$ value = 1000 }
}

select_dutch_province_country_to_vote_for = {
	set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_1 }
	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_2 } }
		}
		set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_2 }
	}
	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_3 } }
		}
		set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_3 }
	}
	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_4 } }
		}
		set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_4 }
	}
	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_5 } }
		}
		set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_5 }
	}
	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_6 } }
		}
		set_variable = { which = dutch_province_election_variable which = election_value_of_dutch_province_6 }
	}

	set_variable = { which = dutch_province_dutch_province_election_variable_rival value = 0 }
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_1 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_1 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_1 }
	}
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_2 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_2 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_2 }
	}
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_3 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_3 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_3 }
	}
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_4 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_4 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_4 }
	}
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_5 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_5 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_5 }
	}
	if = {
		limit = {
			check_variable = { which = election_value_of_dutch_province_6 which = dutch_province_dutch_province_election_variable_rival }
			NOT = { is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_6 } }
		}
		set_variable = { which = dutch_province_dutch_province_election_variable_rival which = election_value_of_dutch_province_6 }
	}

	if = {
		limit = {
			NOT = { check_variable = { which = dutch_province_election_variable value = 0 } }
		}
		elect_this_as_dutch_province_overlord = yes
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_1 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_1_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_2 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_2_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_3 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_3_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_4 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_4_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_5 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_5_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else_if = {
		limit = {
			is_variable_equal = { which = dutch_province_election_variable which = election_value_of_dutch_province_6 }
		}
		every_country = {
			limit = {
				has_country_flag = election_value_of_dutch_province_6_@ROOT
			}
			elect_this_as_dutch_province_overlord = yes
		}
	}
	else = {
		elect_this_as_dutch_province_overlord = yes
	}
}

elect_this_as_dutch_province_overlord = {
	custom_tooltip = ELECT_THIS_AS_DUTCH_PROVINCE_OVERLORD_TT
	hidden_effect = {
		set_country_flag = elected_as_dutch_province_overlord_by_@ROOT
	}
}

find_new_dutch_province_overlord = {
	set_variable = { which = num_of_dutch_province_votes_for_this value = 0 }
	if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 6
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 6 }
	}
	else_if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 5
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 5 }
	}
	else_if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 4
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 4 }
	}
	else_if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 3
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 3 }
	}
	else_if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 2
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 2 }
	}
	else_if = {
		limit = {
			calc_true_if = {
				all_countries_including_self = {
					PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
				}
				amount = 1
			}
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 1 }
	}

	every_subject_country = {
		limit = {
			is_subject_of_type = dutch_province_subject
		}
		set_variable = { which = num_of_dutch_province_votes_for_this value = 0 }
		if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 6
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 6 }
		}
		else_if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 5
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 5 }
		}
		else_if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 4
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 4 }
		}
		else_if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 3
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 3 }
		}
		else_if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 2
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 2 }
		}
		else_if = {
			limit = {
				calc_true_if = {
					all_countries_including_self = {
						PREV = { has_country_flag = elected_as_dutch_province_overlord_by_@PREV }
					}
					amount = 1
				}
			}
			set_variable = { which = num_of_dutch_province_votes_for_this value = 1 }
		}
	}

	if = {
		limit = {
			num_of_subjects = 1
			all_subject_country = {
				OR = {
					NOT = { is_subject_of_type = dutch_province_subject }
					PREV = {
						check_variable = { which = num_of_dutch_province_votes_for_this which = PREV }
					}
				}
			}
		}
		set_country_flag = new_overlord_of_dutch_provinces
	}
	else_if = {
		limit = {
			any_subject_country = {
				is_subject_of_type = dutch_province_subject
				all_country = {
					OR = {
						NOT = { is_subject_of = ROOT }
						NOT = { is_subject_of_type = dutch_province_subject }
						PREV = {
							check_variable = { which = num_of_dutch_province_votes_for_this which = PREV }
						}
					}
				}
			}
		}
		random_subject_country = {
			limit = {
				is_subject_of_type = dutch_province_subject
				all_country = {
					OR = {
						NOT = { is_subject_of = ROOT }
						NOT = { is_subject_of_type = dutch_province_subject }
						PREV = {
							check_variable = { which = num_of_dutch_province_votes_for_this which = PREV }
						}
					}
				}
			}
			set_country_flag = new_overlord_of_dutch_provinces
		}
	}
	else = {
		set_country_flag = new_overlord_of_dutch_provinces
	}
}

make_new_overlord_of_dutch_provinces = {
	ROOT = {
		PREV = { grant_independence = yes }
		every_subject_country = {
			limit = {
				NOT = { tag = PREV }
				NOT = { tag = ROOT }
				is_subject_of_type = dutch_province_subject
			}
			set_country_flag = dutch_province_get_passed_to_new_overlord
			ROOT = {
				PREV = { grant_independence = yes }
			}
		}
	}
	every_country = {
		limit = {
			has_country_flag = dutch_province_get_passed_to_new_overlord
		}
		clr_country_flag = dutch_province_get_passed_to_new_overlord
		PREV = {
			create_subject = {
				subject = PREV
				subject_type = dutch_province_subject
			}
		}
	}
	create_subject = {
		subject = ROOT
		subject_type = dutch_province_subject
	}
	add_country_modifier = {
		name = cooldown_dutch_provinces_election
		duration = 50
		hidden = yes
	}
}