module Admin::AdminHelper

  def entries_info(collection)
    n = collection.respond_to?(:total_count) ? collection.total_count : collection.size

    tag.div class: 'entries-info', id: 'n-entries' do
      return "#{n} #{collection.model_name.human(count: n)}" if collection.respond_to?(:model_name)
      return "#{n} #{collection.first.model_name.human(count: n)}" if collection.is_a?(Array) and collection.any?
    end
  end


  def audit_info(obj)
    tag.div do
      concat tag.div(l(obj.updated_at, format: :listing), title: l(obj.updated_at, format: :long), class: 'audit-date')
      concat tag.div(obj.own_and_associated_audits&.first&.user&.name, class: 'audit-info') if obj.respond_to?(:audits)
    end
  end


  def link_to_sortable(sort_by, title=nil, options={})
    title ||= sort_by.titleize
    current = (sort_by.to_sym == sorting_technique.to_sym)
    reverse = (params[:reverse].blank? and current) ? 1 : nil

    css_class = ['sort_link']
    css_class << 'current' if current
    css_class << (reverse.blank? ? 'asc' : 'desc')

    link_to title,
            url_for(
              params.permit(:q, :category, :tag).merge(sort: sort_by, reverse: reverse)
            ),
            data: options[:data] || {
              turbo_action: 'advance'
            } ,
            class: css_class.join(' ')
  end


end
