establish_se_integrated_personal_union = {
	category = influence

	require_acceptance = no

	is_visible = {
		can_create_subject_type = {
			subject = FROM
			subject_type = se_integrated_personal_union
		}
	}

	is_allowed = {
		can_create_subject_type = {
			subject = FROM
			subject_type = se_integrated_personal_union
		}
		NOT = { truce_with = FROM }
		is_at_war = no
		FROM = { is_at_war = no }
		FROM = { NOT = { liberty_desire = 50 } }
	}

	on_accept = {
		if = {
			limit = {
				can_create_subject_type = {
					subject = FROM
					subject_type = se_integrated_personal_union
				}
				NOT = { truce_with = FROM }
				is_at_war = no
				FROM = { is_at_war = no }
				FROM = { NOT = { liberty_desire = 50 } }
			}
			FROM = {
				if = {
					limit = {
						has_government_attribute = has_ruler_election
					}
					add_government_reform = feudalism_reform
				}
			}
			create_subject = {
				subject = FROM
				subject_type = se_integrated_personal_union
				keep_liberty_desire = yes
			}
		}
	}

	ai_will_do = {
		FROM = {
			NOT = { has_country_flag = se_integrated_personal_union_was_made_to_personal_union_again }
			NOT = { liberty_desire = 40 }
			total_development = 100
			NOT = { total_development = 300 }
			years_in_vassalage_under = {
				who = ROOT
				years = 70
				all_subject_country = yes
			}
		}
		OR = {
			NOT = { started_in = 1350.1.1 }
			started_in = 1600.1.1
			NOT = { tag = POL }
			FROM = {
				OR = {
					NOT = { tag = LIT }
					num_of_cities = 60
				}
			}
		}
	}
}

return_se_integrated_personal_union_to_personal_union = {
	category = influence

	require_acceptance = no

	is_visible = {
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = se_integrated_personal_union
		}
	}

	is_allowed = {
		hidden_trigger = {
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = se_integrated_personal_union
			}
		}
		is_at_war = no
		FROM = {
			is_at_war = no
		}
		FROM = {
			years_in_vassalage_under = {
				who = ROOT
				years = 10
				all_subject_country = yes
			}
		}
	}

	on_accept = {
		if = {
			limit = {
				hidden_trigger = {
					FROM = {
						is_subject_of = ROOT
						is_subject_of_type = se_integrated_personal_union
					}
				}
				is_at_war = no
				FROM = {
					is_at_war = no
				}
				FROM = {
					years_in_vassalage_under = {
						who = ROOT
						years = 10
						all_subject_country = yes
					}
				}
			}
			FROM = {
				set_country_flag = se_integrated_personal_union_was_made_to_personal_union_again
				add_opinion = {
					who = ROOT
					modifier = returned_to_vassal
				}
				grant_independence = yes
			}
			create_union = FROM
			add_prestige = -10
			FROM = {
				add_liberty_desire = 25
			}
		}
	}

	ai_will_do = {
		FROM = {
			OR = {
				liberty_desire = 90
				AND = {
					liberty_desire = 60
					has_support_loyalists = yes
				}
			}
			years_in_vassalage_under = {
				who = ROOT
				years = 25
				all_subject_country = yes
			}
		}
	}
}

cancel_se_integrated_personal_union = {
	category = influence

	require_acceptance = no

	is_visible = {
		NOT = { has_dlc = "Rights of Man" }
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = se_integrated_personal_union
		}
	}

	is_allowed = {
		hidden_trigger = {
			NOT = { has_dlc = "Rights of Man" }
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = se_integrated_personal_union
			}
		}
		NOT = { truce_with = FROM }
		prestige = 0
	}

	on_accept = {
		if = {
			limit = {
				NOT = { has_dlc = "Rights of Man" }
				FROM = {
					is_subject_of = ROOT
					is_subject_of_type = se_integrated_personal_union
				}
				NOT = { truce_with = FROM }
				prestige = 0
			}
			FROM = {
				if = {
					limit = {
						NOT = { liberty_desire = 50 }
					}
					add_opinion = {
						who = ROOT
						modifier = vassal_abandoned
					}
				}
				grant_independence = yes
				add_truce_with = ROOT
			}
			add_truce_with = FROM
			add_prestige = -25
		}
	}

	ai_will_do = {
		always = no
	}
}

increase_chance_to_integrate_se_integrated_personal_union = {
	category = subject_interactions

	require_acceptance = no

	is_visible = {
		FROM = {
			is_subject_of = ROOT
			is_subject_of_type = se_integrated_personal_union
		}
	}

	is_allowed = {
		hidden_trigger = {
			FROM = {
				is_subject_of = ROOT
				is_subject_of_type = se_integrated_personal_union
			}
		}
		NOT = { truce_with = FROM }
		is_at_war = no
		FROM = { is_at_war = no }
		FROM = { NOT = { liberty_desire = 50 } }
		FROM = {
			years_in_vassalage_under = {
				who = ROOT
				years = 10
				all_subject_country = yes
			}
		}
		FROM = {
			NOT = { has_country_modifier = increase_chance_to_integrate_se_integrated_personal_union_modifier }
		}
		if = {
			limit = {
				FROM = {
					total_development = 500
				}
			}
			dip_power = 200
		}
		else_if = {
			limit = {
				FROM = {
					total_development = 200
				}
			}
			dip_power = 150
		}
		else = {
			dip_power = 100
		}
	}

	on_accept = {
		if = {
			limit = {
				FROM = {
					is_subject_of = ROOT
					is_subject_of_type = se_integrated_personal_union
					NOT = { has_country_modifier = increase_chance_to_integrate_se_integrated_personal_union_modifier }
				}
				NOT = { truce_with = FROM }
				is_at_war = no
				FROM = { is_at_war = no }
				FROM = { NOT = { liberty_desire = 50 } }
				FROM = {
					years_in_vassalage_under = {
						who = ROOT
						years = 10
						all_subject_country = yes
					}
				}
			}
			if = {
				limit = {
					FROM = {
						total_development = 500
					}
				}
				add_dip_power = -200
			}
			else_if = {
				limit = {
					FROM = {
						total_development = 200
					}
				}
				add_dip_power = -150
			}
			else = {
				add_dip_power = -100
			}
			FROM = {
				add_country_modifier = {
					name = increase_chance_to_integrate_se_integrated_personal_union_modifier
					duration = 9125
				}
			}
		}
	}

	ai_will_do = {
		always = no
	}
}