module Admin::NodeHelper

def expanded

def expanded
  show_all? || expanded_rows.include?(@current_node.id)
end

def expanded_rows

def expanded_rows
  unless @expanded_rows
    @expanded_rows = if rows = cookies[:expanded_rows]
                       rows.split(',').map do |x|
                         begin
                                                                    Integer(x)
                         rescue StandardError
                           nil
                                                                  end
                       end .compact
                     else
                       []
    end
    if homepage && !@expanded_rows.include?(homepage.id)
      @expanded_rows << homepage.id
    end
  end
  @expanded_rows
end

def expander(level)

def expander(level)
  if @current_node.children.empty? || (level == 0)
    ''
  else
    image((expanded ? 'collapse' : 'expand'),
          class: 'expander', alt: 'toggle children',
          title: '')
  end
end

def format_path(path)

def format_path(path)
  return '' if path.nil? || path.empty?
  parts = path.split('/').reject(&:empty?)
  return 'Root' if parts.size == 1
  return '/' if parts.size == 2
  formatted_path = parts[1..-2].join('/')
  formatted_path.empty? ? '/' : "/#{formatted_path}"
end

def homepage

def homepage
  @homepage ||= Page.find_by_parent_id(nil)
end

def icon

def icon
  icon_name = @current_node.virtual? ? 'virtual_page' : 'page'
  image(icon_name, class: 'icon', alt: '', title: '')
end

def node_title

def node_title
  %{<span class="title">#{h(@current_node.title)}</span>}.html_safe
end

def page_type

def page_type
  display_name = @current_node.class.display_name
  if display_name == 'Page'
    ''
  else
    %{<span class="info">(#{h(display_name)})</span>}.html_safe
  end
end

def prepare_page(page)

def prepare_page(page)
  page.extend MenuRenderer
  page.view = self
  if page.additional_menu_features?
    page.extend(*page.menu_renderer_modules)
  end
  page
end

def render_node(page, index, parent_index = nil, simple = false)

def render_node(page, index, parent_index = nil, simple = false)
  @current_node = prepare_page(page)
  @rendered_html += render_partial(page, index:, parent_index:, simple:)
  index
end

def render_nodes(page, starting_index, parent_index = nil, simple = false)

def render_nodes(page, starting_index, parent_index = nil, simple = false)
  @rendered_html = ''
  render_node page, starting_index, parent_index, simple
  @rendered_html
end

def render_partial(page, index:, parent_index:, simple:)

def render_partial(page, index:, parent_index:, simple:)
  render partial: 'admin/pages/node',
         locals: {
           level: index,
           index: index,
           parent_index: parent_index,
           page: page,
           simple: simple,
           branch: page.children.count.positive?,
         }
end

def render_search_node(page, index = 0, parent_index = nil, simple = false)

def render_search_node(page, index = 0, parent_index = nil, simple = false)
  @current_node = prepare_page(page)
  @rendered_html = render_search_partial(page, index:, parent_index:, simple:, path: format_path(page.path))
end

def render_search_partial(page, index:, parent_index:, simple:, path:)

def render_search_partial(page, index:, parent_index:, simple:, path:)
  render partial: 'admin/pages/search_result_node',
         locals: {
           level: index,
           index: index,
           parent_index: parent_index,
           page: page,
           simple: simple,
           branch: false,
           path: path,
         }
end

def show_all?

def show_all?
  controller.action_name == 'remove'
end

def spinner

def spinner
  image('spinner.gif',
        class: 'busy', id: "busy_#{@current_node.id}",
        alt: '', title: '',
        style: 'display: none;')
end