local OFGuns93DisplayCategoryPatch = {
    patched = false,
    tickHookAdded = false,
    repatchQueued = false,
}

local BC_FIREARM_CATEGORY = "WepFire"
local BC_MAGAZINE_CATEGORY = "WepAmmoMag"

local GUNS93_SENTINEL_FIREARM = "Base.83Cheetah"
local GUNS93_SENTINEL_MAGAZINE = "Base.83Mag"
local GAEL_GUNSTORE_SENTINEL_FIREARM = "Base.A91"
local GAEL_GUNSTORE_SENTINEL_MAGAZINE = "Base.545x39Clip30"

local function resolveWeaponCategories()
    return BC_FIREARM_CATEGORY, BC_MAGAZINE_CATEGORY
end

local function isSortingToggleEnabled()
    local vars = SandboxVars and SandboxVars.OpinionatedFirearms
    if vars and vars.AddSorting ~= nil then
        return vars.AddSorting == true
    end

    return true
end

local GUNS93_FIREARMS = {
    ["Base.83Cheetah"] = true,
    ["Base.AKM"] = true,
    ["Base.AKSport"] = true,
    ["Base.Anaconda"] = true,
    ["Base.AR15"] = true,
    ["Base.AR180"] = true,
    ["Base.AssaultRifle"] = true,
    ["Base.AssaultRifle2"] = true,
    ["Base.Auto5"] = true,
    ["Base.Auto5_Alt"] = true,
    ["Base.BenelliM3"] = true,
    ["Base.BenelliM3_Alt"] = true,
    ["Base.Beretta682"] = true,
    ["Base.Beretta682_Alt"] = true,
    ["Base.Beretta92FS"] = true,
    ["Base.BHP"] = true,
    ["Base.Bobcat"] = true,
    ["Base.Brown3006BAR"] = true,
    ["Base.Brown308BAR"] = true,
    ["Base.BrownCitori"] = true,
    ["Base.BrownCitori_Alt"] = true,
    ["Base.Buckmark"] = true,
    ["Base.CalicoPistol"] = true,
    ["Base.CalicoRifle"] = true,
    ["Base.CAR15"] = true,
    ["Base.Colt9mm"] = true,
    ["Base.ColtArmy"] = true,
    ["Base.ColtDet"] = true,
    ["Base.Command1911"] = true,
    ["Base.CZ75B"] = true,
    ["Base.DE357"] = true,
    ["Base.DeltaElite"] = true,
    ["Base.Dissy"] = true,
    ["Base.DoubleBarrelShotgun"] = true,
    ["Base.DoubleBarrelShotgun_Alt"] = true,
    ["Base.DoubleBarrelShotgunSawnoff"] = true,
    ["Base.DoubleBarrelShotgunSawnoff_Alt"] = true,
    ["Base.FAL"] = true,
    ["Base.Glock17"] = true,
    ["Base.Glock17L"] = true,
    ["Base.Glock19"] = true,
    ["Base.Glock20"] = true,
    ["Base.Glock21"] = true,
    ["Base.Glock22"] = true,
    ["Base.Glock23"] = true,
    ["Base.Glock24"] = true,
    ["Base.Gov1911"] = true,
    ["Base.GP100"] = true,
    ["Base.GP100_Alt"] = true,
    ["Base.GP101"] = true,
    ["Base.GP101_Alt"] = true,
    ["Base.Hammerless"] = true,
    ["Base.HBAR"] = true,
    ["Base.HK91"] = true,
    ["Base.HuntingRifle"] = true,
    ["Base.Ithaca37"] = true,
    ["Base.Ithaca37_Alt"] = true,
    ["Base.Ithaca37Riot"] = true,
    ["Base.Ithaca37Riot_Alt"] = true,
    ["Base.J22"] = true,
    ["Base.Javelina"] = true,
    ["Base.L395K"] = true,
    ["Base.L395K_Alt"] = true,
    ["Base.Longslide"] = true,
    ["Base.M1903"] = true,
    ["Base.M1917"] = true,
    ["Base.M1A"] = true,
    ["Base.M1ABush"] = true,
    ["Base.M1Carbine"] = true,
    ["Base.M1Garand"] = true,
    ["Base.M24"] = true,
    ["Base.M249"] = true,
    ["Base.M3GreaseGun"] = true,
    ["Base.M590"] = true,
    ["Base.M590_Alt"] = true,
    ["Base.M60"] = true,
    ["Base.M635"] = true,
    ["Base.M723"] = true,
    ["Base.M727"] = true,
    ["Base.M733"] = true,
    ["Base.M870"] = true,
    ["Base.M870_Alt"] = true,
    ["Base.MAC10"] = true,
    ["Base.Marlin3363030"] = true,
    ["Base.Marlin33644"] = true,
    ["Base.Marlin39A"] = true,
    ["Base.Marlin45"] = true,
    ["Base.Marlin60"] = true,
    ["Base.Marlin9"] = true,
    ["Base.Mauser98K"] = true,
    ["Base.Mini14"] = true,
    ["Base.Mini30"] = true,
    ["Base.Moss500"] = true,
    ["Base.Moss500_Alt"] = true,
    ["Base.Moss590"] = true,
    ["Base.Moss590_Alt"] = true,
    ["Base.MP5"] = true,
    ["Base.Officer1911"] = true,
    ["Base.P226"] = true,
    ["Base.P228"] = true,
    ["Base.P380"] = true,
    ["Base.P7M13"] = true,
    ["Base.P7M8"] = true,
    ["Base.Pistol"] = true,
    ["Base.Pistol2"] = true,
    ["Base.Pistol3"] = true,
    ["Base.PPK"] = true,
    ["Base.Python"] = true,
    ["Base.Python_Alt"] = true,
    ["Base.R223Rem788"] = true,
    ["Base.R223RugM77"] = true,
    ["Base.R3006Rem700"] = true,
    ["Base.R3006RugM77"] = true,
    ["Base.R3030Rem788"] = true,
    ["Base.R308Rem700"] = true,
    ["Base.R308RugM77"] = true,
    ["Base.R44Rem788"] = true,
    ["Base.Raven25"] = true,
    ["Base.Rem1100"] = true,
    ["Base.Rem1100_Alt"] = true,
    ["Base.Rem1187P"] = true,
    ["Base.Rem1187P_Alt"] = true,
    ["Base.Rem223760"] = true,
    ["Base.Rem3006760"] = true,
    ["Base.Rem308760"] = true,
    ["Base.Rem870P"] = true,
    ["Base.Rem870P_Alt"] = true,
    ["Base.Revolver"] = true,
    ["Base.Revolver_Long"] = true,
    ["Base.Revolver_Short"] = true,
    ["Base.Rug1022"] = true,
    ["Base.Rugmark"] = true,
    ["Base.RugP89"] = true,
    ["Base.RugP90"] = true,
    ["Base.SavageFox"] = true,
    ["Base.SavageFox_Alt"] = true,
    ["Base.SecuritySix"] = true,
    ["Base.SecuritySix_Alt"] = true,
    ["Base.Shotgun"] = true,
    ["Base.Shotgun_Alt"] = true,
    ["Base.ShotgunSawnoff"] = true,
    ["Base.ShotgunSawnoff_Alt"] = true,
    ["Base.SKS"] = true,
    ["Base.SOAuto5"] = true,
    ["Base.SOAuto5_Alt"] = true,
    ["Base.SOBeretta682"] = true,
    ["Base.SOBeretta682_Alt"] = true,
    ["Base.SOBrownCitori"] = true,
    ["Base.SOBrownCitori_Alt"] = true,
    ["Base.SOIthaca37"] = true,
    ["Base.SOIthaca37_Alt"] = true,
    ["Base.SOL395K"] = true,
    ["Base.SOL395K_Alt"] = true,
    ["Base.SOM24"] = true,
    ["Base.SOMoss500"] = true,
    ["Base.SOMoss500_Alt"] = true,
    ["Base.SOR223Rem700"] = true,
    ["Base.SOR223Rem788"] = true,
    ["Base.SOR3006Rem700"] = true,
    ["Base.SOR3030Rem788"] = true,
    ["Base.SOR308Rem700"] = true,
    ["Base.SOR308Rem788"] = true,
    ["Base.SOR44Rem788"] = true,
    ["Base.SORem1100"] = true,
    ["Base.SORem1100_Alt"] = true,
    ["Base.SOSavageFox"] = true,
    ["Base.SOSavageFox_Alt"] = true,
    ["Base.SOW223WinM70"] = true,
    ["Base.SOW3006WinM70"] = true,
    ["Base.SOW308WinM70"] = true,
    ["Base.SOWin1200"] = true,
    ["Base.SOWin1200_Alt"] = true,
    ["Base.SOWin1400"] = true,
    ["Base.SOWin1400_Alt"] = true,
    ["Base.SOWin1912"] = true,
    ["Base.SOWin1912_Alt"] = true,
    ["Base.SOWin37"] = true,
    ["Base.SOWin37_Alt"] = true,
    ["Base.SPAS12"] = true,
    ["Base.SPAS12_Alt"] = true,
    ["Base.SW10"] = true,
    ["Base.SW17"] = true,
    ["Base.SW4006"] = true,
    ["Base.SW4506"] = true,
    ["Base.SW586"] = true,
    ["Base.SW586_Alt"] = true,
    ["Base.SW5906"] = true,
    ["Base.SW65"] = true,
    ["Base.SW65_Alt"] = true,
    ["Base.T56"] = true,
    ["Base.TEC9"] = true,
    ["Base.USP40"] = true,
    ["Base.USP9"] = true,
    ["Base.Uzi"] = true,
    ["Base.Valmet"] = true,
    ["Base.VarmintRifle"] = true,
    ["Base.W223WinM70"] = true,
    ["Base.W3006WinM70"] = true,
    ["Base.W308WinM70"] = true,
    ["Base.Win1200"] = true,
    ["Base.Win1200_Alt"] = true,
    ["Base.Win1200Def"] = true,
    ["Base.Win1200Def_Alt"] = true,
    ["Base.Win1400"] = true,
    ["Base.Win1400_Alt"] = true,
    ["Base.Win1912"] = true,
    ["Base.Win1912_Alt"] = true,
    ["Base.Win30067400"] = true,
    ["Base.Win3087400"] = true,
    ["Base.Win37"] = true,
    ["Base.Win37_Alt"] = true,
    ["Base.Win61"] = true,
    ["Base.Win69"] = true,
    ["Base.Win943030"] = true,
    ["Base.Win94357"] = true,
    ["Base.Win94357_Alt"] = true,
    ["Base.Win9445"] = true,
}

