--- Builds and returns a button for each item in the scope hierarchy.
-- @returns ScopeHierarchyItemButton[]

function ScopeHUD:BuildScopeHierarchyButtons()
    local Hierarchy = {}
    local Buttons = {}

    -- Navigate up hierarchy from scope target
    local CurrentScopePosition = self.state.ScopeTarget or
                                 self.state.Scope
    while (CurrentScopePosition ~= nil) and
          (CurrentScopePosition ~= Workspace.Parent) do
        table.insert(Hierarchy, 1, CurrentScopePosition)
        CurrentScopePosition = CurrentScopePosition.Parent
    end

    -- Create button for each scope hierarchy item
    for Index, ScopePosition in ipairs(Hierarchy) do
        Buttons[Index] = new(ScopeHierarchyItemButton, {
            Instance = ScopePosition;
            IsTarget = (self.state.ScopeTarget == ScopePosition);
            IsScopeParent = self.state.Scope and (self.state.Scope.Parent == ScopePosition);
            IsScope = (self.state.Scope == ScopePosition);
            IsScopable = (self.state.ScopeTarget == ScopePosition) and IsItemScopable(ScopePosition);
            IsScopeLocked = self.state.IsScopeLocked;
            SetScopeFromButton = self.SetScopeFromButton;
            IsAltDown = self.state.IsAltDown;
            LayoutOrder = Index + 1;
        })
    end

    -- Add hotkey tooltip if alt not held down
    if not self.state.IsAltDown then
        Buttons[#Hierarchy + 1] = new(HotkeyTooltip, {
            IsAltDown = false;
            DisplayAltHotkey = UserInputService.KeyboardEnabled;
            LayoutOrder = #Hierarchy + 2;
        })
    end

    -- Return list of buttons
    return Buttons
end

return ScopeHUD
