#########################################
### Effect of clearing broken votings ###
#########################################
update_eu_dirty_variable = {
	if = { limit = { check_variable = { global.EU_dirty_update_var < 10000 } }
		add_to_variable = { global.EU_dirty_update_var = 1 }
	}
	else = {
		set_variable = { global.EU_dirty_update_var = 1 }
	}
}
eu_screen_selection = {
	clr_country_flag = show_eu_parliament # European Parliament
	clr_country_flag = show_euro_voting # European Council
	clr_country_flag = show_eu_fiscal # European budget
	clr_country_flag = show_offices_screen # Offices
	clr_country_flag = show_euro_info # Euro-Info
	clr_country_flag = eu_party_detail_open # close the party MEP breakdown panel on any tab switch
	clr_country_flag = eu_fiscal_distrib_view # reset the fiscal side panel to the contributions view on any tab switch
	update_eu_dirty_variable = yes
}

##############################################
##############################################
### EU scripted effects to use for modding ###
##############################################
##############################################

EU_update_vars = {
	hidden_effect = {
		# EU population vars update
		# Sets Global EU Population and Country Population
		# population_total, not max_manpower_k: the engine value wraps negative above ~922M people (#4879)
		set_variable = { var = global.var_EUpopTotal value = 0 }
		for_each_scope_loop = {
			array = global.EU_member
			add_to_variable = { global.var_EUpopTotal = THIS.population_total }
		}

		set_variable = {
			global.var_EUpopTotalDisplay = {
				value = global.var_EUpopTotal
				multiply = 0.1
			}
		}

		set_variable = { var = global.var_europeanism value = 0 }
		for_each_scope_loop = {
			array = global.EU_member
			# Population share of the Union (0-1), summing to 1 across all members
			set_variable = {
				EU_pop_ratio = {
					value = THIS.population_total
					if = { limit = { value = global.var_EUpopTotal greater_than = 0 }
						divide = global.var_EUpopTotal
					}
				}
			}
			# Population-weighted europeanism: EU_pop_ratio already normalizes, so no final divide
			EU_init_default_europeanism = yes
			clamp_variable = { var = THIS.europeanism min = 0 max = 1 }
			add_to_variable = {
				global.var_europeanism = {
					value = THIS.europeanism
					multiply = THIS.EU_pop_ratio
				}
			}
		}

		# Failsafe: lift a stuck budget/MFF proposal block once its whole draft cycle is idle
		if = {
			limit = {
				has_global_flag = EU_budget_EP_block
				NOT = { has_active_mission = EU_budget_draft_preparations }
				NOT = { has_active_mission = EU_budget_draft_ongoing }
				NOT = { check_variable = { global.current_active_agenda_disp = 999 } }
			}
			clr_global_flag = EU_budget_EP_block
		}
		if = {
			limit = {
				has_global_flag = EU_mff_EP_block
				NOT = { has_active_mission = EU_mff_draft_preparations }
				NOT = { has_active_mission = EU_mff_draft_ongoing }
				NOT = { check_variable = { global.current_active_agenda_disp = 998 } }
			}
			clr_global_flag = EU_mff_EP_block
		}

		# QMV majority threshold
		set_variable = {
			global.EU_majority_qmv_state = {
				value = global.EU_member^num
				multiply = 0.55
				round = yes
			}
		}

		# Pre-Accession Assistance passive Europeanism drift
		for_each_scope_loop = {
			array = global.EU_potential
			if = {
				limit = {
					check_variable = { modifier@europeanism_drift > 0 }
				}
				add_to_variable = { THIS.europeanism = modifier@europeanism_drift }
				clamp_variable = { var = THIS.europeanism min = 0 max = 1 }
			}
		}
	}
	update_eu_dirty_variable = yes
}

######################################
### Changing Europeanism of a tag ###
######################################
# set_temp_variable = { modify_europeanism = +- }   (required; 0 = no-op, skipped)
# set_temp_variable = { modify_europeanism_target = ROOT/THIS/PREV/FROM or country TAG }   (optional, defaults to ROOT)
europeanism_change = {
	if = { limit = { check_variable = { modify_europeanism_target = 0 } }
		set_temp_variable = { modify_europeanism_target = ROOT.id }
	}
	if = { limit = { check_variable = { modify_europeanism = 0 } }
		log = "[GetDateText]: [ROOT.GetTag]: WARNING: europeanism_change called with modify_europeanism = 0 (no-op)"
	}
	else = {
		if = {
			limit = { NOT = { has_global_flag = GAME_RULE_eu_disabled } }
			custom_effect_tooltip = tooltip_europeanism_change
			hidden_effect = {
				log = "[GetDateText]: [FROM.GetTag]: 99_EU_scripted_effects [?modify_europeanism_target] europeanism_change [?modify_europeanism]"
				var:modify_europeanism_target = { add_to_variable = { THIS.europeanism = modify_europeanism } }
				EU_update_vars = yes
			}
		}
	}
}

# Apply the same europeanism delta to both THIS and ROOT. Set modify_europeanism first.
europeanism_change_this_and_root = {
	set_temp_variable = { modify_europeanism_target = THIS }
	europeanism_change = yes
	set_temp_variable = { modify_europeanism_target = ROOT }
	europeanism_change = yes
}

############################################################
### Changing Europeanism of all (current) member states ###
############################################################

# set_temp_variable = { modify_europeanism = +- }   (required; 0 = no-op, skipped)
EU_europeanism_change = {
	if = { limit = { check_variable = { modify_europeanism = 0 } }
		log = "[GetDateText]: [ROOT.GetTag]: WARNING: EU_europeanism_change called with modify_europeanism = 0 (no-op)"
	}
	else = {
		if = {
			limit = { NOT = { has_global_flag = GAME_RULE_eu_disabled } }
			custom_effect_tooltip = tooltip_EU_europeanism_change
			hidden_effect = {
				log = "[GetDateText]: [FROM.GetTag]: 99_EU_scripted_effects EU_europeanism_change [?modify_europeanism]"
				for_each_scope_loop = {
					array = global.EU_member
					add_to_variable = { THIS.europeanism = modify_europeanism }
				}
				EU_update_vars = yes
			}
		}
	}
}

##########################################################################
### Changing Europeanism of all current AND potential member states ###
########################################################################

# set_temp_variable = { modify_europeanism = +- }   (required; 0 = no-op, skipped)
EU_potential_europeanism_change = {
	if = { limit = { check_variable = { modify_europeanism = 0 } }
		log = "[GetDateText]: [ROOT.GetTag]: WARNING: EU_potential_europeanism_change called with modify_europeanism = 0 (no-op)"
	}
	else = {
		if = {
			limit = { NOT = { has_global_flag = GAME_RULE_eu_disabled } }
			custom_effect_tooltip = tooltip_EU_potential_europeanism_change
			hidden_effect = {
				log = "[GetDateText]: [FROM.GetTag]: 99_EU_scripted_effects EU_potential_europeanism_change [?modify_europeanism]"
				for_each_scope_loop = {
					array = global.EU_potential
					add_to_variable = { THIS.europeanism = modify_europeanism }
				}
				EU_update_vars = yes
			}
		}
	}
}

### Raise THIS country's Europeanism to a floor if below it (never lowers)
### use in country scope (THIS = candidate tag)
set_eu_candidate_europeanism_floor = {
	if = {
		limit = { check_variable = { THIS.europeanism < 0.50 } }
		set_variable = { THIS.europeanism = 0.50 }
		update_eu_dirty_variable = yes
	}
}

EU_init_default_europeanism = {
	if = {
		limit = { NOT = { has_variable = europeanism } }
		set_variable = { europeanism = 0.3 }
	}
}

### Inventment in the single market
### use in country scope
### TAG is investee
invest_in_EU_single_market_infra = {
	if = {
		limit = {
			any_owned_state = {
				infrastructure < 5
			}
		}
		custom_effect_tooltip = tooltip_invest_in_EU_single_market_infra
		hidden_effect = {
			random_owned_state = {
				limit = { infrastructure < 5 }
				invest_infra = yes
			}
		}
		set_temp_variable = { modify_europeanism = 0.03 }
		europeanism_change_this_and_root = yes
		update_eu_dirty_variable = yes
	}
}

################################
### ECB OMT scripted effects ###
################################