local GUNS93_MAGAZINES = {
    ["Base.100CalicoMag"] = true,
    ["Base.1022Mag"] = true,
    ["Base.10M14Mag"] = true,
    ["Base.15BHPMag"] = true,
    ["Base.17BHPMag"] = true,
    ["Base.1908Mag"] = true,
    ["Base.1911MagExtend"] = true,
    ["Base.1911MagExtendSS"] = true,
    ["Base.1911MagSS"] = true,
    ["Base.20M16AR180Mag"] = true,
    ["Base.20M16Mag"] = true,
    ["Base.20M9Mag"] = true,
    ["Base.20MP5Mag"] = true,
    ["Base.223Rem760Mag"] = true,
    ["Base.223RemMag"] = true,
    ["Base.25Rug1022Mag"] = true,
    ["Base.25UziColtSMGMag"] = true,
    ["Base.25UziMag"] = true,
    ["Base.3006BARMag"] = true,
    ["Base.3006clip"] = true,
    ["Base.3006Rem760Mag"] = true,
    ["Base.3006WinMag"] = true,
    ["Base.3030RemMag"] = true,
    ["Base.308BARMag"] = true,
    ["Base.308Clip"] = true,
    ["Base.308Rem760Mag"] = true,
    ["Base.308WinMag"] = true,
    ["Base.30M14Mag"] = true,
    ["Base.30M16AR180Mag"] = true,
    ["Base.30M1CarMag"] = true,
    ["Base.30Mini14Mag"] = true,
    ["Base.4006Mag"] = true,
    ["Base.40AKMag"] = true,
    ["Base.40AR180Mag"] = true,
    ["Base.40M16AR180Mag"] = true,
    ["Base.44Clip"] = true,
    ["Base.44RemMag"] = true,
    ["Base.4506Mag"] = true,
    ["Base.45Clip"] = true,
    ["Base.45Moonclip"] = true,
    ["Base.556Clip"] = true,
    ["Base.5906Mag"] = true,
    ["Base.83Mag"] = true,
    ["Base.9mmClip"] = true,
    ["Base.AK5Mag"] = true,
    ["Base.AKBakeMag"] = true,
    ["Base.AKDrumMag"] = true,
    ["Base.AKMag"] = true,
    ["Base.AR180Mag"] = true,
    ["Base.BetaCMag"] = true,
    ["Base.BHPMag"] = true,
    ["Base.BHPMagSS"] = true,
    ["Base.BobcatMag"] = true,
    ["Base.BuckMag"] = true,
    ["Base.CalicoMag"] = true,
    ["Base.ColtSMGMag"] = true,
    ["Base.CZ75BMag"] = true,
    ["Base.DE357Mag"] = true,
    ["Base.DeltaEliteMag"] = true,
    ["Base.DeltaEliteMagBlue"] = true,
    ["Base.DeltaEliteMagExtend"] = true,
    ["Base.DeltaEliteMagExtendBlue"] = true,
    ["Base.EblocClip"] = true,
    ["Base.FALMag"] = true,
    ["Base.G17ExtMag"] = true,
    ["Base.G17Mag"] = true,
    ["Base.G17StickMag"] = true,
    ["Base.G19ExtMag"] = true,
    ["Base.G19Mag"] = true,
    ["Base.G20Mag"] = true,
    ["Base.G21Mag"] = true,
    ["Base.G22Mag"] = true,
    ["Base.G23Mag"] = true,
    ["Base.HK91Mag"] = true,
    ["Base.HKS10A357"] = true,
    ["Base.HKS10A38"] = true,
    ["Base.HKS22K"] = true,
    ["Base.HKS29M44"] = true,
    ["Base.HKS36A357"] = true,
    ["Base.HKS36A38"] = true,
    ["Base.HKS586A357"] = true,
    ["Base.HKS586A38"] = true,
    ["Base.HKSDSA38"] = true,
    ["Base.HKSMK3A357"] = true,
    ["Base.HKSMK3A38"] = true,
    ["Base.HKSPYA357"] = true,
    ["Base.HKSPYA38"] = true,
    ["Base.J22Mag"] = true,
    ["Base.L395KMag"] = true,
    ["Base.L395KMagSlugs"] = true,
    ["Base.M14Clip"] = true,
    ["Base.M1CarMag"] = true,
    ["Base.M249Box"] = true,
    ["Base.M3GreaseMag"] = true,
    ["Base.M60Box"] = true,
    ["Base.MAC10Mag"] = true,
    ["Base.MACGreaseMag"] = true,
    ["Base.MarkMag"] = true,
    ["Base.Mauserclip"] = true,
    ["Base.Mini14Mag"] = true,
    ["Base.Mini30Mag"] = true,
    ["Base.MP5Mag"] = true,
    ["Base.OfficerMag"] = true,
    ["Base.P226Mag"] = true,
    ["Base.P228Mag"] = true,
    ["Base.P380Mag"] = true,
    ["Base.P7M13Mag"] = true,
    ["Base.P7M8Mag"] = true,
    ["Base.P89Mag"] = true,
    ["Base.P90Mag"] = true,
    ["Base.PPKMag"] = true,
    ["Base.R25Mag"] = true,
    ["Base.Size3CompII357"] = true,
    ["Base.Size3CompII38"] = true,
    ["Base.Size3CompIII357"] = true,
    ["Base.Size3CompIII38"] = true,
    ["Base.Size4CompII357"] = true,
    ["Base.Size4CompII38"] = true,
    ["Base.Size4CompIII357"] = true,
    ["Base.Size4CompIII38"] = true,
    ["Base.Size5CompII357"] = true,
    ["Base.Size5CompII38"] = true,
    ["Base.Size5CompIII357"] = true,
    ["Base.Size5CompIII38"] = true,
    ["Base.Size6CompII357"] = true,
    ["Base.Size6CompII38"] = true,
    ["Base.Size6CompIII357"] = true,
    ["Base.Size6CompIII38"] = true,
    ["Base.SKSclip"] = true,
    ["Base.SpeedStrip357"] = true,
    ["Base.SpeedStrip38"] = true,
    ["Base.SpeedStrip44"] = true,
    ["Base.TEC9Mag"] = true,
    ["Base.TherARMag"] = true,
    ["Base.TherMini14Mag"] = true,
    ["Base.USP40Mag"] = true,
    ["Base.USP9Mag"] = true,
    ["Base.UziColtSMGMag"] = true,
    ["Base.UziMag"] = true,
    ["Base.ValmetMag"] = true,
    ["Base.Win69AMag"] = true,
}

