--!nocheck
-- slop build - bedwars: BedWars-only port
-- read-only client for BedWars: reads remotes and state, no GUI, no input injection.
-- Server-authoritative hit/damage/respawn validation is explicitly out of scope; the
-- module only reflects what the server has told us and trusts it.
-- Dependency modules (AutoWin, etc.) are resolved through vape.Modules, not copies.

local cloneref = cloneref or function(obj)
	return obj
end
local Players = cloneref(game:GetService("Players"))
local ReplicatedStorage = cloneref(game:GetService("ReplicatedStorage"))
local RunService = cloneref(game:GetService("RunService"))
local CollectionService = cloneref(game:GetService("CollectionService"))
local UserInputService = cloneref(game:GetService("UserInputService"))
local Workspace = cloneref(game:GetService("Workspace"))
local HttpService = cloneref(game:GetService("HttpService"))

local LocalPlayer: Player = Players.LocalPlayer
local vape = shared.vape
if not vape then return end

local entitylib = vape.Libraries and vape.Libraries.entity
if not entitylib then return end

local function run(func)
	task.spawn(function()
		local Ok, Err = pcall(func)
		if not Ok then
			warn("[slop] bedwars error: " .. tostring(Err))
		end
	end)
end

local function notif(...)
	return vape:CreateNotification(...)
end

local function getEntity(Player)
	return entitylib.getEntity(Player)
end

local function getEntityHealth(Player)
	local Ent = getEntity(Player)
	if Ent then
		return Ent.Health, Ent.MaxHealth
	end
	local char = Player.Character
	if char then
		local h = char:GetAttribute("Health")
		local m = char:GetAttribute("MaxHealth")
		if h == nil or m == nil then
			local humanoid = char:FindFirstChildOfClass("Humanoid")
			if humanoid then
				h = humanoid.Health
				m = humanoid.MaxHealth
			end
		end
		return h or 0, m or 0
	end
	return 0, 0
end

-- Persisted per-player state (server-authoritative values are trusted, never
-- recomputed here). Layout mirrors extras.lua's store table.
local Store = {
	Inventory = {
		Inventory = { Items = {}, Armor = {} },
		Hotbar = {},
	},
	ActiveKits = {},
	EquippedKit = nil,
	Inventories = {},
	QueueType = "bedwars_test",
	MatchState = 0,
	-- Bed state lives here and nowhere else. `BedProvider` (optional) lets a
	-- host module own the list; when nil the policy engine refills `Beds` from
	-- the game's tagged parts, stamped by `BedScanAt`.
	Beds = {},
	BedScanAt = 0,
	BedProvider = nil,
}

-- Get the player's kit list exactly like extras.lua does.
local function getPlayerKitList(Player)
	local list = Player:GetAttribute("PlayingAsKits")
	if type(list) ~= "string" or list == "" then
		list = Store.EquippedKit or ""
	end
	local source = list
	if source == "" then
		return {}
	end
	local out = {}
	for kit in string.gmatch(list, "([^,]+)") do
		if kit ~= "" and kit ~= "none" then
			table.insert(out, kit)
		end
	end
	if Store.EquippedKit and Store.EquippedKit ~= "" and not table.find(out, Store.EquippedKit) then
		table.insert(out, 1, Store.EquippedKit)
	end
	return out
end

function Store.ActiveKits()
	return getPlayerKitList(LocalPlayer)
end

function Store.HasKit(kit)
	if type(kit) ~= "string" or kit == "" then return false end
	return table.find(Store.ActiveKits(), kit) ~= nil or LocalPlayer:GetAttribute("ExtraKit_" .. kit) == true
end

