{%- layout none -%}{% paginate collection.products by 30 %}{% for product in collection.products%}{%- liquid
  # Step 1: Detect if this product has child handles and collect all child product data
  assign has_child_handles = false
  assign child_products_data = ""
  assign child_handles_map = ""
  assign child_handles_array = ""
  
  # Check if any option values have product URLs (indicating child handles)
  for option in product.options_with_values
    for option_value in option.values
      if option_value.product_url
        assign has_child_handles = true
        break
      endif
    endfor
    if has_child_handles
      break
    endif
  endfor
  
  # If child handles exist, collect all child product data and their images
  if has_child_handles
    assign child_products_array = ""
    assign processed_handles = ","
    
    for option in product.options_with_values
      for option_value in option.values
        if option_value.product_url
          # Extract handle from URL
          assign child_handle = option_value.product_url | remove: "/products/"
          assign child_handle = child_handle | split: "?" | first
          
          # Since option_value.value is empty, extract color from the handle
          # Remove the base product part to get just the color portion
          assign base_handle = product.handle
          assign color_portion = child_handle | replace: base_handle, "" | replace_first: "-", ""
          
          # Create condensed versions
          assign condensed_handle = child_handle | replace: "-", "" | downcase
          assign condensed_color = color_portion | replace: "-", "" | replace: "/", "" | replace: "'", "" | downcase
          
          # Only process each handle once
          unless processed_handles contains child_handle
            assign processed_handles = processed_handles | append: child_handle | append: ","
            assign child_product = all_products[child_handle]
            
            if child_product != blank
              # Store the mapping between condensed color and condensed handle
              assign child_handles_map = child_handles_map | append: condensed_color | append: ":" | append: condensed_handle | append: ";"
              
              # Collect this child product for the array
              if child_products_array == ""
                assign child_products_array = child_handle
              else
                assign child_products_array = child_products_array | append: "," | append: child_handle
              endif
              
              # Collect child handles for array format
              if child_handles_array == ""
                assign child_handles_array = '"' | append: child_handle | append: '"'
              else
                assign child_handles_array = child_handles_array | append: ',"' | append: child_handle | append: '"'
              endif
            endif
          endunless
        endif
      endfor
    endfor
  endif
  
  # Step 2: Check if current product is a parent (has children but its handle is not in the children list)
  assign is_parent = false
  if has_child_handles
    assign current_handle = product.handle
    # Add delimiters to check for exact match, not substring
    assign delimited_child_array = "," | append: child_products_array | append: ","
    assign delimited_current_handle = "," | append: current_handle | append: ","
    unless delimited_child_array contains delimited_current_handle
      assign is_parent = true
    endunless
  endif
-%}
{% if is_parent -%}{%- assign child_handles_split = child_products_array | split: "," -%}
{% for child_handle in child_handles_split %}{{ child_handle }},{{ current_handle }}
{% endfor %}{%- endif %}{% endfor %}{% endpaginate %}