local GAEL_GUNSTORE_FIREARMS = {
    ["Base.A2000"] = true,
    ["Base.A91"] = true,
    ["Base.AA12"] = true,
    ["Base.ACE21"] = true,
    ["Base.ACE23"] = true,
    ["Base.ACE52_CQB"] = true,
    ["Base.ACE53"] = true,
    ["Base.ACR"] = true,
    ["Base.ADS"] = true,
    ["Base.AEK"] = true,
    ["Base.AEK919"] = true,
    ["Base.AK_minidrako"] = true,
    ["Base.AK101"] = true,
    ["Base.AK103"] = true,
    ["Base.AK12"] = true,
    ["Base.AK19"] = true,
    ["Base.AK47"] = true,
    ["Base.AK5C"] = true,
    ["Base.AK74"] = true,
    ["Base.AK74u"] = true,
    ["Base.AK74u_long"] = true,
    ["Base.AK9"] = true,
    ["Base.AKM"] = true,
    ["Base.AKU12"] = true,
    ["Base.AMD65"] = true,
    ["Base.AN94"] = true,
    ["Base.Anaconda"] = true,
    ["Base.APC9K"] = true,
    ["Base.AR10"] = true,
    ["Base.AR15"] = true,
    ["Base.AR160"] = true,
    ["Base.AR6951"] = true,
    ["Base.ASH_12"] = true,
    ["Base.AssaultRifle"] = true,
    ["Base.AssaultRifle2"] = true,
    ["Base.AUG_9mm"] = true,
    ["Base.AUG_A1"] = true,
    ["Base.AUG_A2"] = true,
    ["Base.Automag357"] = true,
    ["Base.Automag44"] = true,
    ["Base.Automag50AE"] = true,
    ["Base.AWS"] = true,
    ["Base.BAR"] = true,
    ["Base.Becker_Shotgun"] = true,
    ["Base.Becker_Shotgun_Short"] = true,
    ["Base.BenelliM4"] = true,
    ["Base.Beretta_A400"] = true,
    ["Base.Beretta_A400_Short"] = true,
    ["Base.Beretta_PX4"] = true,
    ["Base.Browning_Auto"] = true,
    ["Base.Browning_Auto_Short"] = true,
    ["Base.BrowningHP"] = true,
    ["Base.Carcano"] = true,
    ["Base.CBJ"] = true,
    ["Base.CETME"] = true,
    ["Base.CircuitJudgeRifle"] = true,
    ["Base.Colt9mm"] = true,
    ["Base.ColtNavy1851"] = true,
    ["Base.ColtNavyExorcist"] = true,
    ["Base.ColtPeacemaker1873"] = true,
    ["Base.Coonan357"] = true,
    ["Base.CS5"] = true,
    ["Base.CZ75"] = true,
    ["Base.CZ805"] = true,
    ["Base.CZScorpion"] = true,
    ["Base.DB_Condor"] = true,
    ["Base.DB_Condor_sawn"] = true,
    ["Base.DDM4"] = true,
    ["Base.Deagle357_gold"] = true,
    ["Base.Deagle50AE"] = true,
    ["Base.DeagleCar14"] = true,
    ["Base.DeLisle"] = true,
    ["Base.DoubleBarrelShotgun"] = true,
    ["Base.DoubleBarrelShotgunSawnoff"] = true,
    ["Base.DVB15"] = true,
    ["Base.ENARM_Pentagun"] = true,
    ["Base.Enfield"] = true,
    ["Base.FAL"] = true,
    ["Base.FAL_CQB"] = true,
    ["Base.FAMAS"] = true,
    ["Base.FiveSeven"] = true,
    ["Base.FN2000"] = true,
    ["Base.FN502_22LR"] = true,
    ["Base.FNX45"] = true,
    ["Base.G17"] = true,
    ["Base.G18"] = true,
    ["Base.G2"] = true,
    ["Base.G27"] = true,
    ["Base.G36C"] = true,
    ["Base.G3A3"] = true,
    ["Base.G43"] = true,
    ["Base.Galil"] = true,
    ["Base.Glock_tactical"] = true,
    ["Base.Glock23"] = true,
    ["Base.Glock43"] = true,
    ["Base.GOL"] = true,
    ["Base.Grizzly50AE"] = true,
    ["Base.Groza"] = true,
    ["Base.GSH18"] = true,
    ["Base.HK_121"] = true,
    ["Base.HK416"] = true,
    ["Base.HKG28"] = true,
    ["Base.HKMK23"] = true,
    ["Base.HoneyBadger"] = true,
    ["Base.HuntingRifle"] = true,
    ["Base.IA2"] = true,
    ["Base.IA2_308"] = true,
    ["Base.Jackhammer"] = true,
    ["Base.Jericho941"] = true,
    ["Base.JNG90"] = true,
    ["Base.K2"] = true,
    ["Base.K7"] = true,
    ["Base.KAC_PDW"] = true,
    ["Base.Kark98"] = true,
    ["Base.Kimber1911"] = true,
    ["Base.Kriss9mm"] = true,
    ["Base.KrissVector45"] = true,
    ["Base.KS23"] = true,
    ["Base.KSG"] = true,
    ["Base.L115A"] = true,
    ["Base.L85"] = true,
    ["Base.L86"] = true,
    ["Base.L96"] = true,
    ["Base.LanchesterMK1"] = true,
    ["Base.Lewis"] = true,
    ["Base.LR300"] = true,
    ["Base.LSAT"] = true,
    ["Base.LVOA"] = true,
    ["Base.M1"] = true,
    ["Base.M110"] = true,
    ["Base.M16A2"] = true,
    ["Base.M1887"] = true,
    ["Base.M1887_Short"] = true,
    ["Base.M1A1"] = true,
    ["Base.M200"] = true,
    ["Base.M21"] = true,
    ["Base.M24"] = true,
    ["Base.M240B"] = true,
    ["Base.M249"] = true,
    ["Base.M39"] = true,
    ["Base.M4"] = true,
    ["Base.M40"] = true,
    ["Base.M60E4"] = true,
    ["Base.M620"] = true,
    ["Base.M82A3"] = true,
    ["Base.M9_Samurai"] = true,
    ["Base.M93R"] = true,
    ["Base.M98B"] = true,
    ["Base.MAB38A"] = true,
    ["Base.MAC10"] = true,
    ["Base.MAS36"] = true,
    ["Base.MAT49"] = true,
    ["Base.MatebaGrifone"] = true,
    ["Base.MG131"] = true,
    ["Base.MG4"] = true,
    ["Base.MG42"] = true,
    ["Base.MG710"] = true,
    ["Base.Micro_UZI"] = true,
    ["Base.Mini_14"] = true,
    ["Base.Minimi"] = true,
    ["Base.MK18"] = true,
    ["Base.Mosin"] = true,
    ["Base.Mossber500"] = true,
    ["Base.Mossber590"] = true,
    ["Base.MP_R8"] = true,
    ["Base.MP18"] = true,
    ["Base.MP1911"] = true,
    ["Base.MP40"] = true,
    ["Base.MP5"] = true,
    ["Base.MP5K"] = true,
    ["Base.MP5SD"] = true,
    ["Base.MP7"] = true,
    ["Base.MP9"] = true,
    ["Base.MPX"] = true,
    ["Base.MSST"] = true,
    ["Base.MTAR"] = true,
    ["Base.MTS_255"] = true,
    ["Base.MTS_255_Short"] = true,
    ["Base.MX4"] = true,
    ["Base.Nagant_M1895"] = true,
    ["Base.Negev"] = true,
    ["Base.OTS_33"] = true,
    ["Base.P220"] = true,
    ["Base.P220_Elite"] = true,
    ["Base.P228"] = true,
    ["Base.P38"] = true,
    ["Base.P90"] = true,
    ["Base.P99"] = true,
    ["Base.P99_Kilin"] = true,
    ["Base.PB6P9"] = true,
    ["Base.Pistol"] = true,
    ["Base.pistol_shotgun"] = true,
    ["Base.Pistol2"] = true,
    ["Base.Pistol3"] = true,
    ["Base.PKP"] = true,
    ["Base.PP_Bizon"] = true,
    ["Base.PP2000"] = true,
    ["Base.PP93"] = true,
    ["Base.PPSH41"] = true,
    ["Base.Python357"] = true,
    ["Base.QBA"] = true,
    ["Base.QBB95"] = true,
    ["Base.QBS09"] = true,
    ["Base.QBS09_Short"] = true,
    ["Base.QBZ951"] = true,
    ["Base.R5"] = true,
    ["Base.Remington1100"] = true,
    ["Base.Remington1100_Short"] = true,
    ["Base.Remington121"] = true,
    ["Base.Remington870"] = true,
    ["Base.Remington870_Short"] = true,
    ["Base.Revolver"] = true,
    ["Base.Revolver_long"] = true,
    ["Base.Revolver_short"] = true,
    ["Base.Revolver38"] = true,
    ["Base.Revolver666"] = true,
    ["Base.Rhino20DS"] = true,
    ["Base.RMB93"] = true,
    ["Base.RPD"] = true,
    ["Base.RPK"] = true,
    ["Base.RPK12"] = true,
    ["Base.RPK16"] = true,
    ["Base.RSH12"] = true,
    ["Base.Ruger10_22"] = true,
    ["Base.Ruger357"] = true,
    ["Base.RugerLC"] = true,
    ["Base.SA58"] = true,
    ["Base.Saiga12"] = true,
    ["Base.Saiga9mm"] = true,
    ["Base.Samurai_aw"] = true,
    ["Base.Samurai_kendo"] = true,
    ["Base.SAR21"] = true,
    ["Base.ScarH"] = true,
    ["Base.ScarL"] = true,
    ["Base.Schofield1875"] = true,
    ["Base.Scout_elite"] = true,
    ["Base.ScrapRevolver"] = true,
    ["Base.Shorty"] = true,
    ["Base.Shotgun"] = true,
    ["Base.ShotgunSawnoff"] = true,
    ["Base.SIG_553"] = true,
    ["Base.SIG516"] = true,
    ["Base.Silenced_Sten"] = true,
    ["Base.Sjorgen"] = true,
    ["Base.Sjorgen_Short"] = true,
    ["Base.SKS"] = true,
    ["Base.SKS_carbine"] = true,
    ["Base.SKS_carbine_short"] = true,
    ["Base.Snub22LR"] = true,
    ["Base.SPAS12"] = true,
    ["Base.SPAS15"] = true,
    ["Base.Springfield_sniper"] = true,
    ["Base.SR1M"] = true,
    ["Base.SR338"] = true,
    ["Base.SR3M"] = true,
    ["Base.SR47"] = true,
    ["Base.SS2V5"] = true,
    ["Base.Sten_MK5"] = true,
    ["Base.Striker"] = true,
    ["Base.SV98"] = true,
    ["Base.SVD"] = true,
    ["Base.SVD_short"] = true,
    ["Base.SVD12"] = true,
    ["Base.SVDK"] = true,
    ["Base.SVDK_short"] = true,
    ["Base.SVT_40"] = true,
    ["Base.SVU"] = true,
    ["Base.SW1905"] = true,
    ["Base.SW1917"] = true,
    ["Base.SW500"] = true,
    ["Base.SW629"] = true,
    ["Base.SWM1854"] = true,
    ["Base.SWM1894"] = true,
    ["Base.SWM3"] = true,
    ["Base.SWM327"] = true,
    ["Base.SWM629_Deluxe"] = true,
    ["Base.SWMP_12"] = true,
    ["Base.Taurus_raging_bull"] = true,
    ["Base.Taurus_raging_bull460"] = true,
    ["Base.Taurus_RT85"] = true,
    ["Base.Taurus606"] = true,
    ["Base.TEC9"] = true,
    ["Base.Thompson"] = true,
    ["Base.TMP"] = true,
    ["Base.Type81"] = true,
    ["Base.Type88"] = true,
    ["Base.UMP45"] = true,
    ["Base.UMP45_long"] = true,
    ["Base.USAS12"] = true,
    ["Base.USP45"] = true,
    ["Base.UZI"] = true,
    ["Base.V_M87"] = true,
    ["Base.ValmetM82"] = true,
    ["Base.VarmintRifle"] = true,
    ["Base.VEPR"] = true,
    ["Base.Veresk"] = true,
    ["Base.VictorySW22"] = true,
    ["Base.VP70"] = true,
    ["Base.VR80"] = true,
    ["Base.VSK"] = true,
    ["Base.VSS"] = true,
    ["Base.VSS_Tactical"] = true,
    ["Base.VSSK"] = true,
    ["Base.VZ58"] = true,
    ["Base.VZ61"] = true,
    ["Base.WA2000"] = true,
    ["Base.Webley_MK_snub"] = true,
    ["Base.Webley_Revolver"] = true,
    ["Base.Wieger940"] = true,
    ["Base.Wildey"] = true,
    ["Base.Winchester1886"] = true,
    ["Base.Winchester1895"] = true,
    ["Base.Winchester1897"] = true,
    ["Base.X86"] = true,
    ["Base.XD"] = true,
    ["Base.XM8"] = true,
}

