local i={}local c=require(script:FindFirstAncestor("\080\108\117\103\105\110\070\114\097\109\101\119\111\114\107"))local P local m local f local C function i.Load(c)local v=i:GetOurDirectory()if not v then v=Instance.new("\067\111\110\102\105\103\117\114\097\116\105\111\110")v.Name=tostring(m:GetUserIdentifier())v.Parent=P.Folders.LocalMacros end local b=f:GetSetting(C.Setting.STORED_LOCAL_MACROS)or{}for i,c in pairs(b)do local P=Instance.new("\077\111\100\117\108\101\083\099\114\105\112\116")P.Name=i P.Source=c P.Parent=v end end function i.Unload(c)local m=i:GetOurDirectory()if not m then return end local v={}for i,c in pairs(m:GetChildren())do if not c:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then warn(("\076\111\099\097\108\077\097\099\114\111 \105\110\115\116\097\110\099\101 \037\113 \105\115 \110\111\116 \097 \096\077\111\100\117\108\101\083\099\114\105\112\116\096\044 \115\111 \119\097\115 \110\111\116 \115\097\118\101\100\046"):format(c:GetFullName()))c.Parent=P.Folders.Directory else local i=c:GetChildren()for i,P in pairs(i)do warn(("\076\111\099\097\108\077\097\099\114\111 \105\110\115\116\097\110\099\101 \037\113 \104\097\115 \097 \099\104\105\108\100 \037\113\046 \084\104\105\115 \099\097\110\110\111\116 \098\101 \115\097\118\101\100\033"):format(c:GetFullName(),P.Name))end if v[c.Name]then local i=0 for P,m in pairs(m:GetChildren())do if m.Name==c.Name then i+=1 end end c.Name=("\037\115 \040\037\100\041"):format(c.Name,i)end v[c.Name]=c.Source end end f:SetSetting(C.Setting.STORED_LOCAL_MACROS,v)if m then m:Destroy()end end function i.GetOurDirectory(i)if P.Folders.LocalMacros then return P.Folders.LocalMacros:FindFirstChild(tostring(m:GetUserIdentifier()))end end function i.FrameworkInit(i)m=c:Require("\083\116\117\100\105\111\085\116\105\108")P=c:Require("\083\116\117\100\105\111\072\097\110\100\108\101\114")f=c:Require("\080\108\117\103\105\110\072\097\110\100\108\101\114")C=c:Require("\080\108\117\103\105\110\067\111\110\115\116\097\110\116\115")end function i.FrameworkStart(i) end return i
---
---Handles management of LocalMacros
---
---@class LocalMacros
---
local LocalMacros = {}

-- Dependencies
local PluginFramework = require(script:FindFirstAncestor("PluginFramework")) ---@type Framework
local StudioHandler ---@type StudioHandler
local StudioUtil ---@type StudioUtil
local PluginHandler ---@type PluginHandler
local PluginConstants ---@type PluginConstants

---
---Creates a folder for our local macros, and creates module scripts
---
function LocalMacros:Load()
    -- Create directory
    local studioUserLocalMacrosFolder = LocalMacros:GetOurDirectory()
    if not studioUserLocalMacrosFolder then
        studioUserLocalMacrosFolder = Instance.new("Configuration")
        studioUserLocalMacrosFolder.Name = tostring(StudioUtil:GetUserIdentifier())
        studioUserLocalMacrosFolder.Parent = StudioHandler.Folders.LocalMacros
    end

    -- Read stored macros
    local storedMacros = PluginHandler:GetSetting(PluginConstants.Setting.STORED_LOCAL_MACROS) or {}
    for name, source in pairs(storedMacros) do
        local moduleScript = Instance.new("ModuleScript") ---@type ModuleScript
        moduleScript.Name = name
        moduleScript.Source = source
        moduleScript.Parent = studioUserLocalMacrosFolder
    end
end

---
---Unloads our LocalMacros by storing them in the plugin, and removing the directory
---
function LocalMacros:Unload()
    -- RETURN: No local macros directory stored
    local ourDirectory = LocalMacros:GetOurDirectory()
    if not ourDirectory then
        return
    end

    -- Write stored macros
    -- Hello reader - I didn't fancy writing a whole Instance serializer/deserializer to load whatever structure the user wants inside their LocalMacros folder
    -- There weren't nice existing options at time of writing either.. :c
    local storedMacros = {}
    for _, moduleScript in pairs(ourDirectory:GetChildren()) do
        -- WARN: Can only do singular modulescripts
        if not moduleScript:IsA("ModuleScript") then
            warn(("LocalMacro instance %q is not a `ModuleScript`, so was not saved."):format(moduleScript:GetFullName()))
            moduleScript.Parent = StudioHandler.Folders.Directory
        else
            -- WARN: Children!
            local children = moduleScript:GetChildren()
            for _, child in pairs(children) do
                warn(("LocalMacro instance %q has a child %q. This cannot be saved!"):format(moduleScript:GetFullName(), child.Name))
            end

            -- Check for duplicate names
            if storedMacros[moduleScript.Name] then
                -- Count how many macros have this name
                local totalDuplicateNames = 0
                for _, instance in pairs(ourDirectory:GetChildren()) do
                    if instance.Name == moduleScript.Name then
                        totalDuplicateNames += 1
                    end
                end

                moduleScript.Name = ("%s (%d)"):format(moduleScript.Name, totalDuplicateNames)
            end

            -- Store!
            storedMacros[moduleScript.Name] = moduleScript.Source
        end
    end
    PluginHandler:SetSetting(PluginConstants.Setting.STORED_LOCAL_MACROS, storedMacros)

    -- Remove directory
    if ourDirectory then
        ourDirectory:Destroy()
    end
end

---
---@return Folder
---
function LocalMacros:GetOurDirectory()
    if StudioHandler.Folders.LocalMacros then
        return StudioHandler.Folders.LocalMacros:FindFirstChild(tostring(StudioUtil:GetUserIdentifier()))
    end
end

---@private
function LocalMacros:FrameworkInit()
    StudioUtil = PluginFramework:Require("StudioUtil")
    StudioHandler = PluginFramework:Require("StudioHandler")
    PluginHandler = PluginFramework:Require("PluginHandler")
    PluginConstants = PluginFramework:Require("PluginConstants")
end

---@private
function LocalMacros:FrameworkStart() end

return LocalMacros