execute_OMT = {
	custom_effect_tooltip = tooltip_ECB_Outright_Monetary_Transactions_complete_effect
	hidden_effect = {
		for_each_scope_loop = {
			array = global.EU_member
			# Pick a debt step (10^n) sized to the country's debt so the OMT loop converges in a reasonable number of iterations without overshooting.
			set_temp_variable = { step = 0 }
			if = { limit = { has_idea = euro check_variable = { debt > 9999.999 } } set_temp_variable = { step = 1000.0 } }
			else_if = { limit = { has_idea = euro check_variable = { debt > 999.999 } } set_temp_variable = { step = 100.0 } }
			else_if = { limit = { has_idea = euro check_variable = { debt > 99.999 } } set_temp_variable = { step = 10.0 } }
			else_if = { limit = { has_idea = euro check_variable = { debt > 9.999 } } set_temp_variable = { step = 1.0 } }
			else_if = { limit = { has_idea = euro check_variable = { debt > 0.999 } } set_temp_variable = { step = 0.1 } }
			if = { limit = { check_variable = { step > 0 } }
				set_variable = { OMT_account = 0 }
				while_loop_effect = {
					limit = {
						check_variable = { interest_rate > 4.0 }
						check_variable = { global.OMT_size_limit > global.OMT_total_value }
						check_variable = { debt > 0 }
					}
					subtract_from_variable = { debt = step }
					clamp_variable = { var = debt min = 0 }
					add_to_variable = { OMT_account = step }
					add_to_variable = { global.OMT_total_value = step }
					calculate_interest_rate = yes
				}
			}
		}
	}
	update_eu_dirty_variable = yes
}

remove_OMT = {
	custom_effect_tooltip = tooltip_ECB_Outright_Monetary_Transactions_remove_effect
	hidden_effect = {
		for_each_scope_loop = {
			array = global.EU_member
			if = {
				limit = { has_idea = euro }
				add_to_variable = { debt = OMT_account }
				clear_variable = OMT_account
				news_event = EU_news.33
			}
		}
		clear_variable = global.OMT_total_value
	}
	update_eu_dirty_variable = yes
}

#####################################
### Leaving EU scripted effects  ####
#####################################

EU_vacate_offices = {
	# Strip every EU leadership office the current scope holds so another member can claim it.
	if = { limit = { has_idea = EU_commission_president }
		remove_ideas = EU_commission_president
		clr_global_flag = EU_commission_taken_flag
	}
	if = { limit = { has_idea = EU_council_president }
		remove_ideas = EU_council_president
		# clr_global_flag = EU_council_taken_flag
	}
	if = { limit = { has_idea = EU_foreign_minister }
		remove_ideas = EU_foreign_minister
		clr_global_flag = EU_foreign_taken_flag
	}
	if = { limit = { has_idea = EU_parliament_president }
		remove_ideas = EU_parliament_president
		clr_global_flag = EU_parliament_taken_flag
	}
	if = { limit = { has_idea = ECB_president }
		remove_ideas = ECB_president
		# clr_global_flag = EU_bank_taken_flag
	}
	if = { limit = { has_idea = EU_finance_minister }
		remove_ideas = EU_finance_minister
		# clr_global_flag = EU_finance_taken_flag
	}
	if = { limit = { has_idea = frontex_ExDir }
		remove_ideas = frontex_ExDir
		# clr_global_flag = EU_frontex_taken_flag
	}
	if = { limit = { has_idea = EUU_euronavy_sc }
		remove_ideas = EUU_euronavy_sc
		# clr_global_flag = EU_euronavy_taken_flag
	}
	if = { limit = { has_idea = EUU_euroarmy_sc }
		remove_ideas = EUU_euroarmy_sc
		# clr_global_flag = EU_euroarmy_taken_flag
	}
	if = { limit = { has_idea = EUU_ambassador_unsc }
		remove_ideas = EUU_ambassador_unsc
		# clr_global_flag = EU_ambassador_taken_flag
	}
	if = { limit = { has_idea = EU_president }
		remove_ideas = EU_president
		clr_global_flag = EU_POTEF_taken_flag
	}
}


leaving_EU = {
	if = {
		limit = { NOT = { has_country_flag = EUU_do_not_remove_eu_idea } }
		remove_ideas = EU_member
	}
	# Mark as a former member and start the 3-year rejoin cooldown (#687).
	# Clearing EU_candidate forces rejoin through the cooldown-gated path, not the first-time candidacy path.
	clr_country_flag = EU_candidate
	set_country_flag = EU_former_member
	set_country_flag = { flag = EU_rejoin_cooldown days = 1095 value = 1 }
	remove_ideas = euro
	custom_effect_tooltip = tooltip_EU_remove_laws_and_offices
	hidden_effect = {
		for_each_scope_loop = {
			array = global.EU_member
			PREV = { remove_opinion_modifier = { target = PREV modifier = EU_member_opinion } }
			remove_opinion_modifier = { target = PREV modifier = EU_member_opinion }
		}
		### EU law and offices
		# European Union Laws
		remove_ideas = EUU_cfr_eu
		remove_ideas = ECB_zero_interest_rate_policy
		remove_ideas = euro_bonds
		remove_ideas = european_debt_crisis
		remove_ideas = EUU_national_dept_crisis_eu
		remove_ideas = euro_rescue_package
		remove_ideas = fiscal_union
		remove_ideas = European_Economic_Government
		remove_from_tech_sharing_group = EU_Tech_Share
		remove_ideas = European_Defence_Fund
		remove_ideas = European_Security_Council
		remove_ideas = EUU_idea_gdpr
		remove_ideas = EU_copyright
		remove_ideas = Migrant_Crisis_Europe
		remove_ideas = relocation_asylum_seekers
		remove_ideas = EU_Turkey_deal
		remove_ideas = European_Asylum_Agency
		remove_ideas = Frontex
		clr_country_flag = adopt_eu_law
		# EU offices — vacate every leadership seat so another member can claim it
		EU_vacate_offices = yes
		# Relinquish Frontex / EuroArmy control so a remaining member can reclaim the centralized force.
		if = { limit = { has_country_flag = control_frontex_force }
			clr_country_flag = control_frontex_force
			set_variable = { global.EU_frontex_controller = 0 }
		}
		if = { limit = { has_country_flag = control_euroarmy_force }
			clr_country_flag = control_euroarmy_force
			set_variable = { global.EU_euroarmy_controller = 0 }
		}
		# Drop in-flight EU vote/budget/MFF missions so an ex-member's timeout no longer drives EU-wide state.
		remove_mission = EU_budget_yearly_timer
		remove_mission = EU_budget_draft_preparations
		remove_mission = EU_budget_draft_ongoing
		remove_mission = EU_mff_yearly_timer
		remove_mission = EU_mff_draft_preparations
		remove_mission = EU_mff_draft_ongoing
		remove_mission = EU_parliament_focus_EUXXX_approve
		remove_mission = EU_voting_mission_focus_EUXXX_QMV_result
		### End of EU law and offices
		### leave EDU
		if = { limit = { has_global_flag = EDU }
			leave_faction = yes
		}
	}
	update_eu_dirty_variable = yes
}

# Runs in the civil-war victor's scope from on_civil_war_end.
EU_resync_member_array = {
	for_each_scope_loop = {
		array = global.EU_member
		if = {
			limit = { NOT = { has_idea = EU_member } }
			remove_from_array = { global.EU_member = THIS }
		}
	}
	# A civil-war victor inherits EU_member (allowed_civil_war) but is never run through on_add, so add it here.
	if = {
		limit = {
			has_idea = EU_member
			NOT = { is_in_array = { global.EU_member = THIS } }
		}
		add_to_array = { global.EU_member = THIS }
		EU_init_default_europeanism = yes
	}
}

