<%
  modal_id ||= "modal-#{SecureRandom.hex(4)}"
  icon_svg ||= nil
  icon_color ||= 'text-primary'
  title ||= 'Confirm'
  description ||= nil
  buttons ||= []
  max_width ||= 'max-w-md'
  custom ||= nil

  custom_html =
    if custom.is_a?(String)
      sanitize(
        custom,
        tags: %w[div p span a button form input label select option textarea h1 h2 h3 h4 h5 h6 ul ol li strong em code pre hr br svg path circle rect line polyline polygon],
        attributes: %w[class id href type name value placeholder data-controller data-action data-target data-token for method required disabled readonly accept maxlength action style target rel]
      )
    elsif custom.present?
      custom
    else
      nil
    end

  actions_html = capture do
    if buttons.any?
%>
      <div class="grid grid-cols-1 gap-3 sm:grid-cols-2">
        <% buttons.each do |button| %>
          <% button_classes = "inline-flex w-full items-center justify-center rounded-xl px-4 py-2.5 text-sm font-semibold transition-colors duration-150 #{button[:class]}" %>
          <% if button[:form] %>
            <% if button[:form_id] %>
              <button type="submit" form="<%= button[:form_id] %>" class="<%= button_classes %>">
                <%= button[:text] %>
              </button>
            <% else %>
              <form method="post" action="<%= button[:url] %>" class="m-0 w-full">
                <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
                <% if button[:method] && button[:method] != 'post' %>
                  <input type="hidden" name="_method" value="<%= button[:method] %>">
                <% end %>
                <button type="submit" class="<%= button_classes %>">
                  <%= button[:text] %>
                </button>
              </form>
            <% end %>
          <% else %>
            <% close_on_click = if button.key?(:close)
                                  button[:close]
                                else
                                  button[:action].blank? || button[:action].to_s.include?("modal#close")
                                end %>
            <button
              type="button"
              data-modal-close="<%= close_on_click %>"
              class="<%= button_classes %>"
            >
              <%= button[:text] %>
            </button>
          <% end %>
        <% end %>
      </div>
<%
    end
  end
%>

<div
  id="<%= modal_id %>"
  class="hidden"
  data-bits-modal
  data-modal-title="<%= title %>"
  data-modal-description="<%= description.to_s %>"
  data-modal-max-width="<%= max_width %>"
>
  <% if icon_svg %>
    <template data-modal-icon>
      <svg class="h-10 w-10 <%= icon_color %>" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
        <%= sanitize icon_svg, tags: %w[path circle rect line polyline polygon], attributes: %w[d cx cy r x y x1 y1 x2 y2 points stroke-linecap stroke-linejoin stroke-width fill] %>
      </svg>
    </template>
  <% end %>

  <% if custom_html.present? %>
    <template data-modal-custom>
      <%= custom_html %>
    </template>
  <% end %>

  <% if actions_html.present? %>
    <template data-modal-actions>
      <%= actions_html %>
    </template>
  <% end %>
</div>