local GAEL_GUNSTORE_MAGAZINES = {
    ["Base.12GClip"] = true,
    ["Base.12GClip14"] = true,
    ["Base.12GDrum24"] = true,
    ["Base.22LRClip"] = true,
    ["Base.22LRClip50"] = true,
    ["Base.22LRDrum100"] = true,
    ["Base.308Box150"] = true,
    ["Base.308Clip"] = true,
    ["Base.308Clip40"] = true,
    ["Base.308Drum100"] = true,
    ["Base.308Drum60"] = true,
    ["Base.357Clip"] = true,
    ["Base.357Drum45"] = true,
    ["Base.44Clip"] = true,
    ["Base.44Clip20"] = true,
    ["Base.44Drum50"] = true,
    ["Base.45Clip"] = true,
    ["Base.45Clip25"] = true,
    ["Base.45Drum100"] = true,
    ["Base.45Drum50"] = true,
    ["Base.50Clip"] = true,
    ["Base.50Clip18"] = true,
    ["Base.50MagnumClip"] = true,
    ["Base.50MagnumClip18"] = true,
    ["Base.50MagnumDrum40"] = true,
    ["Base.545x39Clip30"] = true,
    ["Base.545x39Clip60"] = true,
    ["Base.545x39Drum100"] = true,
    ["Base.556Box150"] = true,
    ["Base.556Clip"] = true,
    ["Base.556Drum_100rnd"] = true,
    ["Base.556Drum_60rnd"] = true,
    ["Base.762x39Clip"] = true,
    ["Base.762x39Clip45"] = true,
    ["Base.762x39Drum100"] = true,
    ["Base.762x39Drum73"] = true,
    ["Base.762x54rBox150"] = true,
    ["Base.762x54rClip"] = true,
    ["Base.762x54rClip40"] = true,
    ["Base.792x57Box75"] = true,
    ["Base.792x57Box97"] = true,
    ["Base.792x57Clip"] = true,
    ["Base.792x57Clip40"] = true,
    ["Base.9mmClip"] = true,
    ["Base.9mmClip30"] = true,
    ["Base.9mmDrum100"] = true,
    ["Base.9mmDrum50"] = true,
    ["Base.9mmDrum75"] = true,
    ["Base.9x39Clip"] = true,
    ["Base.9x39Clip40"] = true,
    ["Base.9x39Drum60"] = true,
    ["Base.BizonClip64"] = true,
}