##########################
### breach of EU value ###
##########################
#TODO: Set flags to allow people to see what their cause of the breach is
check_breach_of_EU_value = {
	set_variable = { THIS.breach_of_EU_value = 0 }
	### Corruption Level Addition ###
	if = { limit = { has_idea = corruption_level_10 }
		add_to_variable = { THIS.breach_of_EU_value = 5 }
	}
	else_if = { limit = { has_idea = corruption_level_09 }
		add_to_variable = { THIS.breach_of_EU_value = 4 }
	}
	else_if = { limit = { has_idea = corruption_level_08 }
		add_to_variable = { THIS.breach_of_EU_value = 3 }
	}
	else_if = { limit = { has_idea = corruption_level_07 }
		add_to_variable = { THIS.breach_of_EU_value = 2 }
	}
	else_if = { limit = { has_idea = corruption_level_06 }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	### peace ###
	if = {
		limit = {
			has_offensive_war = yes
			NOT = { has_civil_war = yes }
			any_enemy_country = {
				has_idea = EU_member
				NOT = { original_tag = ROOT.original_tag }
			}
		}
		add_to_variable = { THIS.breach_of_EU_value = 10 }
	}
	### government ###
	if = { limit = { has_communist_in_coalition = yes }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	if = { limit = { has_reactionary_in_coalition = yes }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	if = { limit = { has_communist_government = yes }
		add_to_variable = { THIS.breach_of_EU_value = 2 }
	}
	if = { limit = { has_reactionary_government = yes }
		add_to_variable = { THIS.breach_of_EU_value = 2 }
	}
	### women ###
	if = { limit = { has_idea = no_women_in_military }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	### intervention ###
	if = { limit = { has_idea = intervention_neo_imperialism }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	### minorities ###
	if = { limit = { check_variable = { election_threshold > 0.095 } }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	### no bailout ###
	if = { limit = { has_active_mission = debt_default_main_mission }
		add_to_variable = { THIS.breach_of_EU_value = 1 }
	}
	### elections ###
	if = { limit = { has_elections = no }
		add_to_variable = { THIS.breach_of_EU_value = 5 }
	}
	## Special Circumstance for HLS
	if = {
		limit = {
			original_tag = HLS
			has_elections = no
			western_autocrats_are_in_power = yes
		}
		add_to_variable = { THIS.breach_of_EU_value = -5 }
	}
	### democracy ###
	if = { limit = { has_idea = dictatorship }
		add_to_variable = { THIS.breach_of_EU_value = 5 }
	}
	if = { limit = { has_idea = communist_dictatorship }
		add_to_variable = { THIS.breach_of_EU_value = 5 }
	}
	if = { limit = { has_idea = fascist_dictatorship }
		add_to_variable = { THIS.breach_of_EU_value = 5 }
	}
	if = { limit = { is_subject = yes }
		add_to_variable = { THIS.breach_of_EU_value = 4 }
	}
}

#############################
### USoE scripted effects ###
#############################

apply_USoE_technologies = {
	# Precondition: EUU must already hold every member's pooled technology (inherit into EUU before calling).
	resize_array = { ROOT.eu_technologies = 0 }
	EUU = {
		for_each_loop = {
			array = researched_techs
			if = {
				limit = {
					NOT = { is_in_array = { array = ROOT.eu_technologies value = v } }
				}
				add_to_array = { ROOT.eu_technologies = v }
			}
		}
	}
	for_each_loop = {
		array = ROOT.eu_technologies
		value = v
		index = i
		meta_effect = {
			text = {
				ROOT = {
					set_technology = {
						[techName] = 1
						popup = no
					}
				}
			}
			techName = "[?ROOT.eu_technologies^i.GetTokenKey]"
		}
	}
	clear_array = ROOT.eu_technologies
}

update_EU_military_leaders = {
	custom_effect_tooltip = tooltip_update_EU_military_leaders
	every_unit_leader = {
		limit = {
			has_unit_leader_flag = USoE_unit_leader
		}
		remove_unit_leader_trait = military_reorganization
	}
	update_eu_dirty_variable = yes
}

update_EU_country_leaders = {
	custom_effect_tooltip = tooltip_update_EU_country_leaders
	set_country_flag = USoE_country_leader
	set_country_flag = USoE_political_parties
}

set_USoE_flag_of_the_house = {
	if = { limit = { has_completed_focus = USoE622 }
		set_cosmetic_tag = USoE_SAV
	}
	else_if = { limit = { has_completed_focus = USoE623 }
		set_cosmetic_tag = USoE_WIT
	}
	else_if = { limit = { has_completed_focus = USoE624 }
		set_cosmetic_tag = USoE_BOR_ANJ
	}
	else_if = { limit = { has_completed_focus = USoE625 }
		set_cosmetic_tag = USoE_BOR
	}
	else_if = { limit = { has_completed_focus = USoE626 }
		set_cosmetic_tag = USoE_HAN
	}
	else_if = { limit = { has_completed_focus = USoE627 }
		set_cosmetic_tag = USoE_GLU
	}
	else_if = { limit = { has_completed_focus = USoE628 }
		set_cosmetic_tag = USoE_WIN
	}
	else_if = { limit = { has_completed_focus = USoE629 }
		set_cosmetic_tag = USoE_ORA
	}
	else_if = { limit = { has_completed_focus = USoE630 }
		set_cosmetic_tag = USoE_HAB
	}
	else_if = { limit = { has_completed_focus = USoE631 }
		set_cosmetic_tag = USoE_NAP
	}
	else_if = { limit = { has_completed_focus = USoE632 }
		set_cosmetic_tag = USoE_HOH
	}
}

# EU Startup Configuration
on_startup_euu_action = {
	# Disable The EU
	if = {
		limit = {
			has_game_rule = {
				rule = rule_disable_eu
				option = yes
			}
		}
		for_each_scope_loop = {
			array = global.EU_member
			remove_ideas = {
				euro
				EU_member
				foreign_data_kraken
				asylum_shopping
			}

			add_ideas = no_foreign_reserve
		}
		set_global_flag = GAME_RULE_eu_disabled
	}

	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = one
			}
		}
		set_global_flag = GAME_RULE_eu_one_cooldown
	}
	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = sixty
			}
		}
		set_global_flag = GAME_RULE_eu_sixty_cooldown
	}
	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = ninety
			}
		}
		set_global_flag = GAME_RULE_eu_ninety_cooldown
	}
	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = two_hundred_ten
			}
		}
		set_global_flag = GAME_RULE_eu_two_hundred_ten_cooldown
	}
	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = one_hundred_eighty
			}
		}
		set_global_flag = GAME_RULE_eu_one_hundred_eighty_cooldown
	}
	if = {
		limit = {
			has_game_rule = {
				rule = rule_european_union_voting_limits
				option = one_hundred_twenty
			}
		}
		set_global_flag = GAME_RULE_eu_one_hundred_twenty_cooldown
	}

	# Configure European Union startup values and information
	if = {
		limit = {
			NOT = { has_global_flag = GAME_RULE_eu_disabled }
			NOT = { has_global_flag = EU_initialized }
		}
		set_global_flag = EU_initialized
		EUU = {
			add_to_array = { global.EU_potential_votes = 101 } # Charter of Fundamental Rights of the EU
			add_to_array = { global.EU_potential_votes = 102 } # European Convention
			add_to_array = { global.EU_potential_votes = 103 } # European Constitution
			add_to_array = { global.EU_potential_votes = 104 } # Treaty of Lisbon
			add_to_array = { global.EU_potential_votes = 105 } # European External Action Service
			add_to_array = { global.EU_potential_votes = 201 } # ECB Intervention
			add_to_array = { global.EU_potential_votes = 202 } # European Banking Union
			add_to_array = { global.EU_potential_votes = 203 } # Eurobonds
			add_to_array = { global.EU_potential_votes = 204 } # European Financial Stability Facility (EFSF)
			add_to_array = { global.EU_potential_votes = 205 } # European Stability Mechanism (ESM)
			add_to_array = { global.EU_potential_votes = 206 } # Fiscal Union
			add_to_array = { global.EU_potential_votes = 207 } # European Economic Government
			add_to_array = { global.EU_potential_votes = 208 } # European Minister of Economy and Finance
			add_to_array = { global.EU_potential_votes = 301 } # European Defence Agency (EDA)
			add_to_array = { global.EU_potential_votes = 302 } # Permanent Structured Cooperation (PESCO)
			add_to_array = { global.EU_potential_votes = 303 } # European Defence Fund
			add_to_array = { global.EU_potential_votes = 304 } # European Security Council
			add_to_array = { global.EU_potential_votes = 305 } # EuroArmy
			add_to_array = { global.EU_potential_votes = 306 } # EuroNavy
			add_to_array = { global.EU_potential_votes = 307 } # EU Permanent Member of the UNSC
			add_to_array = { global.EU_potential_votes = 308 } # EU Nuclear Power
			add_to_array = { global.EU_potential_votes = 401 } # General Data Protection Regulation
			add_to_array = { global.EU_potential_votes = 402 } # Directive on Copyright in the Digital Single Market
			add_to_array = { global.EU_potential_votes = 403 } # Dublin II Regulations
			add_to_array = { global.EU_potential_votes = 404 } # Relocation and Resettlement of Asylum Seekers
			add_to_array = { global.EU_potential_votes = 405 } # EU-Turkey Deal
			add_to_array = { global.EU_potential_votes = 406 } # European Asylum Agency
			add_to_array = { global.EU_potential_votes = 407 } # Frontex
			add_to_array = { global.EU_potential_votes = 408 } # European Border & Coast Guard Agency
			add_to_array = { global.EU_potential_votes = 601 } # 2004 EU Enlargement
			add_to_array = { global.EU_potential_votes = 602 } # 2007 EU Enlargement
			add_to_array = { global.EU_potential_votes = 603 } # 2013 EU Enlargement
			add_to_array = { global.EU_potential_votes = 605 } # Western Balkans Enlargement
			add_to_array = { global.EU_potential_votes = 606 } # Turkey
			add_to_array = { global.EU_potential_votes = 607 } # Black Sea Enlargement
			add_to_array = { global.EU_potential_votes = 608 } # Caucasus Enlargement
			add_to_array = { global.EU_potential_votes = 609 } # European Economic Area
			add_to_array = { global.EU_potential_votes = 610 } # Belarus
			add_to_array = { global.EU_potential_votes = 611 } # Internal Enlargement
			add_to_array = { global.EU_potential_votes = 650 } # Extended Enlargement Framework
			add_to_array = { global.EU_potential_votes = 651 } # Lisbon Treaty Amendment
			add_to_array = { global.EU_potential_votes = 652 } # EU-Russia Partnership Treaty
			add_to_array = { global.EU_potential_votes = 653 } # Russia
			add_to_array = { global.EU_potential_votes = 654 } # Barents Sea Enlargement
			add_to_array = { global.EU_potential_votes = 655 } # Volga Enlargement
			add_to_array = { global.EU_potential_votes = 656 } # Don-Kuban Enlargement
			add_to_array = { global.EU_potential_votes = 657 } # Northern Caucasus Enlargement
			add_to_array = { global.EU_potential_votes = 658 } # Ruthenia Enlargement
			add_to_array = { global.EU_potential_votes = 659 } # Donets Enlargement
			add_to_array = { global.EU_potential_votes = 660 } # Dnieper Enlargement
			add_to_array = { global.EU_potential_votes = 110 } # Federalisation of the European Union
			add_to_array = { global.EU_potential_votes = 111 } # United States of Europe
			add_to_array = { global.EU_potential_votes = 112 } # European Federation

			#base voting array for pp calculations
			add_to_array = { array = global.starting_mep_support_parliament value = -100 }
			add_to_array = { array = global.starting_mep_support_parliament value = -50 }
			add_to_array = { array = global.starting_mep_support_parliament value = 0 }
			add_to_array = { array = global.starting_mep_support_parliament value = 50 }
			add_to_array = { array = global.starting_mep_support_parliament value = 100 }

			add_to_array = { global.EU_member = AUS }
			add_to_array = { global.EU_member = BEL }
			add_to_array = { global.EU_member = DEN }
			add_to_array = { global.EU_member = ENG }
			add_to_array = { global.EU_member = FIN }
			add_to_array = { global.EU_member = FRA }
			add_to_array = { global.EU_member = GER }
			add_to_array = { global.EU_member = GRE }
			add_to_array = { global.EU_member = HOL }
			add_to_array = { global.EU_member = IRE }
			add_to_array = { global.EU_member = ITA }
			add_to_array = { global.EU_member = LUX }
			add_to_array = { global.EU_member = POR }
			add_to_array = { global.EU_member = SPR }
			add_to_array = { global.EU_member = SWE }

			# Baseline EU penalties applied to every founding member at startup
			for_each_scope_loop = {
				array = global.EU_member
				add_ideas = {
					foreign_data_kraken
					asylum_shopping
				}
			}

			every_collection_element = {
				input = { input = collection:eu_members_collection }
				add_to_array = { global.EU_potential = THIS }
			}
			every_collection_element = {
				input = { input = collection:eu_potential_countries_collection }
				add_to_array = { global.EU_potential = THIS }
			}

			random_scope_in_array = {
				array = global.EU_potential
				limit = { is_ai = yes }
				add_ideas = EUU_monitor
				country_event = EUelection.2
				# First EP election lands on the historical June 2004 date (2000.1.1 + 1625 days);
				# EUelection.1 then arms EU_election_5_years, which carries the 5-year cycle (2009, 2014, ...).
				country_event = { id = EUelection.1 days = 1625 }
			}

			set_global_flag = { flag = EU_president_commission_ai value = 1 days = 10 }
			set_global_flag = { flag = ECB_president_ai value = 1 days = 10 }

			# Starting europeanism for members and candidates. Set before EU_update_vars so the
			# population-weighted global.var_europeanism is correct on day 1.
			# Default for non-member candidates in EU_potential
			for_each_scope_loop = {
				array = global.EU_potential
				set_variable = { europeanism = 0.3 }
			}
			# Founding members and other notable cases
			set_variable = { AUS.europeanism = 0.75 }
			set_variable = { BEL.europeanism = 0.9 }
			set_variable = { DEN.europeanism = 0.76 }
			set_variable = { ENG.europeanism = 0.76 }
			set_variable = { FIN.europeanism = 0.78 }
			set_variable = { FRA.europeanism = 0.86 }
			set_variable = { GER.europeanism = 0.85 }
			set_variable = { GRE.europeanism = 0.92 }
			set_variable = { HOL.europeanism = 0.94 }
			set_variable = { IRE.europeanism = 0.94 }
			set_variable = { ITA.europeanism = 0.91 }
			set_variable = { LUX.europeanism = 0.94 }
			set_variable = { POR.europeanism = 0.95 }
			set_variable = { SPR.europeanism = 0.94 }
			set_variable = { SWE.europeanism = 0.62 }
			set_variable = { SOV.europeanism = 0.05 }
			set_variable = { VTB.europeanism = 0.5 }

			EU_update_vars = yes
			EU_init_focus_vote_weights = yes
			if = {
				limit = { has_game_rule = { rule = rule_european_union_expansion_focus option = yes } }
				EU_apply_expansion_focus_weights = yes
			}
			for_each_scope_loop = {
				array = global.EU_member
				tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
				activate_mission = EU_budget_draft_preparations
				activate_mission = EU_mff_draft_preparations
				set_variable = { THIS.EU_budget_call_rate = 1 }
			}
			set_global_flag = EU_budget_EP_block
			set_variable = { global.EU_budget_min_call_rate = 1 }
			set_EU_gdp = yes
			EU_global_click_cost = yes
			update_EU_budget_call_rate = yes
			update_EU_average_eco_lvl = yes
			set_EU_expenditure = yes
			distribute_EU_budget = yes
		}

		set_variable = { global.eucouncil = EUU.id }
		set_variable = { global.eu_commission = EUU.id }
		set_variable = { global.eucentralbank = EUU.id }
		set_variable = { global.euforeignaffairs = EUU.id }
		set_variable = { global.euparliamentpresident = EUU.id }
		set_variable = { global.eufinance = EUU.id }
		set_variable = { global.eufrontex = EUU.id }
		set_variable = { global.euarmy = EUU.id }
		set_variable = { global.eunavy = EUU.id }
		set_variable = { global.euambassador = EUU.id }
		set_variable = { global.eupresfederation = EUU.id }

		clear_array = global.MEP_PG_party_array
		resize_array = { global.MEP_PG_party_array = 24 }
		clear_array = global.MEP_Ratio_PG_array
		resize_array = { global.MEP_Ratio_PG_array = 24 }
		clear_array = global.PG_party_support_array
		resize_array = { global.PG_party_support_array = 24 }
		clear_array = global.EU_draft_party_array
		resize_array = { global.EU_draft_party_array = 24 }
	}

	update_eu_dirty_variable = yes
}

