local G=Tool:WaitForChild("\076\105\098\114\097\114\105\101\115")local E=require(G:WaitForChild("\083\117\112\112\111\114\116\076\105\098\114\097\114\121"))local x={}x.__index=x function x.new()local G=setmetatable({},x)G.Selection=E.CloneTable(Core.Selection.Items)G.Parts=E.CloneTable(Core.Selection.Parts)G.InitialColor=E.GetMemberMap(G.Parts,"\067\111\108\111\114")G.TargetColor=nil G.InitialUnionColoring={}for E,x in pairs(G.Parts)do if x:IsA("\085\110\105\111\110\079\112\101\114\097\116\105\111\110")then G.InitialUnionColoring[x]=x.UsePartColor end end return G end function x.Unapply(G)local E={}for x,S in ipairs(G.Parts)do table.insert(E,{Part=S,Color=G.InitialColor[S],UnionColoring=G.InitialUnionColoring[S]})end Core.SyncAPI:Invoke("\083\121\110\099\067\111\108\111\114",E)Core.Selection.Replace(G.Selection)end function x.Apply(E,G)local x={}for G,S in ipairs(E.Parts)do table.insert(x,{Part=S,Color=E.TargetColor,UnionColoring=true})end Core.SyncAPI:Invoke("\083\121\110\099\067\111\108\111\114",x)if not G then Core.Selection.Replace(E.Selection)end end return x

local Libraries = Tool:WaitForChild 'Libraries'
local Support = require(Libraries:WaitForChild 'SupportLibrary')

local PaintHistoryRecord = {}
PaintHistoryRecord.__index = PaintHistoryRecord

function PaintHistoryRecord.new()
    local self = setmetatable({}, PaintHistoryRecord)

    -- Include selection
    self.Selection = Support.CloneTable(Core.Selection.Items)
    self.Parts = Support.CloneTable(Core.Selection.Parts)

    -- Initialize color data
    self.InitialColor = Support.GetMemberMap(self.Parts, 'Color')
    self.TargetColor = nil

    -- Initialize union data
    self.InitialUnionColoring = {}
    for _, Part in pairs(self.Parts) do
        if Part:IsA 'UnionOperation' then
            self.InitialUnionColoring[Part] = Part.UsePartColor
        end
    end

    -- Return new record
    return self
end

function PaintHistoryRecord:Unapply()
    local Changes = {}

    -- Assemble change list
    for _, Part in ipairs(self.Parts) do
        table.insert(Changes, {
            Part = Part,
            Color = self.InitialColor[Part],
            UnionColoring = self.InitialUnionColoring[Part]
        })
    end

    -- Push changes
    Core.SyncAPI:Invoke('SyncColor', Changes)

    -- Restore selection
    Core.Selection.Replace(self.Selection)
end

function PaintHistoryRecord:Apply(KeepSelection)
    local Changes = {}

    -- Assemble change list
    for _, Part in ipairs(self.Parts) do
        table.insert(Changes, {
            Part = Part,
            Color = self.TargetColor,
            UnionColoring = true
        })
    end

    -- Push changes
    Core.SyncAPI:Invoke('SyncColor', Changes)

    -- Restore selection
    if not KeepSelection then
        Core.Selection.Replace(self.Selection)
    end
end

return PaintHistoryRecord
