SFA_collapse = {
	set_global_flag = SFA_collapse
	#First give away any owned states to their controller, if we're at war
	every_owned_state = {
		limit = { NOT = { is_controlled_by = SFA } }
		save_event_target_as = giveaway
		controller = { transfer_state = event_target:giveaway }
	}

	#Now mark all existing breakaways
	every_country = {
		limit = {
			exists = yes
		}
		set_country_flag = SFA_already_exists
	}

	#Special people
	if = {
		limit = { owns_state = 178 } #Canaries
		var:41.owner = { transfer_state = 178 } #goes to Madrid
	}
	if = {
		limit = {
			owns_state = 1318 #Sidi Ifni
		}
		var:461.owner = { country_event = sharedafrika.132 } #neighbour in Casablanca offered to take
	}
	if = {
		limit = {
			owns_state = 699 #Rio del Oro
		}
		var:557.owner = { country_event = sharedafrika.132 } #neighbour in Mauretania offered to take
	}
	if = {
		limit = { owns_state = 290 } #The Rif
		var:461.owner = { country_event = sharedafrika.132 } #neighbour offered to take, but only if SFA owns
	}
	if = {
		limit = { owns_state = 297 } #Equatorial Guinea
		var:927.owner = { country_event = sharedafrika.132 } #neighbour offered to take
	}
	if = {
		limit = { owns_state = 1335 } #Santa Isabel
		var:927.owner = { country_event = sharedafrika.132 } #neighbour offered to take
	}
	#Anything left, give it to a random neighbor
	every_owned_state = {
		limit = {
			any_neighbor_state = {
				NOT = {
					is_owned_by = SFA
					}
				}
			}
		save_event_target_as = giveaway
		random_neighbor_state = {
			limit = {
				NOT = {
					is_owned_by = SFA
					}
				}
			owner = {
				transfer_state = event_target:giveaway
			}
		}
	}
	every_owned_state = {
		limit = { 
			any_neighbor_state = {
				NOT = { is_owned_by = SFA }
				}
			}
		save_event_target_as = giveaway
		random_neighbor_state = {
			limit = {
				NOT = {
					is_owned_by = SFA
					}
				}
			owner = {
				transfer_state = event_target:giveaway
			}
		}
	}
	#Still stuff left? Give it to SPR if they exist
	if = {
		limit = {
			country_exists = SFA
			country_exists = SPR
		}
		every_owned_state = {
			SPR = {
				transfer_state = PREV
			}
		}
	}
	every_state = {
		limit = { is_core_of = SFA }
		if = {
			limit = {
				country_exists = SPR
				}
		add_claim_by = SPR
		}
		remove_core_of = SFA
	}
	#or if not, to Carlists if they exist
	if = {
		limit = { 
			country_exists = SFA
			country_exists = CAR
			NOT = {
				country_exists = SPR
			}
		}
		every_owned_state = {
			CAR = {
				transfer_state = PREV
			}
		}
	}
	every_state = {
		limit = { is_core_of = SFA }
		if = {
			limit = {
				country_exists = CAR
				}
		add_claim_by = CAR
		}
		remove_core_of = SFA
	}
	#or if not, to CNT if they exist
	if = {
		limit = { 
			country_exists = SFA
			country_exists = CNT
			NOT = {
				country_exists = SPR
				country_exists = CAR
			}
		}
		every_owned_state = {
			CNT = {
				transfer_state = PREV
			}
		}
	}
	every_state = {
		limit = { is_core_of = SFA }
		if = {
			limit = {
				country_exists = CNT
				}
		add_claim_by = CNT
		}
		remove_core_of = SFA
	}
	###
	every_state = {
		limit = { is_claimed_by = SFA }
		remove_claim_by = SFA
	}
	every_country = {
		limit = { has_country_flag = SFA_already_exists }
		clr_country_flag = SFA_already_exists
	}
	add_named_threat = {
		threat = 1
		name = Spanish_African_collapse
	}
}

