# frozen_string_literal: true

class FacetItemPresenter < Blacklight::FacetItemPresenter
  def items
    items = super
    if facet_field == "library_facet"
      # Filter out secondary facets that do not match library
      items = items.select { |i| i.value.match?(/#{value}/) }
      # Add proper secondary facet labels
      items.each { |i| i.label = i.value.split(" - ", 2).last }
    end
    return items
  end

  def has_selected_child?
    return false if facet_item.is_a?(String) || @parent_facet_item || facet_config.pivot.nil?
    items && items.size > 0 && items.any? { |item| search_state.filter([item.field]) && search_state&.filter(item.field).include?(item.value) }
  end

  def remove_href(path = search_state)
    if has_selected_child?
      path_hash = path.remove_facet_params(nil, nil)
      path_hash["f"]&.delete(items[0].field)
      search_path(path_hash)
    else
      path_hash = path.filter(facet_config.key).remove(facet_item)
      if @parent_facet_item && path_hash.dig("f", @parent_facet_item.field)
        path_hash["f"][@parent_facet_item.field] = path_hash["f"][@parent_facet_item.field].reject { |value|
          value == @parent_facet_item.value
        }
      end
      search_path(path_hash)
    end
  end

  def search_path(path)
    view_context.search_action_path(path)
  end

  def parent=(parent_facet_item)
    @parent_facet_item = parent_facet_item
  end

  def nested?
    @parent_facet_item ? true : false
  end

  def selected?
    return true if super
    if facet_config.pivot
      field = facet_item.respond_to?(:field) ? facet_item.field : facet_field
      return search_state&.filter(field).include?(value)
    end
    return false
  end
end
