/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as customElement } from "../../chunks/runtime.js";
import { repeat } from "lit/directives/repeat.js";
import { css, nothing, html } from "lit";
import { LitElement, createEvent, safeClassMap } from "@arcgis/lumina";
import { createRef, ref } from "lit/directives/ref.js";
import { useDirection } from "@arcgis/lumina/controllers";
import { w as whenAnimationDone, a as slotChangeGetAssignedElements, f as focusElementInGroup } from "../../chunks/dom.js";
import { g as guid } from "../../chunks/guid.js";
import { c as createObserver } from "../../chunks/observers.js";
import { b as breakpoints } from "../../chunks/responsive.js";
import { n as numberStringFormatter } from "../../chunks/locale.js";
import { g as getRoundRobinIndex } from "../../chunks/array.js";
import { u as useT9n } from "../../chunks/useT9n.js";
import { u as useSetFocus } from "../../chunks/useSetFocus.js";
import { u as useInteractive } from "../../chunks/useInteractive.js";
const DURATION = 6e3;
const CSS = {
  container: "container",
  containerOverlaid: "container--overlaid",
  containerEdged: "container--edged",
  itemContainer: "item-container",
  itemContainerForward: "item-container--forward",
  itemContainerBackward: "item-container--backward",
  pagination: "pagination",
  paginationAriaLive: "pagination-aria-live",
  paginationItems: "pagination-items",
  paginationItem: "pagination-item",
  paginationItemIndividual: "pagination-item--individual",
  paginationItemVisible: "pagination-item--visible",
  paginationItemOutOfRange: "pagination-item--out-of-range",
  paginationItemSelected: "pagination-item--selected",
  paginationItemRangeEdge: "pagination-item--range-edge",
  pageNext: "page-next",
  pagePrevious: "page-previous",
  autoplayControl: "autoplay-control",
  autoplayProgress: "autoplay-progress"
};
const ICONS = {
  chevronLeft: "chevron-left",
  chevronRight: "chevron-right",
  inactive: "bullet-point",
  active: "bullet-point-large",
  pause: "pause-f",
  play: "play-f"
};
const centerItemsByBreakpoint = {
  medium: 7,
  small: 5,
  xsmall: 3,
  xxsmall: 1
};
const idPrefix = "calcite-carousel-container";
const IDS = {
  host: (id) => `${idPrefix}-${id}`
};
const styles = css`:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:flex;inline-size:100%;--calcite-internal-carousel-pagination-space: 1.5rem;--calcite-internal-carousel-pagination-space-wide: 3.5rem;--calcite-internal-carousel-pagination-background-color: var( --calcite-carousel-pagination-background-color, transparent );--calcite-internal-carousel-pagination-background-color-hover: var( --calcite-carousel-pagination-background-color-hover, transparent );--calcite-internal-carousel-pagination-background-color-press: var( --calcite-carousel-pagination-background-color-press, transparent );--calcite-internal-carousel-pagination-background-color-selected: var( --calcite-carousel-pagination-background-color-selected, transparent );--calcite-internal-carousel-pagination-overlay-background-color: var( --calcite-carousel-pagination-background-color, var(--calcite-color-foreground-1) );--calcite-internal-carousel-pagination-overlay-background-color-hover: var( --calcite-carousel-pagination-background-color-hover, var(--calcite-color-foreground-2) );--calcite-internal-carousel-pagination-overlay-background-color-active: var( --calcite-carousel-pagination-background-color-press, var(--calcite-color-foreground-2) );--calcite-internal-carousel-pagination-overlay-background-color-selected: var( --calcite-carousel-pagination-background-color-selected, var(--calcite-color-foreground-1) );--calcite-internal-carousel-pagination-icon-color-hover: var( --calcite-carousel-pagination-icon-color-hover, var(--calcite-color-text-1) );--calcite-internal-carousel-pagination-icon-color: var( --calcite-carousel-pagination-icon-color, var(--calcite-color-border-1) );--calcite-internal-carousel-pagination-icon-color-selected: var( --calcite-carousel-pagination-icon-color-selected, var(--calcite-color-brand) );--calcite-internal-carousel-control-icon-color-hover: var( --calcite-carousel-control-icon-color-hover, var(--calcite-internal-carousel-pagination-icon-color-hover) );--calcite-internal-carousel-control-icon-color: var( --calcite-carousel-control-icon-color, var(--calcite-carousel-pagination-icon-color, var(--calcite-color-text-3)) );--calcite-internal-carousel-autoplay-progress-background-color: var( --calcite-carousel-autoplay-progress-background-color, var(--calcite-color-border-3) );--calcite-internal-carousel-autoplay-progress-fill-color: var( --calcite-carousel-autoplay-progress-fill-color, var(--calcite-color-brand) );--calcite-internal-carousel-autoplay-control-color: var( --calcite-carousel-pagination-icon-color, var(--calcite-color-text-3) )}.container{position:relative;display:flex;inline-size:100%;flex-direction:column;overflow:hidden;font-size:var(--calcite-font-size-relative-base);line-height:var(--calcite-font-line-height-base);color:var(--calcite-color-text-2);outline-color:transparent}.container:focus{outline:var(--calcite-border-width-md) solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(calc(-1 * var(--calcite-spacing-base)) * calc(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}.container--edged:not(.container--overlaid){padding-inline:var(--calcite-internal-carousel-pagination-space-wide);inline-size:calc(100% - var(--calcite-internal-carousel-pagination-space-wide) * 2)}.item-container{display:flex;flex:1 1 auto;align-items:flex-start;justify-content:center;overflow:auto;padding:.25rem;animation-name:none;animation-duration:var(--calcite-animation-timing)}.container--overlaid .item-container{padding:0}.item-container--forward{animation-name:item-forward}.item-container--backward{animation-name:item-backward}calcite-carousel-item:not([selected]){opacity:0}.pagination-aria-live{position:absolute;inline-size:1px;block-size:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0}.pagination{margin:.75rem;display:flex;flex-direction:row;align-items:center;justify-content:center;inline-size:auto}.pagination-items{display:flex;flex-direction:row;align-items:center}.container--overlaid .pagination{position:absolute}.pagination-item.page-next,.pagination-item.page-previous{color:var(--calcite-internal-carousel-control-icon-color);--calcite-icon-color: var(--calcite-internal-carousel-control-icon-color)}.pagination-item.page-next:hover,.pagination-item.page-previous:hover{color:var(--calcite-internal-carousel-control-icon-color-hover);--calcite-icon-color: var(--calcite-internal-carousel-control-icon-color-hover)}.container--edged .page-next,.container--edged .page-previous{block-size:3rem;inline-size:3rem;position:absolute;inset-block-start:50%;transform:translateY(-50%)}.container--edged .page-next{inset-inline-end:0}.container--edged .page-previous{inset-inline-start:0}.container--overlaid .pagination{inset-block-start:unset;inset-block-end:0;inset-inline:0}:host([pagination-position=top]) .container--overlaid .pagination{inset-block-start:0;inset-block-end:unset}.pagination-item.autoplay-control{position:relative;color:var(--calcite-internal-carousel-autoplay-control-color);--calcite-progress-fill-color: var(--calcite-internal-carousel-autoplay-progress-fill-color);--calcite-progress-background-color: var(--calcite-internal-carousel-autoplay-progress-background-color)}.autoplay-control:focus .autoplay-progress{inset-block-end:4px;inset-inline:2px;inline-size:calc(100% - 4px)}.autoplay-progress{position:absolute;inset-block-end:2px;inset-inline:0;inline-size:100%}.pagination-item{margin:0;block-size:2rem;inline-size:2rem;cursor:pointer;align-items:center;border-style:none;background-color:transparent;outline-color:transparent;transition-property:background-color,block-size,border-color,box-shadow,color,inset-block-end,inset-block-start,inset-inline-end,inset-inline-start,inset-size,opacity,outline-color,transform;transition-duration:var(--calcite-animation-timing);transition-timing-function:ease-in-out;-webkit-appearance:none;display:flex;align-content:center;justify-content:center;background-color:var(--calcite-internal-carousel-pagination-background-color);color:var(--calcite-internal-carousel-pagination-icon-color)}.pagination-item:hover{background-color:var(--calcite-internal-carousel-pagination-background-color-hover);color:var(--calcite-internal-carousel-pagination-icon-color-hover)}.pagination-item:focus{background-color:var(--calcite-internal-carousel-pagination-background-color-press);outline:var(--calcite-border-width-md) solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(calc(-1 * var(--calcite-spacing-base)) * calc(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}.pagination-item:active{background-color:var(--calcite-internal-carousel-pagination-background-color-press);color:var(--calcite-internal-carousel-pagination-icon-color-hover)}.pagination-item calcite-icon{color:inherit;pointer-events:none}.pagination-item.pagination-item--selected{background-color:var(--calcite-internal-carousel-pagination-background-color-selected);color:var(--calcite-internal-carousel-pagination-icon-color-selected)}.pagination-item--individual{pointer-events:none;inline-size:0px;padding:0;opacity:0;visibility:hidden;transition:var(--calcite-animation-timing) ease-in-out inline-size,var(--calcite-animation-timing) ease-in-out padding,var(--calcite-animation-timing) ease-in-out opacity}.pagination-item--individual.pagination-item--visible{pointer-events:auto;inline-size:2rem;opacity:1;visibility:visible}.pagination-item--range-edge calcite-icon{scale:.75;transition:var(--calcite-animation-timing) ease-in-out scale}.container--overlaid .pagination-item{background-color:var(--calcite-internal-carousel-pagination-overlay-background-color)}.container--overlaid .pagination-item:hover{background-color:var(--calcite-internal-carousel-pagination-overlay-background-color-hover)}.container--overlaid .pagination-item:focus{background-color:var(--calcite-internal-carousel-pagination-overlay-background-color-active)}.container--overlaid .pagination-item:active{background-color:var(--calcite-internal-carousel-pagination-overlay-background-color-active)}.container--overlaid .pagination-item.pagination-item--selected{background-color:var(--calcite-internal-carousel-pagination-overlay-background-color-selected);color:var(--calcite-internal-carousel-pagination-icon-color-selected)}@keyframes item-forward{0%{transform:translate3d(100px,0,0)}to{transform:translateZ(0)}}@keyframes item-backward{0%{transform:translate3d(-100px,0,0)}to{transform:translateZ(0)}}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.interaction-container{display:contents}:host([hidden]){display:none}[hidden]{display:none}`;
class Carousel extends LitElement {
  constructor() {
    super(...arguments);
    this.autoplayHandler = () => {
      this.clearIntervals();
      this.slideDurationInterval = setInterval(this.timer, this.autoplayDuration / 100);
    };
    this.containerRef = createRef();
    this.containerId = IDS.host(guid());
    this.direction = useDirection();
    this.itemContainerRef = createRef();
    this.resizeHandler = ({ contentRect: { width } }) => {
      this.setMaxItemsToBreakpoint(width);
    };
    this.resizeObserver = createObserver("resize", (entries) => entries.forEach(this.resizeHandler));
    this.tabListRef = createRef();
    this.timer = () => {
      let time = this.slideDurationRemaining;
      const notSuspended = !this.suspendedDueToFocus && !this.suspendedDueToHover || this.userPreventsSuspend;
      if (notSuspended) {
        if (time <= 0.01) {
          time = 1;
          this.itemDirection = "forward";
          this.nextItem(false);
        } else {
          time = time - 0.01;
        }
      }
      if (time > 0) {
        this.slideDurationRemaining = time;
      }
    };
    this.messages = useT9n();
    this.focusSetter = useSetFocus()(this);
    this.interactiveContainer = useInteractive(this);
    this.itemDirection = "standby";
    this.hasMultiple = false;
    this.items = [];
    this.maxItems = centerItemsByBreakpoint.xxsmall;
    this.playing = false;
    this.selectedIndex = 0;
    this.slideDurationRemaining = 1;
    this.suspendedDueToFocus = false;
    this.suspendedDueToHover = false;
    this.suspendedSlideDurationRemaining = 1;
    this.userPreventsSuspend = false;
    this.arrowType = "inline";
    this.autoplay = false;
    this.autoplayDuration = DURATION;
    this.controlOverlay = false;
    this.disabled = false;
    this.paginationDisabled = false;
    this.paginationPosition = "bottom";
    this.calciteCarouselChange = createEvent({ cancelable: false });
    this.calciteCarouselPause = createEvent({ cancelable: false });
    this.calciteCarouselPlay = createEvent({ cancelable: false });
    this.calciteCarouselResume = createEvent({ cancelable: false });
    this.calciteCarouselStop = createEvent({ cancelable: false });
  }
  static {
    this.properties = { itemDirection: [16, {}, { state: true }], hasMultiple: [16, {}, { state: true }], items: [16, {}, { state: true }], maxItems: [16, {}, { state: true }], playing: [16, {}, { state: true }], selectedIndex: [16, {}, { state: true }], slideDurationRemaining: [16, {}, { state: true }], suspendedDueToFocus: [16, {}, { state: true }], suspendedDueToHover: [16, {}, { state: true }], suspendedSlideDurationRemaining: [16, {}, { state: true }], userPreventsSuspend: [16, {}, { state: true }], arrowType: [3, {}, { reflect: true }], autoplay: [3, {}, { reflect: true }], autoplayDuration: [11, {}, { type: Number, reflect: true }], controlOverlay: [7, {}, { reflect: true, type: Boolean }], disabled: [7, {}, { reflect: true, type: Boolean }], label: 1, messageOverrides: [0, {}, { attribute: false }], paginationDisabled: [5, {}, { type: Boolean }], paginationPosition: [3, {}, { reflect: true }], paused: [5, {}, { type: Boolean }], selectedItem: [0, {}, { attribute: false }] };
  }
  static {
    this.styles = styles;
  }
  async play() {
    if (this.playing || !this.hasMultiple || this.autoplay !== "" && this.autoplay !== true && this.autoplay !== "paused") {
      return;
    }
    this.handlePlay(true);
  }
  async setFocus(options) {
    return this.focusSetter(() => this.containerRef.value, options);
  }
  async stop() {
    if (!this.playing) {
      return;
    }
    this.handlePause(true);
  }
  connectedCallback() {
    super.connectedCallback();
    this.resizeObserver?.observe(this.el);
  }
  async load() {
    if ((this.autoplay === "" || this.autoplay) && this.autoplay !== "paused") {
      this.handlePlay(false);
    } else if (this.autoplay === "paused") {
      this.paused = true;
    }
  }
  willUpdate(changes) {
    if (this.hasUpdated && !this.hasMultiple) {
      this.handlePause(false);
    }
    if (changes.has("autoplay") && this.hasUpdated) {
      this.autoplayWatcher(this.autoplay);
    }
    if (changes.has("itemDirection") && (this.hasUpdated || this.itemDirection !== "standby")) {
      this.itemDirectionWatcher(this.itemDirection);
    }
    if (changes.has("playing") && (this.hasUpdated || this.playing !== false)) {
      this.paused = !this.playing;
    }
    if (changes.has("suspendedDueToFocus") && (this.hasUpdated || this.suspendedDueToFocus !== false) || changes.has("suspendedDueToHover") && (this.hasUpdated || this.suspendedDueToHover !== false)) {
      this.suspendWatcher();
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    this.clearIntervals();
    this.resizeObserver?.disconnect();
  }
  autoplayWatcher(autoplay) {
    if (!autoplay) {
      this.handlePause(false);
    }
  }
  async itemDirectionWatcher(itemDirection) {
    if (itemDirection === "standby" || !this.itemContainerRef.value) {
      return;
    }
    await whenAnimationDone(this.itemContainerRef.value, itemDirection === "forward" ? "item-forward" : "item-backward");
    this.itemDirection = "standby";
  }
  suspendWatcher() {
    if (!this.suspendedDueToFocus && !this.suspendedDueToHover) {
      this.suspendEnd();
    } else {
      this.suspendStart();
    }
  }
  setMaxItemsToBreakpoint(width) {
    if (!width) {
      return;
    }
    if (width >= breakpoints.width.small) {
      this.maxItems = centerItemsByBreakpoint.medium;
      return;
    }
    if (width >= breakpoints.width.xsmall) {
      this.maxItems = centerItemsByBreakpoint.small;
      return;
    }
    if (width >= breakpoints.width.xxsmall) {
      this.maxItems = centerItemsByBreakpoint.xsmall;
      return;
    }
    this.maxItems = centerItemsByBreakpoint.xxsmall;
  }
  clearIntervals() {
    clearInterval(this.slideDurationInterval);
    clearInterval(this.slideInterval);
  }
  nextItem(emit) {
    if (this.playing && emit) {
      this.playing = false;
    }
    const nextIndex = getRoundRobinIndex(this.selectedIndex + 1, this.items.length);
    this.setSelectedItem(nextIndex, emit);
  }
  previousItem() {
    this.playing = false;
    const prevIndex = getRoundRobinIndex(Math.max(this.selectedIndex - 1, -1), this.items.length);
    this.setSelectedItem(prevIndex, true);
  }
  handlePlay(emit) {
    this.playing = true;
    this.autoplayHandler();
    this.slideInterval = setInterval(this.autoplayHandler, this.autoplayDuration);
    if (emit) {
      this.calciteCarouselPlay.emit();
    }
  }
  handlePause(emit) {
    this.playing = false;
    this.clearIntervals();
    this.slideDurationRemaining = 1;
    this.suspendedSlideDurationRemaining = 1;
    if (emit) {
      this.calciteCarouselStop.emit();
    }
  }
  suspendStart() {
    this.suspendedSlideDurationRemaining = this.slideDurationRemaining;
  }
  suspendEnd() {
    this.slideDurationRemaining = this.suspendedSlideDurationRemaining;
  }
  handleSlotChange(event) {
    const items = slotChangeGetAssignedElements(event);
    if (items.length < 1) {
      return;
    }
    const activeItemIndex = items.findIndex((item) => item.selected);
    const requestedSelectedIndex = activeItemIndex > -1 ? activeItemIndex : 0;
    this.items = items;
    this.hasMultiple = items.length > 1;
    this.setSelectedItem(requestedSelectedIndex, false);
  }
  setSelectedItem(requestedIndex, emit) {
    const previousSelected = this.selectedIndex;
    this.items.forEach((el, index) => {
      const match = requestedIndex === index;
      el.selected = match;
      if (match) {
        this.selectedItem = el;
        this.selectedIndex = index;
      }
    });
    if (emit) {
      this.playing = false;
      if (previousSelected !== this.selectedIndex) {
        this.calciteCarouselChange.emit();
      }
    }
  }
  handleArrowClick(event) {
    const direction = event.target.dataset.itemDirection;
    if (this.playing) {
      this.handlePause(true);
    }
    if (direction === "next") {
      this.itemDirection = "forward";
      this.nextItem(true);
    } else if (direction === "previous") {
      this.itemDirection = "backward";
      this.previousItem();
    }
  }
  handleItemSelection(event) {
    const item = event.currentTarget;
    const requestedPosition = parseInt(item.dataset.index);
    if (requestedPosition === this.selectedIndex) {
      return;
    }
    if (this.playing) {
      this.handlePause(true);
    }
    this.itemDirection = requestedPosition > this.selectedIndex ? "forward" : "backward";
    this.setSelectedItem(requestedPosition, true);
  }
  toggleRotation() {
    this.userPreventsSuspend = true;
    if (this.playing) {
      this.handlePause(true);
    } else {
      this.handlePlay(true);
    }
  }
  handleFocusIn() {
    const isPlaying = this.playing;
    if (isPlaying) {
      this.suspendedDueToFocus = true;
    }
    if ((!this.suspendedDueToFocus || !this.suspendedDueToHover) && isPlaying) {
      this.calciteCarouselPause.emit();
    }
  }
  handleMouseIn() {
    const isPlaying = this.playing;
    if (isPlaying) {
      this.suspendedDueToHover = true;
    }
    if ((!this.suspendedDueToFocus || !this.suspendedDueToHover) && isPlaying) {
      this.calciteCarouselPause.emit();
    }
  }
  handleMouseOut(event) {
    const leavingComponent = !this.el.contains(event.relatedTarget);
    const isPlaying = this.playing;
    if (leavingComponent && isPlaying) {
      this.suspendedDueToHover = false;
    }
    if (leavingComponent && isPlaying && !this.suspendedDueToFocus) {
      this.userPreventsSuspend = false;
      this.calciteCarouselResume.emit();
    }
  }
  handleFocusOut(event) {
    const leavingComponent = !event.composedPath().includes(event.relatedTarget);
    const isPlaying = this.playing;
    if (leavingComponent && isPlaying) {
      this.suspendedDueToFocus = false;
    }
    if (leavingComponent && isPlaying && !this.suspendedDueToHover) {
      this.userPreventsSuspend = false;
      this.calciteCarouselResume.emit();
    }
  }
  containerKeyDownHandler(event) {
    if (event.target !== this.containerRef.value) {
      return;
    }
    const lastItem = this.items.length - 1;
    switch (event.key) {
      case " ":
      case "Enter":
        event.preventDefault();
        if (this.autoplay === "" || this.autoplay === true || this.autoplay === "paused") {
          this.toggleRotation();
        }
        break;
      case "ArrowRight":
        event.preventDefault();
        if (!this.hasMultiple) {
          return;
        }
        this.itemDirection = "forward";
        this.nextItem(true);
        break;
      case "ArrowLeft":
        event.preventDefault();
        if (!this.hasMultiple) {
          return;
        }
        this.itemDirection = "backward";
        this.previousItem();
        break;
      case "Home":
        event.preventDefault();
        if (this.selectedIndex === 0) {
          return;
        }
        this.itemDirection = "backward";
        this.setSelectedItem(0, true);
        break;
      case "End":
        event.preventDefault();
        if (this.selectedIndex === lastItem) {
          return;
        }
        this.itemDirection = "forward";
        this.setSelectedItem(lastItem, true);
        break;
    }
  }
  tabListKeyDownHandler(event) {
    const visiblePaginationEls = Array(...this.tabListRef.value.querySelectorAll(`button:not(.${CSS.paginationItemOutOfRange})`));
    const currentEl = event.target;
    switch (event.key) {
      case "ArrowRight":
        focusElementInGroup(visiblePaginationEls, currentEl, "next");
        break;
      case "ArrowLeft":
        focusElementInGroup(visiblePaginationEls, currentEl, "previous");
        break;
      case "Home":
        event.preventDefault();
        focusElementInGroup(visiblePaginationEls, currentEl, "first");
        break;
      case "End":
        event.preventDefault();
        focusElementInGroup(visiblePaginationEls, currentEl, "last");
        break;
    }
  }
  renderRotationControl() {
    const text = this.playing ? this.messages.pause : this.messages.play;
    const formattedValue = this.slideDurationRemaining * 100;
    return html`<button .ariaLabel=${text} class=${safeClassMap({
      [CSS.paginationItem]: true,
      [CSS.autoplayControl]: true
    })} @click=${this.toggleRotation} title=${text ?? nothing}><calcite-icon .icon=${this.playing ? ICONS.pause : ICONS.play} scale=s></calcite-icon>${this.playing && html`<calcite-progress class=${safeClassMap(CSS.autoplayProgress)} .label=${this.messages.carouselItemProgress} .value=${formattedValue}></calcite-progress>` || ""}</button>`;
  }
  renderPaginationArea() {
    return html`<div class=${safeClassMap({
      [CSS.pagination]: true,
      [CSS.containerOverlaid]: this.controlOverlay
    })} @keydown=${this.tabListKeyDownHandler} ${ref(this.tabListRef)}>${(this.playing || this.autoplay === "" || this.autoplay === true || this.autoplay === "paused") && this.hasMultiple && this.renderRotationControl() || ""}${this.arrowType === "inline" && this.hasMultiple && this.renderArrow("previous") || ""}${this.paginationDisabled ? this.renderPaginationAriaLive() : this.renderPaginationItems()}${this.arrowType === "inline" && this.hasMultiple && this.renderArrow("next") || ""}</div>`;
  }
  renderPaginationItems() {
    const { selectedIndex, maxItems, items, label, handleItemSelection } = this;
    return html`<div .ariaLabel=${label} class=${safeClassMap(CSS.paginationItems)} role=tablist>${repeat(items, (item) => item.id, (item, index) => {
      const itemCount = items.length;
      const match = index === selectedIndex;
      const first = index === 0;
      const last = index === itemCount - 1;
      const endRangeStart = itemCount - maxItems - 1;
      const inStartRange = selectedIndex < maxItems;
      const inEndRange = selectedIndex >= endRangeStart;
      const rangeStart = inStartRange ? 0 : selectedIndex - Math.floor(maxItems / 2);
      const rangeEnd = inEndRange ? itemCount : rangeStart + maxItems;
      const low = inStartRange ? 0 : inEndRange ? endRangeStart : rangeStart;
      const high = inStartRange ? maxItems + 1 : rangeEnd;
      const isEdge = !first && !last && !match && (index === low - 1 || index === high);
      const visible = match || index <= high && index >= low - 1;
      const overflowActive = itemCount - 1 <= maxItems;
      const icon = match ? ICONS.active : ICONS.inactive;
      return html`<button aria-controls=${(!match ? item.id : void 0) ?? nothing} .ariaSelected=${match} class=${safeClassMap({
        [CSS.paginationItem]: true,
        [CSS.paginationItemIndividual]: true,
        [CSS.paginationItemSelected]: match,
        [CSS.paginationItemRangeEdge]: itemCount - 1 > maxItems && isEdge,
        [CSS.paginationItemOutOfRange]: !(overflowActive || visible),
        [CSS.paginationItemVisible]: overflowActive || visible
      })} data-index=${index ?? nothing} @click=${handleItemSelection} role=tab title=${item.label ?? nothing}><calcite-icon .icon=${icon} scale=l></calcite-icon></button>`;
    })}</div>`;
  }
  renderPaginationAriaLive() {
    const { messages, messages: { _lang: effectiveLocale }, selectedIndex, items } = this;
    if (messages._loading) {
      return;
    }
    numberStringFormatter.numberFormatOptions = {
      locale: effectiveLocale
    };
    return html`<div aria-live=off class=${safeClassMap(CSS.paginationAriaLive)} role=status>${messages.paginationStatus.replace("{current}", numberStringFormatter.localize(`${selectedIndex + 1}`)).replace("{total}", numberStringFormatter.localize(`${items.length}`))}</div>`;
  }
  renderArrow(itemDirection) {
    const isPrev = itemDirection === "previous";
    const dir = this.direction;
    const scale = this.arrowType === "edge" ? "m" : "s";
    const css2 = isPrev ? CSS.pagePrevious : CSS.pageNext;
    const title = isPrev ? this.messages.previous : this.messages.next;
    const icon = isPrev ? ICONS.chevronLeft : ICONS.chevronRight;
    return html`<button aria-controls=${this.containerId ?? nothing} class=${safeClassMap({ [CSS.paginationItem]: true, [css2]: true })} data-item-direction=${itemDirection ?? nothing} @click=${this.handleArrowClick} title=${title ?? nothing}><calcite-icon .flipRtl=${dir === "rtl"} .icon=${icon} .scale=${scale}></calcite-icon></button>`;
  }
  render() {
    const { itemDirection, paginationPosition } = this;
    const paginationArea = this.renderPaginationArea();
    const itemContainer = html`<section class=${safeClassMap({
      [CSS.itemContainer]: true,
      [CSS.itemContainerForward]: itemDirection === "forward",
      [CSS.itemContainerBackward]: itemDirection === "backward"
    })} id=${this.containerId ?? nothing} ${ref(this.itemContainerRef)}><slot @slotchange=${this.handleSlotChange}></slot></section>`;
    return this.interactiveContainer({ disabled: this.disabled, children: html`<div .ariaLabel=${this.label} .ariaLive=${this.playing ? "off" : "polite"} .ariaRoleDescription=${this.messages.carousel} class=${safeClassMap({
      [CSS.container]: true,
      [CSS.containerOverlaid]: this.controlOverlay,
      [CSS.containerEdged]: this.arrowType === "edge"
    })} @focusin=${this.handleFocusIn} @focusout=${this.handleFocusOut} @keydown=${this.containerKeyDownHandler} @mouseenter=${this.handleMouseIn} @mouseleave=${this.handleMouseOut} role=group tabindex=0 ${ref(this.containerRef)}>${paginationPosition === "top" ? paginationArea : itemContainer}${paginationPosition === "top" ? itemContainer : paginationArea}${this.arrowType === "edge" && this.hasMultiple && this.renderArrow("previous") || ""}${this.arrowType === "edge" && this.hasMultiple && this.renderArrow("next") || ""}</div>` });
  }
}
customElement("calcite-carousel", Carousel);
export {
  Carousel
};