set_the_eu_parliament_delay = {
	hidden_effect = {
		if = {
			limit = { has_global_flag = GAME_RULE_eu_one_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 1 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 1 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 1 }
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_sixty_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 60 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 60 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 74 }
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_ninety_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 90 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 90 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 104 }
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_one_hundred_twenty_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 120 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 120 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 134 }
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_one_hundred_eighty_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 180 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 180 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 194 }
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_two_hundred_ten_cooldown }
			for_each_scope_loop = {
				array = global.EU_member
				set_country_flag = { flag = delay_next_parliament_vote value = 1 days = 210 }
				if = {
					limit = { is_ai = no }
					country_event = { id = EUvote.3 days = 210 }
				}
			}
			set_global_flag = { flag = EP_AI_hold_their_vote value = 1 days = 224 }
		}
	}
}

set_the_eu_council_delay = {
	hidden_effect = {
		if = {
			limit = { has_global_flag = GAME_RULE_eu_one_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 1 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 1 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 1 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 1 }
			}
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_sixty_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 60 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 60 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 74 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 60 }
			}
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_ninety_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 90 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 90 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 104 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 90 }
			}
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_one_hundred_twenty_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 120 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 120 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 134 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 120 }
			}
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_one_hundred_eighty_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 180 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 180 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 194 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 180 }
			}
		}
		else_if = {
			limit = { has_global_flag = GAME_RULE_eu_two_hundred_ten_cooldown }
			set_country_flag = { flag = delay_player_next_council_vote value = 1 days = 210 }
			set_global_flag = { flag = delay_next_council_vote value = 1 days = 210 }
			set_global_flag = { flag = delay_next_council_vote_ai value = 1 days = 224 }
			if = {
				limit = { is_ai = no }
				country_event = { id = EUvote.4 days = 210 }
			}
		}
	}
}

