local d={}local t=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local l=require(script:FindFirstAncestor("\080\108\117\103\105\110\070\114\097\109\101\119\111\114\107"))local m local S local N local B local R local v local j local q local i local K local G local Z function d.Run(d)local t=S:GetWidget()if not t then N:Error("\067\097\110\110\111\116 \082\117\110\059 \110\111 \119\105\100\103\101\116\033")end if K then B.unmount(K)K=nil end local l=B.createElement(R.StoreProvider,{store=m:GetStore()},{Main=v:Get()})K=B.mount(l,t)G:Add(function()B.unmount(K)K=nil end,true)local q=game.StarterGui local i=q:FindFirstChild("\068\101\098\117\103\083\111\099\107\101\116\085\073")if i then i:Destroy()i=nil end if j.ShowDebugUI then i=Instance.new("\083\099\114\101\101\110\071\117\105")i.Name="\068\101\098\117\103\083\111\099\107\101\116\085\073"i.Parent=q local d=B.mount(l,i)G:Add(function()B.unmount(d)end,true)end end function d.Refresh(d)local t={type=j.RoduxActionType.MACROS.REFRESH,data={}};(m:GetStore()):dispatch(t)end function d.Stop(d)G:Cleanup()Z=nil end function d.FrameworkInit(d)S=l:Require("\080\108\117\103\105\110\072\097\110\100\108\101\114")m=l:Require("\083\111\099\107\101\116\067\111\110\116\114\111\108\108\101\114")N=l:Require("\076\111\103\103\101\114")R=l:Require("\082\111\097\099\116\082\111\100\117\120")B=l:Require("\082\111\097\099\116")v=l:Require("\082\111\097\099\116\077\097\105\110\087\105\100\103\101\116")j=l:Require("\083\111\099\107\101\116\067\111\110\115\116\097\110\116\115")q=l:Require("\074\097\110\105\116\111\114")i=l:Require("\083\111\099\107\101\116\083\101\116\116\105\110\103\115")end function d.FrameworkStart(d)G=q.new()end return d
---
---Utility functions for managing Roblox Instances
---
---@class WidgetHandler
---
local WidgetHandler = {}

--------------------------------------------------
-- Types
-- ...

--------------------------------------------------
-- Dependencies
local UserInputService = game:GetService("UserInputService") ---@type UserInputService
local PluginFramework = require(script:FindFirstAncestor("PluginFramework")) ---@type Framework
local SocketController ---@type SocketController
local PluginHandler ---@type PluginHandler
local Logger ---@type Logger
local Roact ---@type Roact
local RoactRodux ---@type RoactRodux
local RoactMainWidget ---@type RoactMainWidget
local SocketConstants ---@type SocketConstants
local Janitor ---@type Janitor
local SocketSettings ---@type SocketSettings

--------------------------------------------------
-- Constants
-- ...

--------------------------------------------------
-- Members
local roactTree ---@type RoactTree
local runJanitor ---@type Janitor
local registerTextBoxMemory ---@type table

---
---Called to start the widget population
---
function WidgetHandler:Run()
    local widget = PluginHandler:GetWidget()
    if not widget then
        Logger:Error("Cannot Run; no widget!")
    end

    if roactTree then
        Roact.unmount(roactTree)
        roactTree = nil
    end

    -- Create main component + link with rodux store
    local app = Roact.createElement(RoactRodux.StoreProvider, {
        store = SocketController:GetStore(),
    }, {
        Main = RoactMainWidget:Get(),
    })

    -- Mount to widget
    roactTree = Roact.mount(app, widget)
    runJanitor:Add(function()
        Roact.unmount(roactTree)
        roactTree = nil
    end, true)

    -- TESTING: Debug UI
    local gui = game.StarterGui
    local debugScreenGui = gui:FindFirstChild("DebugSocketUI")
    if debugScreenGui then
        debugScreenGui:Destroy()
        debugScreenGui = nil
    end

    if SocketConstants.ShowDebugUI then
        debugScreenGui = Instance.new("ScreenGui") ---@type ScreenGui
        debugScreenGui.Name = "DebugSocketUI"
        debugScreenGui.Parent = gui

        local debugRoactTree = Roact.mount(app, debugScreenGui)
        runJanitor:Add(function()
            Roact.unmount(debugRoactTree)
        end, true)
    end
end

---
---Refreshes the MACROS store, and hence the widget
---
function WidgetHandler:Refresh()
    -- Update RoduxStore
    ---@type RoduxAction
    local action = {
        type = SocketConstants.RoduxActionType.MACROS.REFRESH,
        data = {},
    }
    SocketController:GetStore():dispatch(action)
end

---
---Called when we no longer need the widget
---
function WidgetHandler:Stop()
    runJanitor:Cleanup()
    registerTextBoxMemory = nil
end

---@private
function WidgetHandler:FrameworkInit()
    PluginHandler = PluginFramework:Require("PluginHandler")
    SocketController = PluginFramework:Require("SocketController")
    Logger = PluginFramework:Require("Logger")
    RoactRodux = PluginFramework:Require("RoactRodux")
    Roact = PluginFramework:Require("Roact")
    RoactMainWidget = PluginFramework:Require("RoactMainWidget")
    SocketConstants = PluginFramework:Require("SocketConstants")
    Janitor = PluginFramework:Require("Janitor")
    SocketSettings = PluginFramework:Require("SocketSettings")
end

---@private
function WidgetHandler:FrameworkStart()
    runJanitor = Janitor.new()
end

return WidgetHandler
