<% if item.is_a? NamedTuple(entry: Entry, percentage: Float64, grouped_count: Int32) %>
  <% grouped_count = item[:grouped_count] %>
  <% if grouped_count == 1 %>
    <% item = item[:entry] %>
  <% else %>
    <% item = item[:entry].book %>
  <% end %>
<% else %>
  <% grouped_count = 1 %>
<% end %>

<div class="item"
  <% if item.is_a? Entry %> 
    id="<%= item.id %>"
  <% end %>>

  <div class="acard
    <% if item.is_a? Entry && item.err_msg.nil? %>
      <%= "is_entry" %>
    <% end %>
    " 
    <% if item.is_a? Entry %>
      <% if item.err_msg %>
        onclick="location='<%= base_url %>reader/<%= item.book.id %>/<%= item.id %>'"
      <% else %>
        data-encoded-path="<%= item.encoded_path %>"
        data-pages="<%= item.pages %>"
        data-progress="<%= (progress * 100).round(1) %>"
        data-encoded-book-title="<%= item.book.encoded_display_name %>"
        data-encoded-title="<%= item.encoded_display_name %>"
        data-book-id="<%= item.book.id %>"
        data-id="<%= item.id %>"
      <% end %>
    <% else %>
      onclick="location='<%= base_url %>book/<%= item.id %>'" 
    <% end %>>

    <div class="uk-card uk-card-default" x-data="{selected: false, hover: false, disabled: true, selecting: false}" :class="{selected: selected}" @count.window="selecting = $event.detail.count > 0"
      <% if page == "title" && item.is_a?(Entry) && item.err_msg.nil? %>
        x-init="disabled = false"
      <% end %>>
      <div class="uk-card-media-top uk-inline" @mouseenter="hover = true" @mouseleave="hover = false">
        <img data-src="<%= item.cover_url %>" width="100%" height="100%" alt="" uk-img
        <% if item.is_a? Entry && item.err_msg %>
          class="grayscale"
        <% end %>>
        <div class="uk-overlay-primary uk-position-cover" x-show="!disabled && (selected || hover)">
          <div class="uk-height-1-1 uk-width-1-1" x-show="selecting" @click.stop="selected = !selected; $dispatch(selected ? 'add' : 'remove')"></div>
          <div class="uk-position-center">
            <span class="fas fa-check-circle fa-3x" @click.stop="selected = !selected; $dispatch(selected ? 'add' : 'remove')" :style="`color:${selected && 'orange'};`"></span>
          </div>
        </div>
      </div>

      <div class="uk-card-body">
        <% unless progress < 0 || progress > 100 || progress.nan? %>
          <div class="uk-card-badge label"><%= (progress * 100).round(1) %>%</div>
        <% end %>

        <h3 class="uk-card-title break-word
          <% if page == "home" && item.is_a? Entry %>
            <%= "uk-margin-remove-bottom" %>
          <% end %>
          " data-title="<%= HTML.escape(item.display_name) %>"><%= HTML.escape(item.display_name) %>
        </h3>
        <% if page == "home" && item.is_a? Entry %>
          <a class="uk-card-title break-word uk-margin-remove-top uk-text-meta uk-display-inline-block no-modal" data-title="<%= HTML.escape(item.book.display_name) %>" href="<%= base_url %>book/<%= item.book.id %>"><%= HTML.escape(item.book.display_name) %></a>
        <% end %>
        <% if item.is_a? Entry %>
          <% if item.err_msg %>
            <p class="uk-text-meta uk-margin-remove-bottom">Error <span uk-icon="info"></span></p>
            <div uk-dropdown><%= item.err_msg %></div>
          <% else %>
            <p class="uk-text-meta"><%= item.pages %> pages</p>
          <% end %>
        <% end %>
        <% if item.is_a? Title %>
          <% if grouped_count == 1 %>
            <p class="uk-text-meta"><%= item.content_label %></p>
          <% else %>
            <p class="uk-text-meta"><%= grouped_count %> new entries</p>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>
</div>
