# Forked Minds
# Scope: country. Clones the highest-level eligible leader of class $CLASS$.
# Eligible: apb_can_be_forked (common/scripted_triggers/apb_forked_minds_triggers.txt):
# not the ruler, not a forked instance, not a gestalt council node.
apb_forked_clone_best = {
	custom_tooltip = apb_forked_clone_$CLASS$_tt
	hidden_effect = {
		ordered_owned_leader = {
			limit = {
				leader_class = $CLASS$
				apb_can_be_forked = yes
			}
			position = 0
			order_by = trigger:has_base_skill
			save_event_target_as = apb_fork_source
		}
		if = {
			limit = { exists = event_target:apb_fork_source }
			clone_leader = {
				target = event_target:apb_fork_source
				effect = {
					save_event_target_as = apb_fork_clone
					add_trait = {
						trait = leader_trait_apb_forked_instance
						show_message = no
					}
				}
			}
			# The flag is set here, on the finished clone: a flag set inside
			# clone_leader's effect block did not persist in 4.6.
			# The trait is added again if it is somehow missing.
			if = {
				limit = { exists = event_target:apb_fork_clone }
				event_target:apb_fork_clone = {
					set_leader_flag = apb_forked_instance
					if = {
						limit = { NOT = { has_trait = leader_trait_apb_forked_instance } }
						add_trait = {
							trait = leader_trait_apb_forked_instance
							show_message = no
						}
					}
				}
			}
		}
	}
}
