<ng-container>
  @if (root) {
    @if (item.hasDropDown) {
      <div class="layout-menuitem-root-text root-item-with-dropdown">
        <div class="root-item-label-container">
          <div>{{ item.label }}</div>
          @if (item.hasCreate) {
            <i
              class="pi pi-plus plus-icon"
              (click)="openDialog(item)"
            ></i>
          }
        </div>
        <i
          (click)="toggleExpand(key)"
          [ngClass]="{
          'pi': true,
          'pi-angle-up': isExpanded(key),
          'pi-angle-down': !isExpanded(key)
        }"
          class="expand-icon"
        ></i>
      </div>
    } @else {
      <div class="layout-menuitem-root-text root-item">
        <div>{{ item.label }}</div>
      </div>
    }
  }

  <div>
    <div
      class="menu-item-container"
      (click)="triggerLink()"
    >
      @if ((item.routerLink && !item.items)) {
        <a
          #link
          (click)="itemClick($event)"
          [ngClass]="item.class"
          class="menu-item-link"
          [routerLink]="item.routerLink"
          routerLinkActive="active-route"
          [routerLinkActiveOptions]="item.routerLinkActiveOptions || { paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }"
          [fragment]="item.fragment"
          [queryParamsHandling]="item.queryParamsHandling"
          [preserveFragment]="item.preserveFragment"
          [skipLocationChange]="item.skipLocationChange"
          [replaceUrl]="item.replaceUrl"
          [state]="item.state"
          [queryParams]="item.queryParams"
          [attr.target]="item.target"
          tabindex="0"
          pRipple
        >
          <i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
          <span class="layout-menuitem-text menu-item-text">
        {{ item.label }}
      </span>
        </a>
      }

      <span class="menu-item-end-content">
    @if (item.type === 'Library' || item.type === 'Shelf' || item.type === 'magicShelfItem') {
      @if ((item.type !== 'Library' || (admin || canManipulateLibrary)) && item.label !== 'Unshelved' && item.label !== 'Kobo') {
        <p-button
          (click)="entitymenu.toggle($event); $event.stopPropagation()"
          (mouseover)="hovered = true"
          (mouseleave)="hovered = false"
          [text]="true"
          [outlined]="false"
          [rounded]="true"
          [label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''"
          [icon]="hovered ? 'pi pi-ellipsis-v' : ''"
          class="entity-menu-button"
        ></p-button>
      }
      <p-menu #entitymenu [model]="item.menu" [popup]="true" appendTo="body"></p-menu>
    }

        @if (item.type === 'Library' && (!admin && !canManipulateLibrary) || item.type === 'All Books' || item.label === 'Unshelved' || item.label === 'Kobo') {
          <p class="book-count">
        {{ (item.bookCount$ | async)?.toString() || '0' }}
      </p>
        }
  </span>
    </div>

    <div [@children]="isExpanded(key) ? 'expanded' : 'collapsed'" class="submenu-container">
      @if (item.items) {
        <ul>
          @for (child of item.items; track child; let i = $index) {
            <li app-menuitem [item]="child" [index]="i" [parentKey]="key" [class]="child.badgeClass"></li>
          }
        </ul>
      }
    </div>
  </div>
</ng-container>
