﻿succession_crisis_diplomatic_transfer_effect = {
	root = {
		save_scope_as = winner
	}
	involved_activity = {
		activity_host = {
			save_scope_as = loser
		}
	}

	if = {
		limit = { NOT = { exists = scope:crisis_title } }
		scope:loser = {
			var:crisis_title = { save_scope_as = crisis_title }
		}
	}

	if = {
		limit = { NOT = { exists = scope:crisis_destination } }
		if = {
			limit = { exists = scope:crisis_title }
			scope:crisis_title = {
				holder = {
					capital_province = { save_scope_as = crisis_destination }
				}
			}
		}
	}

	scope:loser = {
		every_vassal = {
			add_to_list = crisis_vassals
		}
	}
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = change
		add_claim_on_loss = no
	}

	# Ensure the capital county is transferred along with the Kingdom/Title
	scope:crisis_destination = {
		county = {
			save_scope_as = new_capital_location
			change_title_holder = {
				holder = scope:winner
				change = scope:change
			}
		}
	}

	scope:crisis_title = {
		change_title_holder = {
			holder = scope:winner
			change = scope:change
		}
	}

	hidden_effect = {
		if = {
			limit = {
				scope:crisis_title.tier > scope:winner.primary_title.tier
			}
			scope:crisis_destination = {
				county = {
					kingdom = {
						change_title_holder = {
							holder = scope:winner
							change = scope:change
						}
					}
				}
			}
		}
	}

	resolve_title_and_vassal_change = scope:change

	# Move the Winner's Capital
	if = {
		limit = {
			exists = scope:new_capital_location
			scope:new_capital_location.holder = scope:winner
		}
		scope:winner = {
			set_realm_capital = scope:new_capital_location
		}
	}

	hidden_effect = {
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = fealty_change
			add_claim_on_loss = no
		}
		if = {
			limit = {
				scope:loser = {
					is_alive = yes
					NOT = { this = scope:winner }
					any_held_title = { count >= 1 }
				}
			}
			scope:loser = {
				change_liege = {
					liege = scope:winner
					change = scope:fealty_change
				}
			}
		}
		every_in_list = {
			list = crisis_vassals
			change_liege = {
				liege = scope:winner
				change = scope:fealty_change
			}
		}
		resolve_title_and_vassal_change = scope:fealty_change
	}

	scope:winner = {
		remove_character_flag = debating
	}
	scope:loser = {
		remove_character_flag = debating
	}
}