# POTEF Scripted Effects
POTEF_election = {
	clear_POTEF_variables = yes
	clear_array = POTEF_winner_array
	clear_array = POTEF_pop_vote_array
	clear_array = POTEF_pop_vote_ratio_array
	resize_array = { POTEF_winner_array = 24 }
	resize_array = { POTEF_pop_vote_array = 24 }
	resize_array = { POTEF_pop_vote_ratio_array = 24 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		election_POTEF_in_member_state = yes
	}
	update_POTEF_arrays = yes
	update_eu_dirty_variable = yes
}

election_POTEF_in_member_state = {
	for_each_loop = {
		array = party_pop_array
		set_variable = {
			POTEF_pop_vote^i = {
				value = party_pop_array^i
				multiply = population_total
				multiply = 100 # thousands, as the POTEF vote tooltip shows
			}
		}
		add_to_variable = { global.POTEF_pop_vote^i = THIS.POTEF_pop_vote^i }
		add_to_variable = { global.POTEF_pop_vote_total = THIS.POTEF_pop_vote^i }
	}
	find_highest_in_array = {
		array = party_pop_array
		value = max_1_tmp
		index = max_index_1_tmp
	}
	set_variable = { THIS.POTEF_winner_value = max_1_tmp }
	set_variable = { THIS.POTEF_winner = max_index_1_tmp }
	add_to_variable = { global.POTEF_winner^max_index_1_tmp = 1 }
	update_eu_dirty_variable = yes
}

clear_POTEF_variables = {
	clear_variable = global.POTEF_pop_vote_total
	for_loop_effect = {
		end = 24
		value = i
		clear_variable = global.POTEF_winner^i
		clear_variable = global.POTEF_pop_vote^i
		clear_variable = global.POTEF_pop_vote_ratio^i
	}
	update_eu_dirty_variable = yes
}

update_POTEF_arrays = {
	for_each_loop = {
		array = POTEF_winner_array
		set_variable = { POTEF_winner_array^i = global.POTEF_winner^i }
	}
	for_each_loop = {
		array = POTEF_pop_vote_array
		set_variable = { POTEF_pop_vote_array^i = global.POTEF_pop_vote^i }
	}
	clamp_variable = { var = global.POTEF_pop_vote_total min = 1 }
	for_each_loop = {
		array = POTEF_pop_vote_ratio_array
		set_variable = { var = global.POTEF_pop_vote_ratio^i value = { value = global.POTEF_pop_vote^i divide = global.POTEF_pop_vote_total } }
		set_variable = { POTEF_pop_vote_ratio_array^i = global.POTEF_pop_vote_ratio^i }
	}
	update_eu_dirty_variable = yes
}

set_POTEF_elect = {
	if = {
		limit = {
			absolute_majority_member_states = yes
			absolute_majority_popular_vote = yes
			#identical?
			check_variable = { max_index_1_tmp = max_index_2_tmp }
		}
		set_variable = { POTEF_elect = max_index_2_tmp }
		### five-year term
		set_variable = { POTEF_term = 1825 } #1825
	}
	else_if = {
		limit = {
			OR = {
				AND = {
					relative_majority_member_states = yes
					absolute_majority_popular_vote = yes
				}
				AND = {
					absolute_majority_member_states = yes
					relative_majority_popular_vote = yes
				}
			}
			#identical?
			check_variable = { max_index_1_tmp = max_index_2_tmp }
		}
		set_variable = { POTEF_elect = max_index_2_tmp }
		### three-year term
		set_variable = { POTEF_term = 1095 } #1095
	}
	else_if = {
		limit = {
			relative_majority_member_states = yes
			relative_majority_popular_vote = yes
			#identical?
			check_variable = { max_index_1_tmp = max_index_2_tmp }
		}
		set_variable = { POTEF_elect = max_index_2_tmp }
		### two-year term
		set_variable = { POTEF_term = 730 } #730
	}
	else_if = {
		limit = {
			relative_majority_popular_vote = yes
		}
		set_variable = { POTEF_elect = max_index_2_tmp }
		### one-year term
		set_variable = { POTEF_term = 365 }	#365
	}

	set_variable = {
		var = POTEF_time_to_camp
		value = {
			value = POTEF_term
			subtract = 90
		}
	}
	set_variable = { var = POTEF_years value = { value = POTEF_term divide = 365 } }

	set_temp_variable = { potef_term_temp = ROOT.POTEF_term }
	# The popular-vote winner is independent of who nominated, so the winning subideology may have no
	# registered nominee (value 0). Scoping into country id 0 is invalid, so only assign when one exists.
	if = {
		limit = { check_variable = { var = global.POTEF_nominee_country^POTEF_elect value = 0 compare = greater_than } }
		# Winning subideology's nominee country (POTEF_nominee_country^POTEF_elect holds its id)
		var:global.POTEF_nominee_country^POTEF_elect = {
			# Set the President of the European Federation
			add_timed_idea = {
				idea = EU_president
				days = potef_term_temp
			}
			set_variable = { global.eupresfederation = THIS.id }

			activate_mission = EU_POTEF_term
			news_event = EU_POTEF_news.2
		}
	}

	# End Configuration
	force_update_map_mode = { mapmode = EU_map_mode }
	clear_potef_electoral_values = yes
	update_eu_dirty_variable = yes
}

clear_potef_electoral_values = {
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		clr_country_flag = POTEF_has_nominated
	}
	clr_global_flag = ongoing_potef_election
	for_loop_effect = {
		end = 24
		value = v
		set_variable = { global.POTEF_nominee_country^v = 0 }
	}
}

update_EU_average_gdpc_lvl = { # For calcuations that don't have set_EU_gdp
	set_variable = { global.EU_gdp_capita = 0 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		add_to_variable = { global.EU_gdp_capita = THIS.gdp_total }
	}
	clamp_variable = { var = global.var_EUpopTotalDisplay min = 0.001 }
	divide_variable = { global.EU_gdp_capita = global.var_EUpopTotalDisplay }
	update_eu_dirty_variable = yes
}

update_EU_average_eco_lvl = {
	set_variable = { global.EU_average_eco_lvl = 0 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		# Map the country's mutually exclusive economic-cycle idea to its score.
		set_temp_variable = { eco_score = 0 }
		if = { limit = { has_idea = economic_boom } set_temp_variable = { eco_score = 6 } }
		else_if = { limit = { has_idea = fast_growth } set_temp_variable = { eco_score = 5 } }
		else_if = { limit = { has_idea = stable_growth } set_temp_variable = { eco_score = 4 } }
		else_if = { limit = { has_idea = stagnation } set_temp_variable = { eco_score = 3 } }
		else_if = { limit = { has_idea = recession } set_temp_variable = { eco_score = 2 } }
		else_if = { limit = { has_idea = depression } set_temp_variable = { eco_score = 1 } }
		set_variable = { EU_average_eco_lvl = eco_score }
		add_to_variable = { global.EU_average_eco_lvl = eco_score }
	}
	if = {
		limit = { check_variable = { global.EU_member^num > 0 } }
		divide_variable = { global.EU_average_eco_lvl = global.EU_member^num }
	}
	update_eu_dirty_variable = yes
}

set_EU_gdp = {
	# Calculate the EU Total GDP
	set_variable = { global.EU_gdp = 0 }
	for_each_scope_loop = {
		array = global.EU_member
		add_to_variable = { global.EU_gdp = THIS.gdp_total } #This value is for display purposes
	}

	clamp_variable = { var = global.EU_gdp min = 0.001 }

	# Calculate the individual ratio of GDP per nation
	for_each_scope_loop = {
		array = global.EU_member
		set_variable = {
			EU_ratio_of_total_eu_gdp = {
				value = gdp_total
				divide = global.EU_gdp
			}
		}
	}

	# Calculate the EU GDP/C
	update_EU_average_gdpc_lvl = yes

	update_eu_dirty_variable = yes
}

