import { Injectable, signal, type Injector, type Signal, } from '@angular/core'; import { createEmitter, type CoreSettings, type GalleryAction, type PointerRecord, type SlideDirection, type TypedEmitter, } from '@lightgallery/headless'; import type { LgFeature, LgPluginContext } from './features'; import type { LgCaptionDirective, LgCounterDirective, LgNextButtonDirective, LgPrevButtonDirective, } from './slots'; import type { LgEventMap, LgGalleryHandle, LgGalleryItem } from './types'; /** One `[lgGalleryItem]` trigger registration (uncontrolled mode). */ export interface LgItemRegistration { /** Reads the trigger's current item data (a signal-backed input). */ readonly item: () => LgGalleryItem; readonly element: HTMLElement; } /** Actions consumed by inner components and trigger directives. */ export interface LgGalleryActions extends LgGalleryHandle { /** * Internal: navigate with an explicit direction (gesture releases at the * loop edges need it); same gating as `goToSlide`. */ navigate(index: number, direction?: SlideDirection): void; /** Raw reducer dispatch (feature runtime; React `GalleryActions` parity). */ dispatch(action: GalleryAction): void; } /** * The gesture seam plugins consume, mirroring the React * `GestureSeam` field-for-field: the zoom feature claims the lock * while pinching/zoom-dragging — core swipe stands down — and reads the * live pointer records for its multi-pointer math. Mutable by design: it * changes per pointer event and must never trigger change detection. */ export interface LgGestureSeam { /** Current lock owner; `null` means core swipe/drag may act. */ lockOwner: 'pinch' | 'zoomSwipe' | null; claim(owner: 'pinch' | 'zoomSwipe' | null): void; /** Live pointers inside the gallery (multi-pointer bookkeeping). */ pointers: PointerRecord[]; } /** Timeline hooks the gesture directive drives on the gallery. */ export interface LgGestureHooks { /** Assign prev/next position classes around the current slide (1 render). */ prepareDrag(): void; /** Commit a swipe release to a slide change with fromTouch semantics. */ commitTouchNavigation(target: number, direction: SlideDirection): void; /** The navigation spring settled (or died) — restore the slide mode. */ settleTouchNavigation(): void; } /** Template slots discovered by the gallery via content queries. */ export interface LgGallerySlots { caption: Signal; counter: Signal; prevButton: Signal; nextButton: Signal; } /** * Per-gallery-instance plumbing shared between `` and its inner * components/directives — the Angular analog of the React track's split * contexts. Provided alongside `LightGalleryStore` on the * gallery component; the component assigns the late-bound fields once in its * constructor (single writer). */ @Injectable() export class LgGalleryRuntime { /** * The plugin/core event bus — the shared headless emitter. * Outputs fan out from the same events via `emit`. */ readonly events: TypedEmitter = createEmitter(); /** Resolved settings (headless merge order). Assigned by the gallery. */ settings!: Signal; /** The gallery data: `slides` input or trigger registrations, in order. */ items!: Signal; slots!: LgGallerySlots; /** Emits the matching output AND the bus event. */ emit!: ( name: K, detail: LgEventMap[K], ) => void; actions!: LgGalleryActions; /** Assigned by the gallery; consumed by the gesture directive. */ gestureHooks!: LgGestureHooks; /** Registered features, deduped, in `[features]` order. */ features!: Signal; /** * Per-gallery feature injector: `LG_FEATURE` multi-providers + every * feature's own `providers`, parented on the gallery's node injector. * Slot components and slide renderers are created with it. */ readonly featureInjector = signal(null); /** The `LG_PLUGIN_CONTEXT` value; assigned by the gallery constructor. */ pluginContext!: LgPluginContext; readonly gestureSeam: LgGestureSeam = (() => { const seam: LgGestureSeam = { lockOwner: null, claim(owner) { seam.lockOwner = owner; }, pointers: [], }; return seam; })(); // Uncontrolled trigger registry — registration (mount) order defines // slide order, same caveat as the React `` registry. private readonly registrationsSignal = signal([]); readonly registrations = this.registrationsSignal.asReadonly(); /** True while the first-slide dummy is up (`lg-first-slide-loading`). */ readonly firstSlideLoading = signal(false); /** True while the zoom-from-origin flight is RUNNING (entrance → * flight-end); features defer heavy work until it clears. */ readonly zoomOriginOpen = signal(false); /** * Src for the first-slide dummy image (2.x `getDummyImageContent`): * the item's `thumb`, else the trigger's rendered img — pixels that * are already decoded and can fly without waiting on the network. */ getDummySrc(index: number): string | null { const thumb = this.items()[index]?.thumb; if (thumb) { return thumb; } const element = this.registrationsSignal()[index]?.element; const img = element?.querySelector('img'); return img?.currentSrc || img?.src || null; } registerItem(registration: LgItemRegistration): () => void { this.registrationsSignal.update((prev) => [...prev, registration]); return () => { this.registrationsSignal.update((prev) => prev.filter((entry) => entry !== registration), ); }; } getItemIndex(registration: LgItemRegistration): number { return this.registrationsSignal().indexOf(registration); } }