local u=(Instance.new("\080\097\114\116")).GetConnectedParts local q=script.GetChildren function GetPartWelds(q)local h={}for u in pairs(SearchWelds(q,q))do h[u]=true end for u,S in pairs(u(q))do for u in pairs(SearchWelds(S,q))do h[u]=true end end return h end function SearchWelds(u,h)local S={}for u,q in pairs(q(u))do if q.Name=="\066\084\087\101\108\100"and(q.ClassName=="\087\101\108\100"and(q.Part0==h or q.Part1==h))then S[q]=true end end return S end function CreateWelds()local u=Core.SyncAPI:Invoke("\067\114\101\097\116\101\087\101\108\100\115",Selection.Items,Selection.Focus)UI.Changes.Text.Text=("\099\114\101\097\116\101\100 \037\115 \119\101\108\100\037\115"):format(#u,#u==1 and""or"\115")Core.PlayConfirmationSound()local q={Welds=u;Unapply=function(u)Core.SyncAPI:Invoke("\082\101\109\111\118\101\087\101\108\100\115",u.Welds)end,Apply=function(u)Core.SyncAPI:Invoke("\085\110\100\111\082\101\109\111\118\101\100\087\101\108\100\115",u.Welds)end}Core.History.Add(q)end function BreakWelds()local u={}for q,h in pairs(Selection.Items)do for q in pairs(GetPartWelds(h))do u[q]=true end end u=Support.Keys(u)local q=Core.SyncAPI:Invoke("\082\101\109\111\118\101\087\101\108\100\115",u)UI.Changes.Text.Text=("\114\101\109\111\118\101\100 \037\115 \119\101\108\100\037\115"):format(q,q==1 and""or"\115")local h={Welds=u;Unapply=function(u)Core.SyncAPI:Invoke("\085\110\100\111\082\101\109\111\118\101\100\087\101\108\100\115",u.Welds)end;Apply=function(u)Core.SyncAPI:Invoke("\082\101\109\111\118\101\087\101\108\100\115",u.Welds)end}Core.History.Add(h)end function EnableFocusHighlighting()if Core.Mode~="\084\111\111\108"then return end Core.Selection.RecolorOutlines(Core.Selection.Color)if Selection.Focus and#Selection.Items>1 then Core.Selection.Outlines[Selection.Focus].Color=BrickColor.new("\068\101\101\112 \111\114\097\110\103\101")end Connections.FocusHighlighting=Selection.FocusChanged:connect(function(u)Core.Selection.RecolorOutlines(Core.Selection.Color)if u and#Selection.Items>1 then Core.Selection.Outlines[u].Color=BrickColor.new("\068\101\101\112 \111\114\097\110\103\101")end end)end
-- References to reduce indexing time
local GetConnectedParts = Instance.new("Part").GetConnectedParts
local GetChildren = script.GetChildren

function GetPartWelds(Part)
	-- Returns any BT-created welds involving `Part`

	local Welds = {}

	-- Get welds stored inside `Part`
	for Weld in pairs(SearchWelds(Part, Part)) do
		Welds[Weld] = true
	end

	-- Get welds stored inside connected parts
	for _, ConnectedPart in pairs(GetConnectedParts(Part)) do
		for Weld in pairs(SearchWelds(ConnectedPart, Part)) do
			Welds[Weld] = true
		end
	end

	-- Return all found welds
	return Welds
end

function SearchWelds(Haystack, Part)
	-- Searches for and returns BT-created welds in `Haystack` involving `Part`

	local Welds = {}

	-- Search the haystack for welds involving `Part`
	for _, Item in pairs(GetChildren(Haystack)) do
		-- Check if this item is a BT-created weld involving the part
		if Item.Name == "BTWeld" and Item.ClassName == "Weld" and (Item.Part0 == Part or Item.Part1 == Part) then
			-- Store weld if valid
			Welds[Item] = true
		end
	end

	-- Return the found welds
	return Welds
end

function CreateWelds()
	-- Creates welds for every selected part to the focused part

	-- Send the change request to the server API
	local Welds = Core.SyncAPI:Invoke("CreateWelds", Selection.Items, Selection.Focus)

	-- Update the UI with the number of welds created
	UI.Changes.Text.Text = ("created %s weld%s"):format(#Welds, #Welds == 1 and "" or "s")

	-- Play a confirmation sound
	Core.PlayConfirmationSound()

	-- Put together the history record
	local HistoryRecord = {
		Welds = Welds,

		Unapply = function(HistoryRecord)
			-- Reverts this change

			-- Remove the welds
			Core.SyncAPI:Invoke("RemoveWelds", HistoryRecord.Welds)
		end,

		Apply = function(HistoryRecord)
			-- Reapplies this change

			-- Restore the welds
			Core.SyncAPI:Invoke("UndoRemovedWelds", HistoryRecord.Welds)
		end,
	}

	-- Register the history record
	Core.History.Add(HistoryRecord)
end

function BreakWelds()
	-- Search for any selection-connecting, BT-created welds and remove them

	local Welds = {}

	-- Find welds in selected parts
	for _, Part in pairs(Selection.Items) do
		for Weld in pairs(GetPartWelds(Part)) do
			Welds[Weld] = true
		end
	end

	-- Turn weld index into list
	Welds = Support.Keys(Welds)

	-- Send the change request to the server API
	local WeldsRemoved = Core.SyncAPI:Invoke("RemoveWelds", Welds)

	-- Update the UI with the number of welds removed
	UI.Changes.Text.Text = ("removed %s weld%s"):format(WeldsRemoved, WeldsRemoved == 1 and "" or "s")

	-- Put together the history record
	local HistoryRecord = {
		Welds = Welds,

		Unapply = function(HistoryRecord)
			-- Reverts this change

			-- Restore the welds
			Core.SyncAPI:Invoke("UndoRemovedWelds", HistoryRecord.Welds)
		end,

		Apply = function(HistoryRecord)
			-- Reapplies this change

			-- Remove the welds
			Core.SyncAPI:Invoke("RemoveWelds", HistoryRecord.Welds)
		end,
	}

	-- Register the history record
	Core.History.Add(HistoryRecord)
end

function EnableFocusHighlighting()
	-- Enables automatic highlighting of the focused part in the selection

	-- Only enable focus highlighting in tool mode
	if Core.Mode ~= "Tool" then
		return
	end

	-- Reset all outline colors
	Core.Selection.RecolorOutlines(Core.Selection.Color)

	-- Recolor current focused item
	if Selection.Focus and (#Selection.Items > 1) then
		Core.Selection.Outlines[Selection.Focus].Color = BrickColor.new("Deep orange")
	end

	-- Recolor future focused items
	Connections.FocusHighlighting = Selection.FocusChanged:connect(function(FocusedItem)
		-- Reset all outline colors
		Core.Selection.RecolorOutlines(Core.Selection.Color)

		-- Recolor newly focused item
		if FocusedItem and (#Selection.Items > 1) then
			Core.Selection.Outlines[FocusedItem].Color = BrickColor.new("Deep orange")
		end
	end)
end