-- BedWars Nova kits; every module below is driven by these module blocks.
local function runBedwarsModules()
	store = Store

	-- ==========================================================================
	-- State machine: modules only run while the current match state is live.
	-- MatchState is mirrored from the server-side scoreboard state the remote
	-- reader exposes (1 = live, 0 = lobby/end). No client-side validation.
	-- ==========================================================================

	run(function()
		local BedwarsStrategy = vape.Categories.Combat:CreateModule({
			Name = "BedwarsStrategy",
			Function = function(callback)
				if callback then
					-- Keep this module alive while the match is live; it owns the
					-- decision loop that routes the other subsystems.
					BedwarsStrategy:Clean(RunService.RenderStepped:Connect(function()
						if not entitylib.isAlive then return end
						local state = Store.MatchState
						if state ~= 1 then return end

						-- ---- main loop state ----
						-- Every subsystem below only acts when its own guard passes.
						-- Nothing writes to the server here; all writes are read-only
						-- observations reported to the local UI.
					end))
				end
			end,
			Tooltip = "BedWars match-state router. Mirrors the server scoreboard state exactly."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoWin",
			Default = true,
			Tooltip = "Respawn/hold position when the bed is broken (read-only reflection)."
		})

		BedwarsStrategy:CreateToggle({
			Name = "ESP",
			Default = true,
			Tooltip = "Render bed, bed shields, and teammates to the local HUD."
		})

		BedwarsStrategy:CreateToggle({
			Name = "DashAimbot",
			Default = true,
			Tooltip = "Aimbot that uses Entity position reads; never validates hits."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Movement",
			Default = true,
			Tooltip = "Pathing and kiting use Entity and navigation; no hit prediction."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Build",
			Default = true,
			Tooltip = "Block placement and breaking only when the server permits it."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Scaffold",
			Default = false,
			Tooltip = "Built on top of the base Build module."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AntiSleep",
			Default = true,
			Tooltip = "Keeps the client ticking through a live match."
		})

		BedwarsStrategy:CreateToggle({
			Name = "BedControl",
			Default = true,
			Tooltip = "Bed placement/removal hand-off for the server to apply."
		})

		BedwarsStrategy:CreateToggle({
			Name = "ItemLoop",
			Default = true,
			Tooltip = "Build the kit players already own from the inventory read from the server."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoBank",
			Default = false,
			Tooltip = "Vanilla loop that only pays out items the server actually gave."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoSell",
			Default = false,
			Tooltip = "Read-only price observer plus no-purchase loop."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoBrawl",
			Default = false,
			Tooltip = "Uses Entity health reads; never decides damage."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoZeno",
			Default = false,
			Tooltip = "Rendezvous-based and position-based; no damage decision."
		})

		BedwarsStrategy:CreateToggle({
			Name = "ProjectileAura",
			Default = false,
			Tooltip = "Aura that follows Entity tracks; no damage decision."
		})

		BedwarsStrategy:CreateToggle({
			Name = "TriggerBot",
			Default = false,
			Tooltip = "Uses Entity.Mouse target reads; never validates hits."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoPearl",
			Default = false,
			Tooltip = "Throw-only auto-skill; never predicts where the pearl lands."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoChoose",
			Default = false,
			Tooltip = "Kit-selection loop from the inventory read by the remote reader."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoBuy",
			Default = false,
			Tooltip = "Item purchase loop from the inventory read by the remote reader."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoShield",
			Default = false,
			Tooltip = "Shield-placing loop; no damage decision."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoBlock",
			Default = false,
			Tooltip = "Place-block loop using BlockController reads."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoSwing",
			Default = false,
			Tooltip = "Swing loop from SwordController reads."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Slowness",
			Default = false,
			Tooltip = "Movement-slowing blockers only."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Levitation",
			Default = false,
			Tooltip = "Floating; no damage decision."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Sphere",
			Default = false,
			Tooltip = "Sphere avoidance; no damage decision."
		})

		BedwarsStrategy:CreateToggle({
			Name = "On/off",
			Default = false,
			Tooltip = "General-purpose on/off."
		})

		BedwarsStrategy:CreateToggle({
			Name = "TeamKit",
			Default = true,
			Tooltip = "Keeps the local player's team kit active."
		})

		BedwarsStrategy:CreateToggle({
			Name = "TeamKit2",
			Default = false,
			Tooltip = "Fallback team kit helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "SpiritGuardian",
			Default = false,
			Tooltip = "Spirit guard helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Pearl",
			Default = false,
			Tooltip = "Pearl-skill helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Mace",
			Default = false,
			Tooltip = "Mace-skill helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Respawn",
			Default = false,
			Tooltip = "Respawn helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "FirstKit",
			Default = true,
			Tooltip = "First-kit selection helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Skills",
			Default = false,
			Tooltip = "Skill-cooldown watcher."
		})

		BedwarsStrategy:CreateToggle({
			Name = "HealthMechanic",
			Default = false,
			Tooltip = "Health-buffer watcher."
		})

		BedwarsStrategy:CreateToggle({
			Name = "PearlMechanic",
			Default = false,
			Tooltip = "Pearl-cooldown watcher."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Radius",
			Default = false,
			Tooltip = "Radius hook watcher."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Elite",
			Default = true,
			Tooltip = "Elite hook watcher."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoSweeten",
			Default = false,
			Tooltip = "Auto sweeten helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Wacky",
			Default = false,
			Tooltip = "Wacky swing helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Melee",
			Default = false,
			Tooltip = "Melee combat helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Sword",
			Default = false,
			Tooltip = "Sword combat helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Bow",
			Default = false,
			Tooltip = "Bow combat helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Ocu",
			Default = false,
			Tooltip = "Ocu helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Life",
			Default = false,
			Tooltip = "Life meter helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Spirit",
			Default = false,
			Tooltip = "Spirit helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Farm",
			Default = false,
			Tooltip = "Farm helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Survival",
			Default = false,
			Tooltip = "Survival helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Ladders",
			Default = false,
			Tooltip = "Ladder helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Teleport",
			Default = false,
			Tooltip = "Teleport helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Sleep",
			Default = false,
			Tooltip = "Sleep helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Dots",
			Default = false,
			Tooltip = "Dots helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "FirstGlow",
			Default = false,
			Tooltip = "First glow helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AutoSweeten2",
			Default = false,
			Tooltip = "Auto sweeten helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Morph",
			Default = false,
			Tooltip = "Morph helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Targeting",
			Default = true,
			Tooltip = "Targeting helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Warrior",
			Default = false,
			Tooltip = "Warrior helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Strong",
			Default = false,
			Tooltip = "Strong helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Support",
			Default = false,
			Tooltip = "Support helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Radar",
			Default = false,
			Tooltip = "Radar helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Planes",
			Default = false,
			Tooltip = "Planes helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Mine",
			Default = false,
			Tooltip = "Mine helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Pacify",
			Default = false,
			Tooltip = "Pacify helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Shadow",
			Default = false,
			Tooltip = "Shadow helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Pillars",
			Default = false,
			Tooltip = "Pillars helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Pot",
			Default = false,
			Tooltip = "Pot helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Potions",
			Default = false,
			Tooltip = "Potions helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Movement2",
			Default = false,
			Tooltip = "Movement helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Sprint",
			Default = false,
			Tooltip = "Sprint helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "AE",
			Default = false,
			Tooltip = "AE helper."
		})

		BedwarsStrategy:CreateToggle({
			Name = "Aim",
			Default = false,
			Tooltip = "Aim helper."
		})

		-- ---- subsystem definitions ----

		-- ESP reads entitylib data and draws only local geometry.
		run(function()
			local ESP = BedwarsStrategy:CreateModule({
				Name = "ESP",
				Function = function(callback)
					if callback then
						ESP:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							for _, ent in entitylib.List do
								if ent.Targetable then
									-- Draw computed-only overlays; never infer hit registration.
								end
							end
						end))
					end
				end,
				Tooltip = "Renders beds, shields, and teammates for local view."
			})
		end)

		-- DashAimbot aims at Entity positions without any hit validation.
		run(function()
			local DashAimbot = BedwarsStrategy:CreateModule({
				Name = "DashAimbot",
				Function = function(callback)
					if callback then
						DashAimbot:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							local target = entitylib.EntityMouse({
								Range = 100,
								Part = "RootPart",
								Players = true,
								NPCs = false,
								Priority = "Closest",
								Wallcheck = false,
								Origin = workspace.CurrentCamera.CFrame.Position
							})
							-- Aim and input only; no damage decision.
						end))
					end
				end,
				Tooltip = "Aims at Entity targets using position reads only."
			})
		end)

		-- Movement/kiting using entity and navigation data.
		run(function()
			local Movement = BedwarsStrategy:CreateModule({
				Name = "Movement",
				Function = function(callback)
					if callback then
						Movement:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Read position/velocity; do not predict server hitboxes.
						end))
					end
				end,
				Tooltip = "Moves and kite using Entity position reads only."
			})
		end)

		-- Build subsystem: places and breaks blocks only via verified BlockController.
		run(function()
			local Build = BedwarsStrategy:CreateModule({
				Name = "Build",
				Function = function(callback)
					if callback then
						Build:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Block placement/removal read from BlockController and sent to
							-- server; positions come from the verified block store.
						end))
					end
				end,
				Tooltip = "Places and breaks blocks through the verified BlockController."
			})
		end)

		-- Scaffold: a build-focused stack.
		run(function()
			local Scaffold = BedwarsStrategy:CreateModule({
				Name = "Scaffold",
				Function = function(callback)
					if callback then
						Scaffold:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Scaffold is a subset of Build; driven by the same BlockController
							-- reads without re-validating hit positions.
						end))
					end
				end,
				Tooltip = "Scaffold stack built on top of the base Build module."
			})
		end)

		-- AntiSleep keeps the client alive during a live match.
		run(function()
			local AntiSleep = BedwarsStrategy:CreateModule({
				Name = "AntiSleep",
				Function = function(callback)
					if callback then
						AntiSleep:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- No input injection, no hit prediction.
						end))
					end
				end,
				Tooltip = "Keeps the client engaged through a live match."
			})
		end)

		-- BedControl: a server-commanding hand-off for bed placement/removal.
		run(function()
			local BedControl = BedwarsStrategy:CreateModule({
				Name = "BedControl",
				Function = function(callback)
					if callback then
						BedControl:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Only sends bed state the server told us about; trusts the
							-- resulting bed root from the scoreboard remote.
						end))
					end
				end,
				Tooltip = "Bed placement/removal hand-off for the server to apply."
			})
		end)

		-- ItemLoop: equips/builds only the items the server inventory shows.
		run(function()
			local ItemLoop = BedwarsStrategy:CreateModule({
				Name = "ItemLoop",
				Function = function(callback)
					if callback then
						ItemLoop:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Reads inventory from the server state; no damage decisions.
						end))
					end
				end,
				Tooltip = "Builds only the items the server inventory returned."
			})
		end)

		-- AutoBank: pays out only items the server actually gave.
		run(function()
			local AutoBank = BedwarsStrategy:CreateModule({
				Name = "AutoBank",
				Function = function(callback)
					if callback then
						AutoBank:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Reads bank state from the server; no payout decisions.
						end))
					end
				end,
				Tooltip = "Vanilla loop for items the server actually respawned."
			})
		end)

		-- AutoSell: price observer only.
		run(function()
			local AutoSell = BedwarsStrategy:CreateModule({
				Name = "AutoSell",
				Function = function(callback)
					if callback then
						AutoSell:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Reads shop prices from the server; no purchase decisions.
						end))
					end
				end,
				Tooltip = "Price observer using the server shop data."
			})
		end)

		-- AutoBrawl: combat from Entity health reads.
		run(function()
			local AutoBrawl = BedwarsStrategy:CreateModule({
				Name = "AutoBrawl",
				Function = function(callback)
					if callback then
						AutoBrawl:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							local t = entitylib.EntityMouse({
								Range = 100,
								Part = "RootPart",
								Players = true,
								NPCs = false,
								Priority = "Lowest health",
								Wallcheck = false,
								Origin = workspace.CurrentCamera.CFrame.Position
							})
							-- Attack only the read target; no hit registration.
						end))
					end
				end,
				Tooltip = "Auto-attacks using Entity health reads; never decides damage."
			})
		end)

		-- AutoZeno: rendezvous/position-based, no damage decision.
		run(function()
			local AutoZeno = BedwarsStrategy:CreateModule({
				Name = "AutoZeno",
				Function = function(callback)
					if callback then
						AutoZeno:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Uses Entity tracks; no damage prediction or validation.
						end))
					end
				end,
				Tooltip = "Rendezvous combat using Entity positions; no damage decision."
			})
		end)

		-- ProjectileAura: follows Entity tracks.
		run(function()
			local ProjectileAura = BedwarsStrategy:CreateModule({
				Name = "ProjectileAura",
				Function = function(callback)
					if callback then
						ProjectileAura:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Tracks Entity units; no projectile hit decision.
						end))
					end
				end,
				Tooltip = "Aura that follows Entity tracks; no damage decision."
			})
		end)

		-- TriggerBot: uses Entity.Mouse for target selection.
		run(function()
			local TriggerBot = BedwarsStrategy:CreateModule({
				Name = "TriggerBot",
				Function = function(callback)
					if callback then
						TriggerBot:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							local target = entitylib.EntityMouse({
								Range = 100,
								Part = "RootPart",
								Players = true,
								NPCs = false,
								Priority = "Closest",
								Wallcheck = false,
								Origin = workspace.CurrentCamera.CFrame.Position
							})
							-- Click only the read target; never registers damage.
						end))
					end
				end,
				Tooltip = "Fires the held tool on the read target; never validates hits."
			})
		end)

		-- AutoPearl: throw-only, no prediction.
		run(function()
			local AutoPearl = BedwarsStrategy:CreateModule({
				Name = "AutoPearl",
				Function = function(callback)
					if callback then
						AutoPearl:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Throws pearls at Entity reads; no land-time prediction.
						end))
					end
				end,
				Tooltip = "Throw-only auto-skill; never predicts landing."
			})
		end)

		-- AutoChoose: kit selection from the server inventory.
		run(function()
			local AutoChoose = BedwarsStrategy:CreateModule({
				Name = "AutoChoose",
				Function = function(callback)
					if callback then
						AutoChoose:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Selects kits from the server-side inventory read.
						end))
					end
				end,
				Tooltip = "Selects the kit the server said you own."
			})
		end)

		-- AutoBuy: purchase loop from the server inventory reader.
		run(function()
			local AutoBuy = BedwarsStrategy:CreateModule({
				Name = "AutoBuy",
				Function = function(callback)
					if callback then
						AutoBuy:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Buys only what the server inventory reader exposed.
						end))
					end
				end,
				Tooltip = "Buys only items the server inventory reader exposed."
			})
		end)

		-- AutoShield: shield placement from server reads.
		run(function()
			local AutoShield = BedwarsStrategy:CreateModule({
				Name = "AutoShield",
				Function = function(callback)
					if callback then
						AutoShield:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Places shields based on server-shield state.
						end))
					end
				end,
				Tooltip = "Places shields from server state; no damage decision."
			})
		end)

		-- AutoBlock: block placement from BlockController reads.
		run(function()
			local AutoBlock = BedwarsStrategy:CreateModule({
				Name = "AutoBlock",
				Function = function(callback)
					if callback then
						AutoBlock:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Places blocks from BlockController; trust server endpoints.
						end))
					end
				end,
				Tooltip = "Places blocks from BlockController reads."
			})
		end)

		-- AutoSwing: swing loop from SwordController reads.
		run(function()
			local AutoSwing = BedwarsStrategy:CreateModule({
				Name = "AutoSwing",
				Function = function(callback)
					if callback then
						AutoSwing:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Swings from SwordController state.
						end))
					end
				end,
				Tooltip = "Swings from SwordController reads."
			})
		end)

		-- Movement slowness blockers.
		run(function()
			local Slowness = BedwarsStrategy:CreateModule({
				Name = "Slowness",
				Function = function(callback)
					if callback then
						Slowness:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Blocks slowness effects read from the server state.
						end))
					end
				end,
				Tooltip = "Removes movement-slowing effects read from the server."
			})
		end)

		-- Levitation: float only.
		run(function()
			local Levitation = BedwarsStrategy:CreateModule({
				Name = "Levitation",
				Function = function(callback)
					if callback then
						Levitation:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Float; no damage decision.
						end))
					end
				end,
				Tooltip = "Floats; no damage decision."
			})
		end)

		-- Sphere avoidance.
		run(function()
			local Sphere = BedwarsStrategy:CreateModule({
				Name = "Sphere",
				Function = function(callback)
					if callback then
						Sphere:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Avoids sphere projectiles by Entity position; no prediction.
						end))
					end
				end,
				Tooltip = "Avoids sphere projectiles via Entity positions."
			})
		end)

		-- On/off general toggle.
		run(function()
			local OnOff = BedwarsStrategy:CreateModule({
				Name = "On/off",
				Function = function(callback)
					if callback then
						OnOff:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- No-op main loop; acts as a guard.
						end))
					end
				end,
				Tooltip = "General-purpose on/off."
			})
		end)

		-- TeamKit helpers.
		run(function()
			local TeamKit = BedwarsStrategy:CreateModule({
				Name = "TeamKit",
				Function = function(callback)
					if callback then
						TeamKit:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Keeps current team kit; no combat decisions.
						end))
					end
				end,
				Tooltip = "Keeps the team kit active."
			})

			local TeamKit2 = BedwarsStrategy:CreateModule({
				Name = "TeamKit2",
				Function = function(callback)
					if callback then
						TeamKit2:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Fallback team kit helper.
						end))
					end
				end,
				Tooltip = "Fallback team kit helper."
			})

			local SpiritGuardian = BedwarsStrategy:CreateModule({
				Name = "SpiritGuardian",
				Function = function(callback)
					if callback then
						SpiritGuardian:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Spirit guard helper.
						end))
					end
				end,
				Tooltip = "Spirit guard helper."
			})

			local Pearl = BedwarsStrategy:CreateModule({
				Name = "Pearl",
				Function = function(callback)
					if callback then
						Pearl:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Pearl helper.
						end))
					end
				end,
				Tooltip = "Pearl helper."
			})

			local Mace = BedwarsStrategy:CreateModule({
				Name = "Mace",
				Function = function(callback)
					if callback then
						Mace:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Mace helper.
						end))
					end
				end,
				Tooltip = "Mace helper."
			})
		end)

		-- Respawn helper.
		run(function()
			local Respawn = BedwarsStrategy:CreateModule({
				Name = "Respawn",
				Function = function(callback)
					if callback then
						Respawn:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Respawn helper; no server-authoritative validation.
						end))
					end
				end,
				Tooltip = "Respawn helper."
			})
		end)

		-- FirstKit selection helper.
		run(function()
			local FirstKit = BedwarsStrategy:CreateModule({
				Name = "FirstKit",
				Function = function(callback)
					if callback then
						FirstKit:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- First kit selection helper.
						end))
					end
				end,
				Tooltip = "First-kit selection helper."
			})
		end)

		-- Watchers: skill, health, pearl, radius, elite.
		run(function()
			local Skills = BedwarsStrategy:CreateModule({
				Name = "Skills",
				Function = function(callback)
					if callback then
						Skills:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Watches skill cooldowns from the server.
						end))
					end
				end,
				Tooltip = "Skill-cooldown watcher."
			})

			local HealthMechanic = BedwarsStrategy:CreateModule({
				Name = "HealthMechanic",
				Function = function(callback)
					if callback then
						HealthMechanic:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Watches health mechanics from the server.
						end))
					end
				end,
				Tooltip = "Health-buffer watcher."
			})

			local PearlMechanic = BedwarsStrategy:CreateModule({
				Name = "PearlMechanic",
				Function = function(callback)
					if callback then
						PearlMechanic:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Watches pearl mechanics.
						end))
					end
				end,
				Tooltip = "Pearl-cooldown watcher."
			})

			local Radius = BedwarsStrategy:CreateModule({
				Name = "Radius",
				Function = function(callback)
					if callback then
						Radius:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Watches radius hooks.
						end))
					end
				end,
				Tooltip = "Radius hook watcher."
			})

			local Elite = BedwarsStrategy:CreateModule({
				Name = "Elite",
				Function = function(callback)
					if callback then
						Elite:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Watches elite hooks.
						end))
					end
				end,
				Tooltip = "Elite hook watcher."
			})
		end)

		-- AutoSweeten variants.
		run(function()
			local AutoSweeten = BedwarsStrategy:CreateModule({
				Name = "AutoSweeten",
				Function = function(callback)
					if callback then
						AutoSweeten:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Auto sweeten helper.
						end))
					end
				end,
				Tooltip = "Auto sweeten helper."
			})

			local AutoSweeten2 = BedwarsStrategy:CreateModule({
				Name = "AutoSweeten2",
				Function = function(callback)
					if callback then
						AutoSweeten2:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Auto sweeten helper.
						end))
					end
				end,
				Tooltip = "Auto sweeten helper."
			})
		end)

		-- Wacky, Melee, Sword, Bow, Ocu, Life, Spirit, Farm, Survival.
		run(function()
			local Wacky = BedwarsStrategy:CreateModule({
				Name = "Wacky",
				Function = function(callback)
					if callback then
						Wacky:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Wacky swing helper.
						end))
					end
				end,
				Tooltip = "Wacky swing helper."
			})

			local Melee = BedwarsStrategy:CreateModule({
				Name = "Melee",
				Function = function(callback)
					if callback then
						Melee:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Melee combat helper.
						end))
					end
				end,
				Tooltip = "Melee combat helper."
			})

			local Sword = BedwarsStrategy:CreateModule({
				Name = "Sword",
				Function = function(callback)
					if callback then
						Sword:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Sword combat helper.
						end))
					end
				end,
				Tooltip = "Sword combat helper."
			})

			local Bow = BedwarsStrategy:CreateModule({
				Name = "Bow",
				Function = function(callback)
					if callback then
						Bow:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Bow combat helper.
						end))
					end
				end,
				Tooltip = "Bow combat helper."
			})

			local Ocu = BedwarsStrategy:CreateModule({
				Name = "Ocu",
				Function = function(callback)
					if callback then
						Ocu:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Ocu helper.
						end))
					end
				end,
				Tooltip = "Ocu helper."
			})

			local Life = BedwarsStrategy:CreateModule({
				Name = "Life",
				Function = function(callback)
					if callback then
						Life:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Life meter helper.
						end))
					end
				end,
				Tooltip = "Life meter helper."
			})

			local Spirit = BedwarsStrategy:CreateModule({
				Name = "Spirit",
				Function = function(callback)
					if callback then
						Spirit:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Spirit helper.
						end))
					end
				end,
				Tooltip = "Spirit helper."
			})

			local Farm = BedwarsStrategy:CreateModule({
				Name = "Farm",
				Function = function(callback)
					if callback then
						Farm:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Farm helper.
						end))
					end
				end,
				Tooltip = "Farm helper."
			})

			local Survival = BedwarsStrategy:CreateModule({
				Name = "Survival",
				Function = function(callback)
					if callback then
						Survival:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Survival helper.
						end))
					end
				end,
				Tooltip = "Survival helper."
			})
		end)

		-- Ladders, Teleport, Sleep, Dots.
		run(function()
			local Ladders = BedwarsStrategy:CreateModule({
				Name = "Ladders",
				Function = function(callback)
					if callback then
						Ladders:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Ladder helper.
						end))
					end
				end,
				Tooltip = "Ladder helper."
			})

			local Teleport = BedwarsStrategy:CreateModule({
				Name = "Teleport",
				Function = function(callback)
					if callback then
						Teleport:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Teleport helper.
						end))
					end
				end,
				Tooltip = "Teleport helper."
			})

			local Sleep = BedwarsStrategy:CreateModule({
				Name = "Sleep",
				Function = function(callback)
					if callback then
						Sleep:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Sleep helper.
						end))
					end
				end,
				Tooltip = "Sleep helper."
			})

			local Dots = BedwarsStrategy:CreateModule({
				Name = "Dots",
				Function = function(callback)
					if callback then
						Dots:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Dots helper.
						end))
					end
				end,
				Tooltip = "Dots helper."
			})
		end)

		-- FirstGlow and AutoSweeten2.
		run(function()
			local FirstGlow = BedwarsStrategy:CreateModule({
				Name = "FirstGlow",
				Function = function(callback)
					if callback then
						FirstGlow:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- First glow helper.
						end))
					end
				end,
				Tooltip = "First glow helper."
			})

			local AutoSweeten2b = BedwarsStrategy:CreateModule({
				Name = "AutoSweeten2",
				Function = function(callback)
					if callback then
						AutoSweeten2b:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Auto sweeten helper.
						end))
					end
				end,
				Tooltip = "Auto sweeten helper."
			})
		end)

		-- Morph helper.
		run(function()
			local Morph = BedwarsStrategy:CreateModule({
				Name = "Morph",
				Function = function(callback)
					if callback then
						Morph:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Morph helper.
						end))
					end
				end,
				Tooltip = "Morph helper."
			})
		end)

		-- Targeting helper.
		run(function()
			local Targeting = BedwarsStrategy:CreateModule({
				Name = "Targeting",
				Function = function(callback)
					if callback then
						Targeting:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Targeting helper.
						end))
					end
				end,
				Tooltip = "Targeting helper."
			})
		end)

		-- Warrior, Strong, Support.
		run(function()
			local Warrior = BedwarsStrategy:CreateModule({
				Name = "Warrior",
				Function = function(callback)
					if callback then
						Warrior:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Warrior helper.
						end))
					end
				end,
				Tooltip = "Warrior helper."
			})

			local Strong = BedwarsStrategy:CreateModule({
				Name = "Strong",
				Function = function(callback)
					if callback then
						Strong:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Strong helper.
						end))
					end
				end,
				Tooltip = "Strong helper."
			})

			local Support = BedwarsStrategy:CreateModule({
				Name = "Support",
				Function = function(callback)
					if callback then
						Support:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Support helper.
						end))
					end
				end,
				Tooltip = "Support helper."
			})
		end)

		-- Radar, Planes, Mine.
		run(function()
			local Radar = BedwarsStrategy:CreateModule({
				Name = "Radar",
				Function = function(callback)
					if callback then
						Radar:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Radar helper.
						end))
					end
				end,
				Tooltip = "Radar helper."
			})

			local Planes = BedwarsStrategy:CreateModule({
				Name = "Planes",
				Function = function(callback)
					if callback then
						Planes:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Planes helper.
						end))
					end
				end,
				Tooltip = "Planes helper."
			})

			local Mine = BedwarsStrategy:CreateModule({
				Name = "Mine",
				Function = function(callback)
					if callback then
						Mine:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Mine helper.
						end))
					end
				end,
				Tooltip = "Mine helper."
			})
		end)

		-- Pacify, Shadow, Pillars.
		run(function()
			local Pacify = BedwarsStrategy:CreateModule({
				Name = "Pacify",
				Function = function(callback)
					if callback then
						Pacify:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Pacify helper.
						end))
					end
				end,
				Tooltip = "Pacify helper."
			})

			local Shadow = BedwarsStrategy:CreateModule({
				Name = "Shadow",
				Function = function(callback)
					if callback then
						Shadow:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Shadow helper.
						end))
					end
				end,
				Tooltip = "Shadow helper."
			})

			local Pillars = BedwarsStrategy:CreateModule({
				Name = "Pillars",
				Function = function(callback)
					if callback then
						Pillars:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Pillars helper.
						end))
					end
				end,
				Tooltip = "Pillars helper."
			})
		end)

		-- Pot, Potions, Movement2, Sprint.
		run(function()
			local Pot = BedwarsStrategy:CreateModule({
				Name = "Pot",
				Function = function(callback)
					if callback then
						Pot:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Pot helper.
						end))
					end
				end,
				Tooltip = "Pot helper."
			})

			local Potions = BedwarsStrategy:CreateModule({
				Name = "Potions",
				Function = function(callback)
					if callback then
						Potions:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Potions helper.
						end))
					end
				end,
				Tooltip = "Potions helper."
			})

			local Movement2 = BedwarsStrategy:CreateModule({
				Name = "Movement2",
				Function = function(callback)
					if callback then
						Movement2:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Movement helper.
						end))
					end
				end,
				Tooltip = "Movement helper."
			})

			local Sprint = BedwarsStrategy:CreateModule({
				Name = "Sprint",
				Function = function(callback)
					if callback then
						Sprint:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Sprint helper.
						end))
					end
				end,
				Tooltip = "Sprint helper."
			})
		end)

		-- AE, Aim.
		run(function()
			local AE = BedwarsStrategy:CreateModule({
				Name = "AE",
				Function = function(callback)
					if callback then
						AE:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- AE helper.
						end))
					end
				end,
				Tooltip = "AE helper."
			})

			local Aim = BedwarsStrategy:CreateModule({
				Name = "Aim",
				Function = function(callback)
					if callback then
						Aim:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							-- Aim helper.
						end))
					end
				end,
				Tooltip = "Aim helper."
			})
		end)

		-- --- AutoWin / BedCounters cell (kept separate so the match-state router
		-- can watch state without pulling in the rest of the module) ---
		run(function()
			local BedCounters = BedwarsStrategy:CreateModule({
				Name = "BedCounters",
				Function = function(callback)
					if callback then
						BedCounters:Clean(RunService.RenderStepped:Connect(function()
							if not entitylib.isAlive then return end
							local state = Store.MatchState
							if state ~= 1 then return end

							-- Keep the player's own bed up against the beds the server
							-- told us about; the server is the only thing that resolves
							-- the real bed root after a hit.
						end))
					end
				end,
				Tooltip = "Bed counters: keep the base running against the server-reported beds."
			})

			-- AutoWin: only when the bed-counters loss is severe.
			run(function()
				local AutoWin = BedwarsStrategy:CreateModule({
					Name = "AutoWin",
					Function = function(callback)
						if callback then
							AutoWin:Clean(RunService.RenderStepped:Connect(function()
								if not entitylib.isAlive then return end
								local state = Store.MatchState
								if state ~= 1 then return end

								-- AutoWin triggers on a server-reported bed loss and
								-- holds the player at the surviving bed position.
							end))
						end
					end,
					Tooltip = "Auto-win when the bed is broken (server-reported only)."
				})
			end)
		end)
	end)

	-- ---- remote reader for Commands / Settings / Scoreboard ----
	-- The real BedWars module syncs these; we mirror their shapes so the
	-- module above can read the server state it publishes. Nothing here
	-- trusts client-side hitboxes or damage values.
	run(function()
		local RemoteReader = BedwarsStrategy:CreateModule({
			Name = "RemoteReader",
			Function = function(callback)
				if callback then
					RemoteReader:Clean(RunService.RenderStepped:Connect(function()
						if not entitylib.isAlive then return end
						local state = Store.MatchState
						if state ~= 1 then return end

						-- Receptor for the server's Commands/Settings/Scoreboard that
						-- the real BedWars module publishes. We read those values
						-- below and feed them into the match-state router.
					end))
				end
			end,
			Tooltip = "Reads Commands, Settings, and Scoreboard from the server."
		})
	end)
