﻿namespace = asp_mem_compat

# Fresh-game bootstrap: wait one day so MEM can establish mem_more_events_mod.
country_event = {
	id = asp_mem_compat.1
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		is_ai = no
		is_country_type = default
		NOT = { has_country_flag = asp_mem_compat_notice_acknowledged }
	}
	immediate = {
		country_event = { id = asp_mem_compat.2 days = 1 }
		country_event = { id = asp_mem_compat.4 days = 1 }
	}
}

# Correct-order path. Compatibility is applied in immediate; the popup is informational.
country_event = {
	id = asp_mem_compat.2
	title = asp_mem_compat.2.name
	desc = asp_mem_compat.2.desc
	picture = GFX_evt_diplomatic_visit
	is_triggered_only = yes
	trigger = {
		is_ai = no
		is_country_type = default
		has_global_flag = mem_more_events_mod
		# Exact MEM 2.20.0 defines this probe as yes but does not consume it.
		# ASP owns it as no only when ASP is effectively later in the requested MEM -> ASP order.
		NOT = { mem_asp_mod_active = yes }
		NOT = { has_country_flag = asp_mem_compat_notice_acknowledged }
	}
	immediate = {
		set_global_flag = asp_mem_compat_mem_detected
		asp_mem_220_apply_compatibility = yes
	}
	option = {
		name = asp_mem_compat.2.a
		set_country_flag = asp_mem_compat_notice_acknowledged
	}
}

# Single-player save-load bridge. Apply guards/migration immediately, then show the
# appropriate one-time informational/wrong-order popup.
event = {
	id = asp_mem_compat.3
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_global_flag = mem_more_events_mod }
	immediate = {
		every_country = {
			limit = {
				is_ai = no
				is_country_type = default
			}
			asp_mem_220_apply_compatibility = yes
			if = {
				limit = { NOT = { has_country_flag = asp_mem_compat_notice_acknowledged } }
				country_event = { id = asp_mem_compat.2 days = 1 }
			}
			if = {
				limit = { NOT = { has_country_flag = asp_mem_compat_load_order_warning_acknowledged } }
				country_event = { id = asp_mem_compat.4 days = 1 }
			}
		}
	}
}

# Wrong-order warning. Runtime guards/migration are still applied, but aliases that
# participate in galaxy generation or shared identity ownership cannot be certified
# when MEM is effectively later than ASP.
country_event = {
	id = asp_mem_compat.4
	title = asp_mem_compat.4.name
	desc = asp_mem_compat.4.desc
	picture = GFX_evt_diplomatic_visit
	is_triggered_only = yes
	trigger = {
		is_ai = no
		is_country_type = default
		has_global_flag = mem_more_events_mod
		mem_asp_mod_active = yes
		NOT = { has_country_flag = asp_mem_compat_load_order_warning_acknowledged }
	}
	immediate = {
		set_global_flag = asp_mem_compat_mem_detected
		asp_mem_220_apply_compatibility = yes
	}
	option = {
		name = asp_mem_compat.4.a
		set_country_flag = asp_mem_compat_load_order_warning_acknowledged
	}
}

# Inert target for compatibility aliases that must never start a duplicate MEM chain.
carrier_event = {
	id = asp_mem_compat.90
	hide_window = yes
	is_triggered_only = yes
	trigger = { always = no }
}