EU_save_budget_values = {
	set_variable = { global.EU_global_exp_saved = global.EU_global_exp_ratio }
	set_variable = { global.EU_growth_exp_saved = global.EU_growth_exp_ratio }
	set_variable = { global.EU_natural_exp_saved = global.EU_natural_exp_ratio }
}

EU_save_mff_values = {
	set_variable = { global.EU_min_call_rate_saved = global.EU_budget_min_call_rate }
	set_variable = { global.EU_global_call_rate_saved = global.EU_budget_call_rate }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = { THIS.EU_call_rate_saved = THIS.EU_budget_call_rate }
	}
}

EU_rollback_budget_values = {
	set_variable = { global.EU_global_exp_ratio = global.EU_global_exp_saved }
	set_variable = { global.EU_growth_exp_ratio = global.EU_growth_exp_saved }
	set_variable = { global.EU_natural_exp_ratio = global.EU_natural_exp_saved }
	distribute_EU_budget = yes
}

EU_rollback_mff_values = {
	set_variable = { global.EU_budget_min_call_rate = global.EU_min_call_rate_saved }
	set_variable = { global.EU_budget_call_rate = global.EU_global_call_rate_saved }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = { THIS.EU_budget_call_rate = THIS.EU_call_rate_saved }
	}
	update_EU_budget_call_rate = yes
	distribute_EU_budget = yes
}

EU_military_exercise_cleanup = {
	if = {
		limit = { check_variable = { ROOT.var_EU_military_exercise_target > 0 } }
		var:ROOT.var_EU_military_exercise_target = {
			clr_country_flag = EU_military_exercise_at_border_from@ROOT
			clr_country_flag = EU_host_military_exercise@ROOT
		}
	}
	if = {
		limit = { check_variable = { ROOT.var_EU_military_exercise_host_tag > 0 } }
		var:ROOT.var_EU_military_exercise_host_tag = {
			clr_country_flag = EU_military_exercise_at_border_from@ROOT
			clr_country_flag = EU_host_military_exercise@ROOT
		}
	}
	clr_country_flag = EU_military_exercise_prep
	clear_variable = ROOT.var_EU_military_exercise_target
	clear_variable = ROOT.var_EU_military_exercise_host_state
	clear_variable = ROOT.var_EU_military_exercise_host_tag
}

update_EU_budget_call_rate = {
	set_variable = { global.EU_total_budget = 0 }
	### member state contribution
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = {
			EU_budget_contribution = {
				value = gdp_total
				multiply = EU_budget_call_rate
				multiply = 0.01
			}
		}
		add_to_variable = { global.EU_total_budget = THIS.EU_budget_contribution }
	}
	# EUR seigniorage income accumulated while EUU was not active
	if = { limit = { check_variable = { global.EU_seigniorage_pool > 0 } }
		add_to_variable = { global.EU_total_budget = global.EU_seigniorage_pool }
		set_variable = { global.EU_seigniorage_pool = 0 }
	}
	clamp_variable = { var = global.EU_total_budget min = 0.001 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = {
			var = THIS.EU_budget_contribution_weekly
			value = {
				value = THIS.EU_budget_contribution
				divide = 52 #Weeks in a year
			}
		}
		set_variable = {
			var = THIS.EU_budget_ratio
			value = {
				value = THIS.EU_budget_contribution
				divide = global.EU_total_budget
			}
		}
	}
	### EU Total Budget
	set_variable = { var = global.EU_mff_total value = { value = global.EU_total_budget multiply = 5 } }

	update_eu_dirty_variable = yes
}

EU_global_click_cost = {
	set_variable = { var = global.EU_10_per_member_cost value = { value = 10 multiply = global.EU_member^num } }
	set_variable = { var = global.EU_10_per_member_cost_minus value = { value = global.EU_10_per_member_cost multiply = -1 } }
	set_variable = { var = global.EU_15_per_member_cost value = { value = 15 multiply = global.EU_member^num } }
	set_variable = { var = global.EU_15_per_member_cost_minus value = { value = global.EU_15_per_member_cost multiply = -1 } }
}

EU_country_call_rate_cost_calc = {
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = { var = THIS.EU_mff_country_cost value = { value = 1000 multiply = THIS.EU_budget_ratio } }
		set_variable = { var = THIS.EU_mff_country_cost_minus value = { value = THIS.EU_mff_country_cost multiply = -1 } }
	}
}

set_EU_expenditure = {
	set_variable = { global.EU_total_exp = global.EU_total_budget }
	set_variable = { global.EU_growth_exp_ratio = 0.44 }
	set_variable = { global.EU_natural_exp_ratio = 0.44 }
	set_variable = { global.EU_global_exp_ratio = 0.06 }
	set_variable = { global.EU_admin_exp_ratio = 0.06 }
	set_variable = { global.EU_growth_exp_total = { value = global.EU_growth_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_natural_exp_total = { value = global.EU_natural_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_global_exp_total = { value = global.EU_global_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_admin_exp_total = { value = global.EU_admin_exp_ratio multiply = global.EU_total_exp } }
	update_eu_dirty_variable = yes
}

update_EU_expenditure = {
	set_variable = { global.EU_growth_exp_total = { value = global.EU_growth_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_natural_exp_total = { value = global.EU_natural_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_global_exp_total = { value = global.EU_global_exp_ratio multiply = global.EU_total_exp } }
	set_variable = { global.EU_admin_exp_total = { value = global.EU_admin_exp_ratio multiply = global.EU_total_exp } }
	update_eu_dirty_variable = yes
}


distribute_EU_budget = {
	distribute_EU_growth_exp = yes
	distribute_EU_natural_exp = yes
	### distribute_EU_global_exp
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		if = {
			limit = { EU_is_leader_of_foreign_policy = yes }
			set_variable = { EU_global_exp_subsidies = global.EU_global_exp_total }
		}
	}
	distribute_EU_admin_exp = yes
	EU_budget_total_gain = yes
	update_eu_dirty_variable = yes
}

distribute_EU_growth_exp = {
	set_variable = { global.EU_growth_subsidies_total = 0 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		# Map the country's mutually exclusive economic-cycle idea to a tier; tier = 0 means no recognized idea (skipped below).
		set_temp_variable = { tier = 0 }
		if = { limit = { has_idea = economic_boom } set_temp_variable = { tier = 1 } }
		else_if = { limit = { has_idea = fast_growth } set_temp_variable = { tier = 2 } }
		else_if = { limit = { has_idea = stable_growth } set_temp_variable = { tier = 3 } }
		else_if = { limit = { has_idea = stagnation } set_temp_variable = { tier = 4 } }
		else_if = { limit = { has_idea = recession } set_temp_variable = { tier = 5 } }
		else_if = { limit = { has_idea = depression } set_temp_variable = { tier = 6 } }
		if = { limit = { check_variable = { tier > 0 } }
			set_variable = { var = EU_growth_subsidies value = { value = tier multiply = EU_pop_ratio } }
			add_to_variable = { global.EU_growth_subsidies_total = EU_growth_subsidies }
		}
	}
	clamp_variable = { var = global.EU_growth_subsidies_total min = 0.001 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = {
			var = THIS.EU_growth_subsidies
			value = {
				value = THIS.EU_growth_subsidies
				divide = global.EU_growth_subsidies_total
				multiply = global.EU_growth_exp_total
			}
		}
	}
	update_eu_dirty_variable = yes
}

distribute_EU_natural_exp = {
	set_variable = { global.EU_natural_subsidies_total = 0 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		# Map the country's gdp_per_capita to a tier. Each band is mutually exclusive so sequential ifs are safe; we use `less_than_or_equals` because the original ladder excludes the upper bound of every band (e.g. 200 falls to tier 2 in the original because the tier-1 check is `> 199.999`).
		set_temp_variable = { tier = 1 }
		if = { limit = { check_variable = { var = gdp_per_capita value = 199.999 compare = less_than_or_equals } } set_temp_variable = { tier = 2 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 139.999 compare = less_than_or_equals } } set_temp_variable = { tier = 3 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 89.999 compare = less_than_or_equals } } set_temp_variable = { tier = 4 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 49.999 compare = less_than_or_equals } } set_temp_variable = { tier = 5 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 29.999 compare = less_than_or_equals } } set_temp_variable = { tier = 6 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 19.999 compare = less_than_or_equals } } set_temp_variable = { tier = 7 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 14.999 compare = less_than_or_equals } } set_temp_variable = { tier = 8 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 9.999 compare = less_than_or_equals } } set_temp_variable = { tier = 9 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 6.999 compare = less_than_or_equals } } set_temp_variable = { tier = 10 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 3.999 compare = less_than_or_equals } } set_temp_variable = { tier = 11 } }
		if = { limit = { check_variable = { var = gdp_per_capita value = 1.999 compare = less_than_or_equals } } set_temp_variable = { tier = 12 } }
		set_variable = { var = EU_natural_subsidies value = { value = tier multiply = EU_pop_ratio } }
		add_to_variable = { global.EU_natural_subsidies_total = EU_natural_subsidies }
	}
	clamp_variable = { var = global.EU_natural_subsidies_total min = 0.001 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = {
			var = THIS.EU_natural_subsidies
			value = {
				value = THIS.EU_natural_subsidies
				divide = global.EU_natural_subsidies_total
				multiply = global.EU_natural_exp_total
			}
		}
	}
	update_eu_dirty_variable = yes
}


