import { html, nothing, type TemplateResult } from 'lit'; import { styleMap } from 'lit/directives/style-map.js'; import type { NuiType } from '../../types/nui-type.js'; import type { SwiperProps, SwiperSlideInterface } from './types.js'; export interface NuiSwiperViewState extends SwiperProps { currentIndex: number; totalSlides: number; wrapperStyle: Record; slideStyle: Record; } export interface NuiSwiperHandlers { onPrev: () => void; onNext: () => void; onGoTo: (index: number) => void; onSlotChange: (event: Event) => void; } function renderSlides( slides: SwiperSlideInterface[] | undefined, slideStyle: Record, nuiType: NuiType, ): TemplateResult | typeof nothing { if (!slides || slides.length === 0) { return nothing; } return html` ${slides.map((slide) => { const src = (slide.prefix || '') + (slide.image || ''); return html`
${ slide.url ? html` ` : html`` }
`; })} `; } function renderNavigation( show: boolean | undefined, handlers: NuiSwiperHandlers, currentIndex: number, totalSlides: number, loop: boolean | undefined, slidesPerView: number, ): TemplateResult | typeof nothing { if (!show || totalSlides <= slidesPerView) { return nothing; } const hasPrev = loop || currentIndex > 0; const hasNext = loop || currentIndex < totalSlides - slidesPerView; return html` `; } function renderPagination( show: boolean | undefined, totalSlides: number, currentIndex: number, handlers: NuiSwiperHandlers, slidesPerView: number, ): TemplateResult | typeof nothing { if (!show || totalSlides <= slidesPerView) { return nothing; } const dotsCount = totalSlides - slidesPerView + 1; if (dotsCount <= 1) { return nothing; } const dots = Array.from({ length: dotsCount }, (_, i) => i); return html`
    ${dots.map((index) => { const active = index === currentIndex; return html`
  • `; })}
`; } export function renderSwiper( state: NuiSwiperViewState, handlers: NuiSwiperHandlers, ): TemplateResult { const slidesPerView = state.slidesPerView ?? 1; return html`
${renderSlides(state.slides, state.slideStyle, state.nuiType || '')}
${renderNavigation( state.navigation, handlers, state.currentIndex, state.totalSlides, state.loop, slidesPerView, )} ${renderPagination( state.pagination, state.totalSlides, state.currentIndex, handlers, slidesPerView, )}
`; }