<%
%i[actions_on_multiple].each do |key|
  if (content = content_for(key).presence)
    flush_content_for key
    partial.section key, content
  end
end
%>

<%
form ||= current_fields_form

if defined?(options) && options.is_a?(Array)
  option_field_options = options
  options = defined?(html_options) ? html_options : {}
  ActiveSupport::Deprecation.new.warn(
    "`options` will be replaced with `option_field_options` in a later version. " \
    "Please pass all of the options you want as strings to ``."
  )
end

options ||= {}
option_field_options ||= options_for(form, method)
other_options ||= {}

if defined?(html_options)
  ActiveSupport::Deprecation.new.warn(
    "The `html_options` attribute for checkboxes and radio buttons will be removed in a later version " \
    "Pass all of your html options like `class` to `options` instead."
  )
end

options[:multiple] ||= false
if defined?(multiple)
  options[:multiple] = multiple
  ActiveSupport::Deprecation.new.warn(
    "The `multiple` attribute will be removed in a later version. " \
    "Please pass `options: {multiple: true}` to `render` instead."
  )
end

options[:id] ||= form.field_id(method)
show_select_all_top ||= false
show_select_all_bottom ||= false
use_columns ||= false
labels = labels_for(form, method)

options[:data] ||= {}
options[:data][:controller] ||= ""
options[:data][:controller] += " fields--field"

if options[:multiple]
  options[:data] = options[:data].merge({
    "select-all-target": 'checkbox'
  })
  options[:data][:action] ||= ""
  options[:data][:action] += " select-all#updateToggle"
end

%>

<%#
  # TODO: We should either remove this, or move the Action Models `bulk_action_select`
  # partial here to bullet_train-core because this cannot work with the setup we currently have.
%>
<% if options[:multiple] && (show_select_all_top || show_select_all_bottom) %>
  <% select_all = capture do %>
    <div class="flex">
      <%= tag.div class: [
          "hidden",
          "inline-block dark:border-slate-600",
          "border-b pb-4 mb-4": show_select_all_top && !show_select_all_bottom,
          "border-t pt-4 mt-4": !show_select_all_top && show_select_all_bottom
      ], data: {
        "select-all-target": "wrapper"
      } do %>
        <label class="relative flex items-start">
          <div class="flex items-center h-5">
            <%= check_box_tag "#{options[:id]}-select_all", 'select-all', false, data: {
              "select-all-target": 'toggleCheckbox',
              'action': "select-all#selectAllOrNone"
            }, class: "focus:ring-primary-500 h-4 w-4 text-primary-500 border-slate-300 rounded" %>
          </div>
          <div class="ml-2.5 text-sm pr-4">
            <%= t("global.bulk_select.all") %>
          </div>
        </label>
      <% end %>
      <%= partial.actions_on_multiple %>
    </div>
  <% end %>
<% end %>

<%= render 'shared/fields/field', form: form, method: method, options: options, other_options: other_options do %>
  <% content_for :field do %>
    <%= tag.div class: "pt-1.5 pb-1 sm:col-span-2", data: {
      controller: "select-all",
      "select-all-unavailable-class": "hidden"
    } do %>

      <% if options[:multiple] && show_select_all_top && !show_select_all_bottom %>
        <%= select_all %>
      <% end %>

      <%= tag.div class: ["max-w-lg": !use_columns, "max-w-3xl": use_columns, "columns-[var(--column-width,_15ch)_3]": use_columns] do %>
        <%= render 'shared/fields/option', method: method, form: form, option_field_options: option_field_options, options: options %>
      <% end %>

      <% if options[:multiple] && !show_select_all_top && show_select_all_bottom %>
        <%= select_all %>
      <% end %>

    <% end %>
  <% end %>
<% end %>