distribute_EU_admin_exp = {
	if = {
		limit = {
			has_global_flag = EU_frontex_taken_flag
			has_global_flag = EU_commission_taken_flag
		}
		for_each_scope_loop = {
			array = global.EU_member
			tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
			if = {
				limit = { has_idea = frontex_ExDir }
				set_variable = { var = THIS.EU_admin_subsidies value = { value = 0.5 multiply = global.EU_admin_exp_total } }
			}
			if = {
				limit = { has_idea = EU_commission_president }
				set_variable = { var = THIS.EU_admin_subsidies value = { value = 0.5 multiply = global.EU_admin_exp_total } }
			}
		}
	}
	else_if = {
		limit = {
			has_global_flag = EU_frontex_taken_flag
			NOT = { has_global_flag = EU_commission_taken_flag }
		}
		for_each_scope_loop = {
			array = global.EU_member
			tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
			if = {
				limit = { has_idea = frontex_ExDir }
				set_variable = { THIS.EU_admin_subsidies = global.EU_admin_exp_total }
			}
		}
	}
	else_if = {
		limit = {
			NOT = { has_global_flag = EU_frontex_taken_flag }
			has_global_flag = EU_commission_taken_flag
		}
		for_each_scope_loop = {
			array = global.EU_member
			tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
			if = {
				limit = { has_idea = EU_commission_president }
				set_variable = { THIS.EU_admin_subsidies = global.EU_admin_exp_total }
			}
		}
	}
	update_eu_dirty_variable = yes
}

EU_budget_total_gain = {
	clamp_variable = { var = global.EU_total_exp min = 0.001 }
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		set_variable = { var = THIS.EU_budget_total_gain value = { value = 0 add = THIS.EU_growth_subsidies add = THIS.EU_natural_subsidies add = THIS.EU_global_exp_subsidies add = THIS.EU_admin_subsidies } }
		set_variable = {
			var = THIS.EU_budget_ratio_gain
			value = {
				value = THIS.EU_budget_total_gain
				divide = global.EU_total_exp
			}
		}
		set_variable = { var = THIS.EU_budget_net value = { value = THIS.EU_budget_total_gain subtract = THIS.EU_budget_contribution } }
	}
	update_eu_dirty_variable = yes
}

######################
######################
### EU Parliament ####
######################
######################
clear_EU_Parliament_MEP_THIS = {
	clear_array = THIS.MEP_party_array
	clear_variable = THIS.MEP_Total
	clear_variable = THIS.MEP_Total_target
	clear_variable = THIS.highest_MEP_party_value
	clear_variable = THIS.highest_MEP_party_index
	clear_variable = THIS.var77
}

clear_EU_Parliament = {
	clear_array = global.MEP_PG_party_array
	resize_array = { global.MEP_PG_party_array = 24 }
	clear_array = global.MEP_Ratio_PG_array
	resize_array = { global.MEP_Ratio_PG_array = 24 }

	for_each_scope_loop = {
		array = global.EU_potential
		clear_EU_Parliament_MEP_THIS = yes
	}

	clear_variable = global.MEP_Total
	clear_variable = global.MEP_Majority
	update_eu_dirty_variable = yes
}

election_EU_Parliament = {
	# set max MEPs
	if = {
		limit = {
			focus_EU651_accepted = yes
			focus_EU104_accepted = yes
		}
		set_variable = { global.EU_Parliament_max_MEP = 1000 }
	}
	else_if = {
		limit = {
			NOT = { focus_EU651_accepted = yes }
			focus_EU104_accepted = yes
		}
		set_variable = { global.EU_Parliament_max_MEP = 750 }
	}
	else = { set_variable = { global.EU_Parliament_max_MEP = 732 } }

	# set MEP clamps for countries
	if = {
		limit = {
			focus_EU651_accepted = yes
			focus_EU104_accepted = yes
		}
		set_variable = { global.min_clamp_eu_mep = 8 }
	}
	else_if = {
		limit = {
			NOT = { focus_EU651_accepted = yes }
			focus_EU104_accepted = yes
		}
		set_variable = { global.min_clamp_eu_mep = 6 }
	}
	else_if = {
		limit = {
			NOT = { focus_EU651_accepted = yes }
			NOT = { focus_EU104_accepted = yes }
		}
		set_variable = { global.min_clamp_eu_mep = 5 }
	}
	if = {
		limit = {
			focus_EU651_accepted = yes
			focus_EU104_accepted = yes
		}
		set_variable = { global.max_clamp_eu_mep = 100 }
	}
	else_if = {
		limit = {
			NOT = { focus_EU651_accepted = yes }
			focus_EU104_accepted = yes
		}
		set_variable = { global.max_clamp_eu_mep = 96 }
	}
	else_if = {
		limit = {
			NOT = { focus_EU651_accepted = yes }
			NOT = { focus_EU104_accepted = yes }
		}
		set_variable = { global.max_clamp_eu_mep = 99 }
	}

	### Election in member_state
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		election_EU_Parliament_in_member_state = yes
	}

	set_variable = { global.MEP_Total = 0 } # Recalculate to account for roundings
	for_each_scope_loop = {
		array = global.EU_member
		tooltip = TT_ALL_EU_MEMBER_NATIONS_GAIN
		add_to_variable = { global.MEP_Total = THIS.MEP_Total }
	}

	### check political groups
	clamp_variable = { var = global.MEP_Total min = 1 }
	for_loop_effect = {
		end = 24
		value = p_number
		set_variable = { var = global.MEP_Ratio_PG_array^p_number value = { value = global.MEP_PG_party_array^p_number divide = global.MEP_Total } }
	}

	set_variable = { global.MEP_Total = 0 } # Recalculate to account for more roundings
	for_loop_effect = {
		end = 24
		value = p_number
		add_to_variable = { global.MEP_Total = global.MEP_PG_party_array^p_number }
	}

	set_variable = { var = global.MEP_Majority value = { value = global.MEP_Total multiply = 0.5 add = 1 } }

	update_eu_dirty_variable = yes
}
election_EU_Parliament_in_member_state = {
	clear_array = THIS.MEP_party_array
	resize_array = { THIS.MEP_party_array = 24 }

	###  MEP for this member state
	set_temp_variable = {
		temp1 = {
			value = global.EU_Parliament_max_MEP
			multiply = THIS.EU_pop_ratio
			round = yes
			clamp = { min = global.min_clamp_eu_mep max = global.max_clamp_eu_mep }
		}
	}
	set_variable = { THIS.MEP_Total = temp1 }
	set_variable = { THIS.MEP_Total_target = THIS.MEP_Total }

	### MEP per subideology
	for_loop_effect = {
		end = 24
		value = p_number
		set_variable = { var = THIS.MEP_party_array^p_number value = { value = party_pop_array^p_number multiply = THIS.MEP_Total round = yes } }
	}

	set_variable = { THIS.MEP_Total = 0 }
	### MEP per subideology # Recalc for roundings
	for_loop_effect = {
		end = 24
		value = p_number
		add_to_variable = { THIS.MEP_Total = THIS.MEP_party_array^p_number }
	}

	set_temp_variable = { value_temp = 0 }
	set_temp_variable = { index_temp = 0 }

	find_highest_in_array = {
		array = THIS.MEP_party_array
		value = value_temp
		index = index_temp
	}
	set_variable = { THIS.highest_MEP_party_value = value_temp }
	set_variable = { THIS.highest_MEP_party_index = index_temp }
	set_variable = { THIS.var77 = THIS.highest_MEP_party_value }

	if = {
		limit = { NOT = { check_variable = { THIS.MEP_Total_target = THIS.MEP_Total } } }

		set_temp_variable = { highest_index = THIS.highest_MEP_party_index }
		# Over- and under-count both resolve to the same delta: highest party += (target - current)
		add_to_variable = {
			var = THIS.MEP_party_array^highest_index
			value = {
				value = THIS.MEP_Total_target
				subtract = THIS.MEP_Total
			}
		}
	}

	### Recalc total + PG
	set_variable = { THIS.MEP_Total = 0 }
	for_loop_effect = {
		end = 24
		value = p_number
		add_to_variable = { THIS.MEP_Total = THIS.MEP_party_array^p_number }
		add_to_variable = { global.MEP_PG_party_array^p_number = THIS.MEP_party_array^p_number }
	}
}

