# frozen_string_literal: true
require "active_support/core_ext/module/delegation"
require_relative "filter_set"
require_relative "query_builder_set"
require_relative "empty_state"
module DTB
# Data Tables act as presenter objects for the data returned from a {Query}.
# Queries pass data to this object, which is then passed to the view layer,
# providing methods to access the different components that should be rendered
# on the page.
#
# DataTables provide also a method (.build) to run the Query and turn it into
# a DataTable in one pass, since most likely that's what you will need on most
# endpoints that use these objects.
#
# @example build a data table in the controller
# def index
# @data_table = DTB::DataTable.build SomeQuery, params
# end
#
# @example render a data table on the view
# <%= render partial: @data_table.filters, as: :filters %>
#
# <% if @data_table.any? %>
#
#
# <%= @data_table.columns.renderable.each do |column| %>
#
<%= column.header %>
# <% end %>
#
#
# <%= render partial: @data_table.rows %>
#
#
# <% else %>
# <%= render partial: @data_table.empty_state,
# as: :empty_state,
# locals: { data_table: @data_table } %>
# <% end %>
class DataTable
# @overload build(query_class, params = {}, options = {})
# @param query_class [Class] a Query class to run and turn into a
# data table.
# @param params [Hash] Any user-supplied params (such as filters to apply)
# @param options [Hash] Any options to customize this query.
# @raise (see HasOptions#initialize)
# @return [Datatable] the data table with the results of running the query.
#
# @overload build(query)
# @param query [Query] an instance of a Query which may or may not have
# been run yet.
# @return [Datatable] the data table with the results of running the query.
#
# @overload build(object, ...)
# @param object [#to_data_table] an object that implements +#to_data_table+.
# @param ... [Array