class ERR_ItemPreferenceTest : ERR_ItemPreferences
{
 void ERR_ItemPreferenceTest(string path) { }
 void ForgetLoaded() { m_Loaded.Clear(); }
 bool Validate(array<string> records) { return Valid(records); }
}
modded class ArmaReforgerScripted
{
 protected bool m_ERRItemEditDone;
 protected int m_ERRItemEditFailures;
 protected int ItemBadge(ERR_ServerItemCatalog catalog, ERR_GameMode mode, string id)
 {
  array<string> values = {}; catalog.Snapshot(mode, values, id, 0);
  foreach (string row : values)
  {
   if (row.IndexOf("serverMod=" + id + "|") != 0) continue;
   array<string> parts = {}; row.Substring(10, row.Length() - 10).Split("|", parts, false);
   if (parts.Count() != 5) return -1;
   return ERR_SelectionStyle.Aggregate(parts[2].ToInt(), parts[3].ToInt(), parts[4] == "1");
  }
  return -1;
 }
 protected void ItemAssert(bool condition, string name)
 {
  if (!condition) m_ERRItemEditFailures++;
  PrintFormat("ERR ITEM EDIT TEST: pass=%1 %2", condition, name);
 }
 override void OnUpdate(BaseWorld world, float timeslice)
 {
  super.OnUpdate(world, timeslice);
  ERR_GameMode mode = ERR_GameMode.Get();
  if (!Replication.IsServer() || !mode || !mode.Active() || m_ERRItemEditDone) return;
  ERR_ServerItemCatalog catalog = ERR_ServerItems();
  array<string> values = {}; catalog.Snapshot(mode, values, "58d0fb3206b6f859", 0);
  if (!values.Contains("itemsScan=Catalog complete")) return;
  m_ERRItemEditDone = true;
  ERR_ServerAddonItems addon = catalog.Find("58d0fb3206b6f859");
  if (!addon || addon.Items.IsEmpty()) { ItemAssert(false, "native catalog ready"); return; }
  ResourceName prefab = addon.Items[0].Prefab;
  string id = ERR_TerrainCatalog.Key(prefab), base = addon.Id;
  ERR_ItemPreferences prefs = ERR_ItemSelections();
  int initial = ERR_AutomaticEquipmentCatalog.Count(mode);
  ItemAssert(initial > 0 && prefs.Allows(prefab), "default eligible pool and item allowed");
  ItemAssert(prefs.Set(base, "*", false) && !prefs.Allows(prefab), "bulk disable");
  ItemAssert(ItemBadge(catalog, mode, base) == 0, "mod status red after disable");
  int blocked = ERR_AutomaticEquipmentCatalog.Count(mode);
  ItemAssert(blocked < initial, "cached pool invalidated by selection");
  ItemAssert(prefs.Set(base, id, true) && prefs.Allows(prefab), "explicit enable overrides same-mod default");
  ItemAssert(ItemBadge(catalog, mode, base) == 2, "mod status mixed after individual override");
  ItemAssert(prefs.Preset("save", "custom_1"), "save named preset");
  ItemAssert(prefs.Set(base, "*", true) && prefs.Enabled(base, id), "enable all clears item exceptions");
  ItemAssert(ItemBadge(catalog, mode, base) == 1, "mod status green after enable");
  ItemAssert(ERR_AutomaticEquipmentCatalog.Count(mode) == initial, "enabled pool restored without reroll bias");
  ItemAssert(prefs.Preset("load", "custom_1") && !prefs.Enabled(base, "0000000000000001") && prefs.Enabled(base, id), "load preset restores baseline and override");
  ItemAssert(ItemBadge(catalog, mode, base) == 2, "mod status refreshes after preset load");
  ERR_ItemPreferences reread = new ERR_ItemPreferences();
  ItemAssert(!reread.Enabled(base, "0000000000000001") && reread.Enabled(base, id), "disk reload preserves working selection");
  ERR_ItemPreferenceTest missing = new ERR_ItemPreferenceTest();
  missing.ForgetLoaded();
  ItemAssert(missing.Allows(prefab), "missing mod rules do not apply even to shared resources");
  ItemAssert(prefs.Set("0000000000000001", "*", false) && prefs.Allows(prefab), "unloaded unrelated mod rule dormant");
  ItemAssert(!prefs.Preset("load", "absent") && !prefs.Preset("save", "../escape") && !prefs.Preset("save", "active"), "invalid or absent preset rejected");
  ItemAssert(ItemBadge(catalog, mode, base) == 2, "mod status mixed after individual override");
  ItemAssert(prefs.Preset("save", "custom_1") && prefs.Preset("delete", "custom_1") && !prefs.Preset("load", "custom_1"), "replace and delete preset");
  ERR_ItemPreferences failure = new ERR_ItemPreferences("$profile:nonexistent-folder/settings.json");
  int revision = failure.Revision();
  ItemAssert(!failure.Set(base, "*", false) && failure.Enabled(base, id) && failure.Revision() == revision, "failed save preserves state and revision");
  array<string> bad = {"active", "active|58d0fb3206b6f859|*|2"};
  ItemAssert(!missing.Validate(bad), "invalid stored value rejected");
  array<string> duplicate = {"active", "active|58d0fb3206b6f859|*|0", "active|58d0fb3206b6f859|*|1"};
  ItemAssert(!missing.Validate(duplicate), "duplicate stored rules rejected");
  ItemAssert(prefs.Set(base, "*", true), "restore test selection");
  PrintFormat("ERR ITEM EDIT COMPLETE failures=%1 initial=%2 blocked=%3", m_ERRItemEditFailures, initial, blocked);
 }
}