local function setDisplayCategory(scriptManager, fullType, category)
    local scriptItem = scriptManager:FindItem(fullType)
    if not scriptItem then
        return false
    end

    if type(scriptItem.DoParam) == "function" then
        scriptItem:DoParam("DisplayCategory = " .. category)
        return true
    end

    if type(scriptItem.setDisplayCategory) == "function" then
        scriptItem:setDisplayCategory(category)
        return true
    end

    return false
end

local function areSupportedItemsReady(scriptManager)
    if not scriptManager then
        return false
    end

    local guns93Firearm = scriptManager:FindItem(GUNS93_SENTINEL_FIREARM)
    local guns93Magazine = scriptManager:FindItem(GUNS93_SENTINEL_MAGAZINE)
    local guns93Ready = guns93Firearm ~= nil and guns93Magazine ~= nil

    local gaelFirearm = scriptManager:FindItem(GAEL_GUNSTORE_SENTINEL_FIREARM)
    local gaelMagazine = scriptManager:FindItem(GAEL_GUNSTORE_SENTINEL_MAGAZINE)
    local gaelReady = gaelFirearm ~= nil and gaelMagazine ~= nil

    return guns93Ready or gaelReady
end

local function applyPatch()
    if OFGuns93DisplayCategoryPatch.patched then
        return true
    end

    if not isSortingToggleEnabled() then
        OFGuns93DisplayCategoryPatch.patched = true
        return true
    end

    local scriptManager = ScriptManager and ScriptManager.instance
    if not scriptManager or type(scriptManager.FindItem) ~= "function" then
        return false
    end

    local firearmCategory, magazineCategory = resolveWeaponCategories()

    if not areSupportedItemsReady(scriptManager) then
        return false
    end

    local patchedCount = 0
    for fullType in pairs(GUNS93_FIREARMS) do
        if setDisplayCategory(scriptManager, fullType, firearmCategory) then
            patchedCount = patchedCount + 1
        end
    end

    for fullType in pairs(GUNS93_MAGAZINES) do
        if setDisplayCategory(scriptManager, fullType, magazineCategory) then
            patchedCount = patchedCount + 1
        end
    end

    for fullType in pairs(GAEL_GUNSTORE_FIREARMS) do
        if setDisplayCategory(scriptManager, fullType, firearmCategory) then
            patchedCount = patchedCount + 1
        end
    end

    for fullType in pairs(GAEL_GUNSTORE_MAGAZINES) do
        if setDisplayCategory(scriptManager, fullType, magazineCategory) then
            patchedCount = patchedCount + 1
        end
    end

    if patchedCount == 0 then
        return false
    end

    OFGuns93DisplayCategoryPatch.patched = true
    return true