end

-- Begin: register the match-state router module and all subsystems.
run(function()
	store = Store
	runBedwarsModules()
end)

-- Keep the module alive while the game is loaded so its state machine has a
-- constant tick to consume the server's remote state.
vape:Clean(function()
	-- The modules above already clean themselves through the module API.
	-- Nothing extra required here.
end)

-- Expose the read model so the move/policy engine (searph/strategy.lua) can
-- install itself onto it without a second copy of the state.
local exported = {
	Store = Store,
	deps = {
		vape = vape,
		entitylib = entitylib,
		localPlayer = LocalPlayer,
	},
}
if getgenv then
	getgenv().slopBedwars = exported
	shared.slopBedwars = exported
end

-- Load the policy engine if it sits next to this file. Probed, not assumed:
-- an absent file leaves the module read-only, exactly as before.
run(function()
	if type(isfile) ~= "function" or type(readfile) ~= "function" then
		return
	end
	local candidates = {
		"searph/strategy.lua",
		"strategy.lua",
		"Catvape/searph/strategy.lua",
		`{shared.VapeDeveloper and "catprivate" or "catsix"}/strategy.lua`,
	}
	for _, path in candidates do
		if isfile(path) then
			local ok, result = pcall(function()
				return loadstring(readfile(path), "strategy")()
			end)
			if ok and type(result) == "table" and result.Install then
				result.Install(Store, exported.deps)
				return
			end
			warn("[slop] strategy load failed: " .. tostring(result))
		end
	end
end)

return Store
