class Playbook::PbAdvancedTable::TableHeader

def classname

def classname
  additional_classes = []
  additional_classes << "advanced-table-responsive-#{responsive} pinned-left" if responsive == "scroll"
  additional_classes << "selectable-rows-enabled" if selectable_rows && enable_toggle_expansion == "none"
  generate_classname("pb_advanced_table_header", "pb_table_thead", *additional_classes, separator: " ")
end

def compute_leaf_columns(columns)

def compute_leaf_columns(columns)
  columns.reduce(0) do |sum, col|
    col[:columns] ? sum + compute_leaf_columns(col[:columns]) : sum + 1
  end
end

def compute_max_depth(columns)

def compute_max_depth(columns)
  columns.map do |col|
    col[:columns] ? 1 + compute_max_depth(col[:columns]) : 1
  end.max || 1
end

def header_rows

def header_rows
  wrapped_columns = wrap_leaf_columns(column_definitions)
  rows = []
  max_depth = compute_max_depth(wrapped_columns)
  max_depth.times { rows << [] }
  process_columns(wrapped_columns, rows, 0, max_depth)
  rows
end

def process_columns(columns, rows, current_depth, max_depth)

def process_columns(columns, rows, current_depth, max_depth)
  total_columns = columns.size
  columns.each_with_index do |col, index|
    is_last = index == total_columns - 1
    if col[:columns]
      colspan = compute_leaf_columns(col[:columns])
      rows[current_depth] << {
        label: col[:label],
        colspan: colspan,
        is_last_in_group: is_last && current_depth.positive?,
      }
      process_columns(col[:columns], rows, current_depth + 1, max_depth)
    else
      colspan = 1
      rows[current_depth] << {
        label: col[:label],
        colspan: colspan,
        accessor: col[:accessor],
        sort_menu: col[:sort_menu],
        is_last_in_group: is_last && current_depth.positive?,
      }
    end
  end
end

def render_select_all_checkbox

Selectable Rows w/ Subrows - checkboxes part of toggleable first cell
def render_select_all_checkbox
  if selectable_rows
    pb_rails("checkbox", props: {
               id: "select-all-rows",
               name: "select-all-rows",
               data: {
                 action: "click->pb-advanced-table#toggleAllRowSelection",
               },
             })
  end
end

def render_select_all_header

Selectable Rows No Subrows - checkboxes in their own first cell
def render_select_all_header
  if selectable_rows
    additional_classes = []
    additional_classes << "table-header-cells-custom"
    additional_classes << "checkbox-cell-header"
    additional_classes << "pinned-left" if responsive == "scroll"
    pb_rails("table/table_header", props: {
               classname: additional_classes.join(" "),
             }) do
      pb_rails("checkbox", props: {
                 id: "select-all-rows",
                 name: "select-all-rows",
               })
    end
  end
end

def th_classname(is_first_column: false)

def th_classname(is_first_column: false)
  additional_classes = []
  additional_classes << "pinned-left" if is_first_column && responsive == "scroll" && !selectable_rows
  generate_classname("table-header-cells", *additional_classes, separator: " ")
end

def wrap_leaf_column(col, max_depth)

def wrap_leaf_column(col, max_depth)
  wrapped = {
    accessor: col[:accessor],
    label: col[:label] || "",
    sort_menu: col[:sort_menu] || nil,
  }
  (max_depth - 1).times do
    wrapped = { label: "", columns: [wrapped] }
  end
  wrapped
end

def wrap_leaf_columns(column_definitions)

def wrap_leaf_columns(column_definitions)
  max_depth = compute_max_depth(column_definitions)
  column_definitions.map do |col|
    if col.key?(:columns)
      {
        label: col[:label],
        columns: wrap_leaf_columns(col[:columns]),
      }
    else
      # For leaf columns, wrap with empty labels up to max depth to get proper structure
      wrap_leaf_column(col, max_depth)
    end
  end
end