# Rebuilds the member list shown in the EU Parliament party-detail panel.
# Runs after global.EU_selected_party is set by a party-row button click; the
# resulting global.EU_MEP_members_current array feeds the eu_party_members_list gridbox.
EU_select_party_members = {
	clear_array = global.EU_MEP_members_current
	set_temp_variable = { sel_party = global.EU_selected_party }
	for_each_scope_loop = {
		array = global.EU_member
		if = {
			limit = {
				check_variable = {
					var = THIS.MEP_party_array^sel_party
					value = 0
					compare = greater_than
				}
			}
			set_variable = { THIS.MEP_party_selected_display = THIS.MEP_party_array^sel_party }
			add_to_array = { global.EU_MEP_members_current = THIS }
		}
	}
}

#################################
### EU Parliament legislation ###
#################################
set_ruling_party_PG_support = {
	log = "[GetDateText]: [THIS.GetName]: set_ruling_party_PG_support"
	set_temp_variable = { indexed_addition = 0 }

	for_loop_effect = {
		end = 24
		value = party_number
		if = {
			limit = { check_variable = { ruling_party = party_number } }
			set_variable = { global.PG_party_support_array^party_number = 1 }
			set_temp_variable = { indexed_addition = global.MEP_PG_party_array^party_number }
		}
	}

	set_variable = { global.active_MEP_Support = indexed_addition }
	update_eu_dirty_variable = yes
}

reset_MEP_support_party_x = {
	clear_array = global.EU_draft_party_array
	resize_array = { global.EU_draft_party_array = 24 }
}

all_MEP_support_draft = {
	for_loop_effect = {
		end = 24
		value = party_number
		set_variable = { global.EU_draft_party_array^party_number = 10 }
	}
}

assess_MEP_support_party_x = {
	for_loop_effect = {
		end = 24
		value = party_number
		if = {
			limit = {
				check_variable = {
					var = global.EU_draft_party_array^party_number
					value = 0
					compare = greater_than
				}
			}
			set_variable = { global.PG_party_support_array^party_number = 1 }
			add_to_variable = { global.active_MEP_Support = global.MEP_PG_party_array^party_number }
		}
	}
	update_eu_dirty_variable = yes
}

####################################
### clr_global_flag_PG_X_support ###
####################################

clr_global_flag_PG_X_support = {
	clear_array = global.PG_party_support_array
	resize_array = { global.PG_party_support_array = 24 }
	update_eu_dirty_variable = yes
}

joining_the_euro = {
	set_country_flag = EU_joined_the_eurozone
	if = {
		limit = { has_idea = us_dollar }
		swap_ideas = {
			remove_idea = us_dollar
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = chinese_yuan }
		swap_ideas = {
			remove_idea = chinese_yuan
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = chinese_yuan_2 }
		swap_ideas = {
			remove_idea = chinese_yuan_2
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = chinese_yuan_3 }
		swap_ideas = {
			remove_idea = chinese_yuan_3
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = japanese_yen }
		swap_ideas = {
			remove_idea = japanese_yen
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = russia_rouble }
		swap_ideas = {
			remove_idea = russia_rouble
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = british_pound }
		swap_ideas = {
			remove_idea = british_pound
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = swiss_frank }
		swap_ideas = {
			remove_idea = swiss_frank
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = gulden }
		swap_ideas = {
			remove_idea = gulden
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = brazilian_real }
		swap_ideas = {
			remove_idea = brazilian_real
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = bitcoin }
		swap_ideas = {
			remove_idea = bitcoin
			add_idea = euro
		}
	}
	if = {
		limit = { has_idea = no_foreign_reserve }
		swap_ideas = {
			remove_idea = no_foreign_reserve
			add_idea = euro
		}
	}
}

EU_adopt_passed_laws = {
	if = {
		limit = { NOT = { has_country_flag = adopt_eu_law } }
		set_country_flag = adopt_eu_law
		# focus_EU101
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 101 }
			}
			add_ideas = EUU_cfr_eu
		}
		# focus_EU201
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 201 }
			}
			add_ideas = ECB_zero_interest_rate_policy
		}
		# focus_EU202
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 202 }
			}
			add_stability = 0.05
			remove_ideas = european_debt_crisis
		}
		# focus_EU203
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 203 }
			}
			add_ideas = euro_bonds
			remove_ideas = european_debt_crisis
		}
		# focus_EU204
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 204 }
			}
			add_stability = 0.10
		}
		# focus_EU205
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 205 }
			}
			add_ideas = euro_rescue_package
			remove_ideas = european_debt_crisis
		}
		# focus_EU206
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 206 }
			}
			add_ideas = fiscal_union
		}
		# focus_EU207
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 207 }
			}
			add_ideas = European_Economic_Government
		}
		# focus_EU301
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 301 }
			}
			add_to_tech_sharing_group = EU_Tech_Share
		}
		# focus_EU302
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 302 }
			}
			modify_tech_sharing_bonus = { id = EU_Tech_Share bonus = 0.08 } hidden_effect = { rq_ms_34e3b731b6 = yes }
		}
		# focus_EU303
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 303 }
			}
			add_ideas = European_Defence_Fund
		}
		# focus_EU304
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 304 }
			}
			add_ideas = European_Security_Council
		}
		# focus_EU401
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 401 }
			}
			add_ideas = EUU_idea_gdpr
			remove_ideas = foreign_data_kraken
		}
		else = {
			# GDPR not passed yet — new member inherits the baseline data penalty unless already on GDPR
			if = {
				limit = { NOT = { has_idea = EUU_idea_gdpr } }
				add_ideas = foreign_data_kraken
			}
		}
		# focus_EU402
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 402 }
			}
			add_ideas = EU_copyright
			add_to_variable = { party_pop_array^1 = -0.03 }
			add_popularity = { ideology = democratic popularity = -0.03 }
			add_to_variable = { party_pop_array^6 = -0.03 }
			add_popularity = { ideology = communism popularity = -0.03 }
			add_to_variable = { party_pop_array^14 = -0.03 }
			add_popularity = { ideology = neutrality popularity = -0.03 }
			recalculate_party = yes
		}
		# focus_EU403
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 403 }
			}
			remove_ideas = asylum_shopping
			add_to_variable = { party_pop_array^20 = 0.03 }
			add_popularity = { ideology = nationalist popularity = 0.03 }
			recalculate_party = yes
		}
		else = {
			# EU403 not passed yet — new member inherits the baseline asylum penalty
			add_ideas = asylum_shopping
		}
		# focus_EU404
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 404 }
			}
			if = {
				limit = { has_global_flag = EUU_migrant_crisis_triggered }
				remove_ideas = Migrant_Crisis_Europe
			}
			add_ideas = relocation_asylum_seekers
			add_to_variable = { party_pop_array^20 = 0.05 }
			add_popularity = { ideology = nationalist popularity = 0.05 }
			recalculate_party = yes
		}
		# focus_EU405
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 405 }
			}
			if = {
				limit = { has_global_flag = EUU_migrant_crisis_triggered }
				remove_ideas = Migrant_Crisis_Europe
			}
		}
		# focus_EU406
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 406 }
			}
			remove_ideas = relocation_asylum_seekers
			remove_ideas = EU_Turkey_deal
			add_ideas = European_Asylum_Agency
		}
		# focus_EU407
		if = {
			limit = {
				is_in_array = { array = global.EU_passed_votes value = 407 }
			}
			add_ideas = Frontex
		}
	}
}