end

local function tryPatchOnTick()
    if applyPatch() then
        OFGuns93DisplayCategoryPatch.tickHookAdded = false
        Events.OnTick.Remove(tryPatchOnTick)
    end
end

local function ensureTickHook()
    if OFGuns93DisplayCategoryPatch.tickHookAdded then
        return
    end

    OFGuns93DisplayCategoryPatch.tickHookAdded = true
    Events.OnTick.Add(tryPatchOnTick)
end

local function queueRepatchNow()
    OFGuns93DisplayCategoryPatch.patched = false
    if not applyPatch() then
        ensureTickHook()
    end
end

local function runQueuedRepatch()
    OFGuns93DisplayCategoryPatch.repatchQueued = false
    Events.OnTick.Remove(runQueuedRepatch)
    queueRepatchNow()
end

local function queueRepatch()
    if OFGuns93DisplayCategoryPatch.repatchQueued then
        return
    end

    OFGuns93DisplayCategoryPatch.repatchQueued = true
    Events.OnTick.Add(runQueuedRepatch)
end

local function tryInstallBetterContainersRepatchHook()
    local ok, helpers = pcall(require, "BetterContainers/Helpers")
    if not ok or type(helpers) ~= "table" then
        return
    end

    local eventName = helpers.OPTIONS_APPLIED
    if type(eventName) ~= "string" then
        return
    end

    local event = Events[eventName]
    if event and type(event.Add) == "function" then
        event.Add(queueRepatch)
    end
end

if not applyPatch() then
    ensureTickHook()
end

if Events.OnMainMenuEnter and type(Events.OnMainMenuEnter.Add) == "function" then
    Events.OnMainMenuEnter.Add(queueRepatch)
end

tryInstallBetterContainersRepatchHook()

