﻿designate_diarch_or_secretariat_effect = {
	if = {
		limit = {
			has_diarchy_type = grand_secretariat
		}
		if = {
			limit = {
				$ACTOR$ = {
					has_title = title:e_minister_grand_marshal
				}
			}
			add_realm_law = grand_marshal_law
		}
		else_if = {
			limit = {
				$ACTOR$ = {
					has_title = title:e_minister_chancellor
				}
			}
			add_realm_law = grand_chancellor_law
		}
		else_if = {
			limit = {
				$ACTOR$ = {
					has_title = title:e_minister_censor
				}
			}
			add_realm_law = grand_censor_law
		}
		$ACTOR$ = {
			set_variable = {
				name = asked_to_be_secretary_director
				value = yes
			}
		}
	}
	else = {
		scope:crisis_heir = {
			if = {
				limit = {
					any_parent = {
						is_alive = yes
					}
				}
				random_parent = {
					limit = {
						is_alive = yes
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_close_family_member = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
				}
				random_close_family_member = {
					limit = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_extended_family_member = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
				}
				random_extended_family_member = {
					limit = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_close_family_member = {
						is_alive = yes
					}
				}
				random_close_family_member = {
					limit = {
						is_alive = yes
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_extended_family_member = {
						is_alive = yes
					}
				}
				random_extended_family_member = {
					limit = {
						is_alive = yes
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_vassal = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
				}
				random_vassal = {
					limit = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 30
						}
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
			else_if = {
				limit = {
					any_courtier = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 0
						}
					}
				}
				random_courtier = {
					limit = {
						is_alive = yes
						opinion = {
							target = scope:crisis_heir
							value > 0
						}
					}
					save_scope_as = chosen_crisis_diarch
				}
			}
		}
		if = {
			limit = {
				exists = scope:chosen_crisis_diarch
			}
			designate_diarch = scope:chosen_crisis_diarch
		}
	}
}