component AvatarGroup {
  outputs {
    overflow(payload: { sourceEvent?: Event; value: string | number | boolean | null | object; [key: string]: string | number | boolean | null | object } | string | number | boolean | null | object[])
  }

  props {
    items: unknown[] = []
    size: string = "md"
    color: string = "primary"
    variant: string = "solid"
    shape: string = "circle"
    layout: string = "stack"
    maxVisible: number = 4
    columns: number = 3
    borderColor: string = ""
    showTooltips: boolean = true
    overflowLabel: string = "Show remaining members"
    class: string = ""

}

  state overflowOpen: boolean = false

  functions {
    shared function visibleMembers() {
      return items.slice(0, Number(maxVisible))
    }

    shared function hiddenMembers() {
      return items.slice(Number(maxVisible))
    }

    client function toggleOverflow() {
      overflowOpen = !overflowOpen
      output.overflow({
        component: "AvatarGroup",
        open: overflowOpen,
        hiddenCount: hiddenMembers().length
      })
    }
  }

  view {
    <div
      {...attrs}
      class="wire-component wire-next--avatar-group {class}"
      data-wrn-avatar-group
      data-layout="{layout}"
      data-shape="{shape}"
      data-size="{size}"
      style="--wire-avatar-group-columns:{columns}; --wire-avatar-group-ring:{borderColor || 'var(--wire-color-bg)'}"
      role="group"
      aria-label="Avatar group"
    >
      <div
        class="wire-next__avatar-group-members"
      >
        {#each visibleMembers() as item}
          <span
            class="wire-next__avatar-group-member"
            data-size="{item.size || size}"
            data-shape="{item.shape || shape}"
            data-color="{item.color || color}"
            data-variant="{item.variant || variant}"
            tabindex="{showTooltips && (item.tooltip || item.name) ? '0' : '-1'}"
            aria-label="{item.name || item.alt || item.initials || 'Group member'}"
          >
            <span
              class="wire-next__avatar-group-avatar"
            >
              {#if item.src}
                <img
                  src="{item.src}"
                  alt="{item.alt || item.name || ''}"
                  loading="lazy"
                />
              {:else if item.initials}
                <span
                  aria-hidden="true"
                >
                  {item.initials}
                </span>
              {:else}
                <span
                  class="icon-[lucide--user-round]"
                  aria-hidden="true"
                >
                </span>
              {/if}
            </span>

            {#if showTooltips && (item.tooltip || item.name)}
              <span
                class="wire-next__avatar-group-tooltip"
                role="tooltip"
              >
                {item.tooltip || item.name}
              </span>
            {/if}
          </span>
        {/each}

        {#if hiddenMembers().length > 0}
          <span
            class="wire-next__avatar-group-overflow"
          >
            <button
              type="button"
              class="wire-next__avatar-group-overflow-button"
              aria-label="{overflowLabel}"
              aria-expanded="{overflowOpen ? 'true' : 'false'}"
              @click="toggleOverflow(event)"
            >
              +{hiddenMembers().length}
            </button>

            <span
              class="wire-next__avatar-group-menu"
              role="menu"
              data-show="overflowOpen"
              aria-hidden="{overflowOpen ? 'false' : 'true'}"
            >
              {#each hiddenMembers() as item}
                <span
                  class="wire-next__avatar-group-menu-item"
                  role="menuitem"
                >
                  <span
                    class="wire-next__avatar-group-menu-avatar"
                  >
                    {#if item.src}
                      <img
                        src="{item.src}"
                        alt=""
                        loading="lazy"
                      />
                    {:else}
                      <span>{item.initials || '?'}</span>
                    {/if}
                  </span>
                  <span>{item.name || item.alt || item.initials || 'Team member'}</span>
                </span>
              {/each}
            </span>
          </span>
        {/if}
      </div>
    </div>
  }

  style {
    /* --- Avatar group -------------------------------------------------------- */
    .wire-next--avatar-group {
      --wire-avatar-group-size: 3rem;
      position: relative;
      display: inline-flex;
      width: fit-content;
      max-width: 100%;
    }

    .wire-next--avatar-group[data-size="xs"] {
      --wire-avatar-group-size: 1.75rem;
    }

    .wire-next--avatar-group[data-size="sm"] {
      --wire-avatar-group-size: 2.25rem;
    }

    .wire-next--avatar-group[data-size="md"],
    .wire-next--avatar-group[data-size="default"] {
      --wire-avatar-group-size: 3rem;
    }

    .wire-next--avatar-group[data-size="lg"] {
      --wire-avatar-group-size: 4rem;
    }

    .wire-next--avatar-group[data-size="xl"] {
      --wire-avatar-group-size: 5rem;
    }

    .wire-next__avatar-group-members {
      display: flex;
      align-items: center;
    }

    .wire-next--avatar-group[data-layout="stack"] .wire-next__avatar-group-member,
    .wire-next--avatar-group[data-layout="stack"] .wire-next__avatar-group-overflow {
      margin-left: calc(var(--wire-avatar-group-size) * -0.22);
    }

    .wire-next--avatar-group[data-layout="stack"] :first-child {
      margin-left: 0;
    }

    .wire-next--avatar-group[data-layout="grid"] .wire-next__avatar-group-members {
      display: grid;
      grid-template-columns: repeat(var(--wire-avatar-group-columns), var(--wire-avatar-group-size));
      gap: 0.65rem;
    }

    .wire-next__avatar-group-member,
    .wire-next__avatar-group-overflow {
      --wire-avatar-group-color: var(--wire-color-primary);
      position: relative;
      display: inline-grid;
      width: var(--wire-avatar-group-size);
      height: var(--wire-avatar-group-size);
      flex: none;
      place-items: center;
      border-radius: 999px;
      outline: none;
      transition:
        z-index var(--wire-motion-fast),
        transform var(--wire-motion-base) var(--wire-ease-emphasized);
    }

    .wire-next__avatar-group-member[data-color="secondary"] {
      --wire-avatar-group-color: #737373;
    }

    .wire-next__avatar-group-member[data-color="success"] {
      --wire-avatar-group-color: var(--wire-color-success);
    }

    .wire-next__avatar-group-member[data-color="info"] {
      --wire-avatar-group-color: var(--wire-color-info);
    }

    .wire-next__avatar-group-member[data-color="warning"] {
      --wire-avatar-group-color: var(--wire-color-warning);
    }

    .wire-next__avatar-group-member[data-color="danger"] {
      --wire-avatar-group-color: var(--wire-color-danger);
    }

    .wire-next__avatar-group-member[data-shape="rounded"] {
      border-radius: calc(var(--wire-radius-sm, 0.5rem) * 0.72);
    }

    .wire-next__avatar-group-avatar,
    .wire-next__avatar-group-overflow-button {
      display: grid;
      width: 100%;
      height: 100%;
      overflow: hidden;
      place-items: center;
      color: white;
      border: 2px solid var(--wire-avatar-group-ring);
      border-radius: inherit;
      background: var(--wire-avatar-group-color, var(--wire-color-primary));
      box-shadow: 0 5px 14px rgb(0 0 0 / 0.16);
      font: inherit;
      font-size: calc(var(--wire-avatar-group-size) * 0.3);
      font-weight: 800;
    }

    .wire-next__avatar-group-avatar img,
    .wire-next__avatar-group-menu-avatar img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .wire-next__avatar-group-overflow-button {
      cursor: pointer;
      color: var(--wire-color-text);
      background: var(--wire-color-surface-2);
    }

    .wire-next__avatar-group-member:hover,
    .wire-next__avatar-group-member:focus-visible,
    .wire-next__avatar-group-overflow:focus-within {
      z-index: 5;
      transform: translateY(-3px);
    }

    .wire-next__avatar-group-member:focus-visible,
    .wire-next__avatar-group-overflow-button:focus-visible {
      outline: 2px solid var(--wire-color-primary);
      outline-offset: 3px;
    }

    .wire-next__avatar-group-tooltip {
      position: absolute;
      left: 50%;
      bottom: calc(100% + 0.65rem);
      z-index: 20;
      width: max-content;
      max-width: 14rem;
      padding: 0.4rem 0.6rem;
      color: var(--wire-color-bg);
      border-radius: 0.4rem;
      background: var(--wire-color-text);
      box-shadow: 0 8px 22px rgb(0 0 0 / 0.22);
      font-size: 0.75rem;
      font-weight: 650;
      pointer-events: none;
      opacity: 0;
      transform: translate(-50%, 0.25rem);
      transition:
        opacity var(--wire-motion-fast),
        transform var(--wire-motion-fast);
    }

    .wire-next__avatar-group-member:hover .wire-next__avatar-group-tooltip,
    .wire-next__avatar-group-member:focus-visible .wire-next__avatar-group-tooltip {
      opacity: 1;
      transform: translate(-50%, 0);
    }

    .wire-next__avatar-group-menu {
      position: absolute;
      top: calc(100% + 0.65rem);
      right: 0;
      z-index: 30;
      display: grid;
      width: max-content;
      min-width: 12rem;
      gap: 0.25rem;
      padding: 0.45rem;
      color: var(--wire-color-text);
      border: 1px solid var(--wire-color-border);
      border-radius: var(--wire-radius-sm, 0.5rem);
      background: var(--wire-color-surface);
      box-shadow: 0 16px 38px rgb(0 0 0 / 0.22);
    }

    .wire-next__avatar-group-menu-item {
      display: flex;
      align-items: center;
      gap: 0.6rem;
      padding: 0.45rem 0.55rem;
      border-radius: 0.4rem;
      font-size: 0.82rem;
    }

    .wire-next__avatar-group-menu-item:hover {
      background: var(--wire-color-surface-2);
    }

    .wire-next__avatar-group-menu-avatar {
      display: grid;
      width: 1.75rem;
      height: 1.75rem;
      overflow: hidden;
      flex: none;
      place-items: center;
      border-radius: 999px;
      background: var(--wire-color-surface-2);
      font-size: 0.65rem;
      font-weight: 800;
    }

    /* Shared component foundation. */

        .wire-component {
          --wire-component-color: var(--wire-color-primary);
          --wire-component-scale: 1;
          margin: 0;
          color: var(--wire-color-text);
          font-family: var(--wrn-font-sans, inherit);
        }

        .wire-component--color-primary {
          --wire-component-color: var(--wire-color-primary);
        }

        .wire-component--color-secondary {
          --wire-component-color: var(--wire-color-secondary);
        }

        .wire-component--color-success {
          --wire-component-color: var(--wire-color-success);
        }

        .wire-component--color-warning {
          --wire-component-color: var(--wire-color-warning);
        }

        .wire-component--color-danger {
          --wire-component-color: var(--wire-color-danger);
        }

        .wire-component--color-info {
          --wire-component-color: var(--wire-color-info);
        }

        .wire-component--size-xs {
          --wire-component-scale: 0.78;
        }

        .wire-component--size-sm {
          --wire-component-scale: 0.88;
        }

        .wire-component--size-default,
        .wire-component--size-md {
          --wire-component-scale: 1;
        }

        .wire-component--size-lg {
          --wire-component-scale: 1.14;
        }

        .wire-component--size-xl {
          --wire-component-scale: 1.28;
        }

        .wire-component:not(.wire-btn) {
          font-size: calc(1em * var(--wire-component-scale));
        }

        .wire-component :is(a, button, input, select, textarea):focus-visible {
          outline-color: var(--wire-component-color);
        }

        .wire-component p {
          margin: 0;
          color: var(--wire-color-muted);
          line-height: 1.6;
        }
  }
}
