-- @description RTL Simplify & Merge
-- @author Rockettree Labs
-- @version 1.0
-- @about Glues selected items into a single continuous item and applies a neon color.

local num_items = reaper.CountSelectedMediaItems(0)
if num_items == 0 then
    reaper.ShowMessageBox("Please select items to merge.", "RTL Simplify & Merge", 0)
    return
end

reaper.Undo_BeginBlock()

-- Item: Glue items (41588)
reaper.Main_OnCommand(41588, 0)

-- Auto-color the glued item
-- We have to recount selected items because gluing changes the items
local glued_count = reaper.CountSelectedMediaItems(0)
for i = 0, glued_count - 1 do
    local item = reaper.GetSelectedMediaItem(0, i)
    -- Random nice neon color
    local colors = {
        reaper.ColorToNative(191, 0, 255)|0x1000000, -- Purple
        reaper.ColorToNative(0, 255, 238)|0x1000000, -- Cyan
        reaper.ColorToNative(255, 0, 153)|0x1000000, -- Pink
        reaper.ColorToNative(0, 255, 135)|0x1000000  -- Green
    }
    local rnd = math.random(1, 4)
    reaper.SetMediaItemInfo_Value(item, "I_CUSTOMCOLOR", colors[rnd])
end

reaper.UpdateArrange()
reaper.Undo_EndBlock("RTL: Simplify & Merge Items", -1)
