import { AfterViewInit, ElementRef, EventEmitter, OnChanges, OnDestroy, Renderer2, SimpleChanges, ViewContainerRef } from '@angular/core'; import { ControlValueAccessor } from '@angular/forms'; import { KaduLanguage, KaduMultiLangRichValue, KaduMultiLangShape, KaduMultiLangValue } from './kadu-multi-lang.types'; import { KaduMultiLangTranslator } from './kadu-multi-lang.translator'; import * as i0 from "@angular/core"; /** * Attribute "decorator" directive that turns a kadu form input into a * multi-language input. The form value transparently becomes a map keyed by * language code (Record), while the inline host still displays the * primary language. An icon in the corner of the host opens a popup with one * cloned host component per configured language. * * Optional AI translation: when a KaduMultiLangTranslator is provided via DI * the directive automatically fans out debounced translations from the * primary language into the others, with per-row controls (auto toggle and * regenerate button) and visual origin indication. * * Can only be applied to kadu-input, kadu-textarea or kadu-text-editor. */ export declare class KaduMultiLangDirective implements AfterViewInit, OnChanges, OnDestroy { private accessors; private translator; private elRef; private renderer; private viewContainerRef; languages: KaduLanguage[]; defaultLang: string; popupAppendTo: HTMLElement | 'body'; /** * CSS classes for the trigger icon element. Defaults to Bootstrap * Icons' `bi bi-translate` (A → 文 glyph) so the trigger reads as * "translate" rather than "geography". Override with any icon-font * class set the consumer ships (PrimeIcons, FontAwesome, Material * Icons, custom). When set to an empty string the directive falls * back to `popupIconText` (unicode glyph) so the trigger remains * visible even without an icon font. */ popupIconClass: string; popupIconText: string; /** Label for the popup footer button that dismisses the popup. */ popupOkLabel: string; /** * Seeds {@link KaduLangCellState.autoTranslate} on newly created empty * secondary cells. Does not by itself enable typing fan-out — see * {@link aiTranslateOnType} for that gate. */ aiAutoTranslate: boolean; /** * When true (default), changes to the primary language debounce into * automatic AI fan-out for cells with `autoTranslate` enabled. Set to * false to require explicit "Prevedi" / "Prevedi sve" button clicks; * successful button-triggered translations do not re-enable typing * fan-out. */ aiTranslateOnType: boolean; aiDebounceMs: number; aiMinChars: number; /** * CSS classes for the per-row "translate now" button icon. Empty by * default so the directive renders the textual `aiRegenLabel` below * (matches the text-style "Obriši prevode" toolbar button). Set to * any icon-font class set the consumer ships (e.g. `'bi bi-robot'`, * `'pi pi-refresh'`) to render an icon-only button instead — the * label text is dropped when an icon class is provided. */ aiRegenIconClass: string; /** * Visible label / fallback glyph for the per-row "translate now" * button. Used directly when `aiRegenIconClass` is empty (default). */ aiRegenLabel: string; /** * Fully disables the directive: the host input becomes read-only, * every cloned popup input becomes read-only, all per-row regen * buttons are disabled, the global "AI prevod" switch becomes a * no-op, and the "Obriši prevode" button is hidden. The trigger * icon stays clickable so users can still open the popup and * inspect translations in read-only mode. Composes additively * with {@link originDisabled}. */ disabled: boolean; /** * Locks the primary (origin) value while keeping translation * editing fully enabled. The host input and the primary-language * clone in the popup become read-only; secondary-language clones * stay editable; AI fan-out, the per-row regen button, the global * AI switch and the "Obriši prevode" button all keep working * because none of them mutate the primary value. Useful when the * source string is owned upstream (e.g. a translator portal where * an admin already approved the canonical text). Implied by * {@link disabled}. */ originDisabled: boolean; /** * When the host input is read-only (localized, {@link originDisabled} * or {@link disabled}), clicking anywhere on the host opens the * translation popup. Defaults to true. */ openPopupOnLockedClick: boolean; /** * Selects the wire shape emitted to the parent control / `(multiLangChange)` * subscribers and accepted via `writeValue`. Defaults to `'rich'` so the * directive natively round-trips the backend `LocalizedString` shape * (Brendly i18n — `i18n-plan.md` §3.1, §3.4). Set to `'plain'` to keep * the legacy `KaduMultiLangValue` map for consumers that have not yet * migrated. */ multiLangShape: KaduMultiLangShape; /** * Optional model version string stamped on every AI translation as * `aiModelVersion` on the emitted rich value. When empty, the field is * omitted from the translation object. */ aiModelVersion: string; multiLangChange: EventEmitter | KaduMultiLangRichValue>; aiTranslationStarted: EventEmitter<{ targetLang: string; sourceText: string; }>; aiTranslationCompleted: EventEmitter<{ targetLang: string; value: string; }>; aiTranslationFailed: EventEmitter<{ targetLang: string; error: unknown; }>; private host; private hostType; private cells; private formOnChange; private trigger?; private triggerClickUnlisten?; private hostClickUnlisten?; private triggerAiBadge?; private popupContainer?; /** * Read-only "AI" chip in the toolbar that lights up when every * secondary cell is currently AI-produced. Replaces the old * interactive on/off toggle: switching everything to AI is now an * explicit action via the {@link translateAllBtn} button, while * this badge is purely a status indicator. */ private globalAiBadge?; /** "Prevedi sve" action button in the toolbar. */ private translateAllBtn?; private clearTranslationsBtn?; private popupOpen; private documentClickUnlisten?; private windowResizeUnlisten?; private windowScrollUnlisten?; private documentScrollUnlisten?; private repositionRafId?; private clones; private readonly primaryText$; private primaryStreamSub?; private readonly inflight; private readonly rowUnlistens; constructor(accessors: ControlValueAccessor[] | null, translator: KaduMultiLangTranslator | null, elRef: ElementRef, renderer: Renderer2, viewContainerRef: ViewContainerRef); ngAfterViewInit(): void; ngOnChanges(changes: SimpleChanges): void; ngOnDestroy(): void; /** * Multi-lang UI (trigger icon + popup) is meaningful only when there * is more than one configured language. With 0 or 1 entry the * directive becomes a transparent pass-through: the host input shows * its single value, no trigger or popup is offered. Called from both * ngAfterViewInit and ngOnChanges so the trigger appears/disappears * if the languages Input shrinks/grows past the threshold at runtime. */ private syncTriggerVisibility; private teardownPopupUi; private resolveHost; private patchPrimaryHost; private unpatchPrimaryHost; private applyIncomingFormValue; /** * Rich shape detection is intentionally independent of `multiLangShape`: * an external service may patch the FormControl with a rich value even * when the directive Input is `'plain'`. We honour what the data looks * like, not what the Input declares. */ private isRichLocalizedShape; private isPlainMultiLangShape; private applyPlainValue; private applyRichValue; private initCells; private createEmptyCell; private setCellFromUserInput; /** * Mirror the primary value into every secondary cell that the user has not * yet personalised (origin === 'empty'). The "inherited" cells keep their * empty origin so they remain eligible for the AI fan-out and the regen * button flow, but the popup, the emitted value and the cloned form * controls all see the primary text as a sensible default. */ private propagatePrimaryToInheritedSecondaries; private getPlainValueMap; /** * Build the rich `LocalizedString`-shaped snapshot. Cells whose origin is * `'empty'` (not personalised — either truly untouched or only filled by * the inherited-mirror UX) are skipped on purpose: backend treats a * missing entry in the `translations` map as "no row in the * `*_translations` table = no translation, fall back to original". * Cells with `value` of `null`/`undefined` are also skipped for the same * reason. */ private getRichValueMap; /** * Build a sensible empty value when no primary cell has been filled. * The directive's host components are typically string-bound (kadu-input * / kadu-textarea / kadu-text-editor), so an empty string is the right * fallback for the overwhelming majority of consumers. The `as unknown * as T` keeps the return typed to the directive's generic without * forcing every caller to narrow. */ private emptyTValue; private readPrimaryDisplay; private buildEmitSnapshot; private emitValue; private refreshPrimaryDisplay; private coerceToText; private createTriggerIcon; /** * Refreshes the trigger icon to reflect two things at a glance: * - green border / "Uključena lokalizacija" tooltip when at least one * secondary language carries a different value from the primary one, * red border / "Nije uključena lokalizacija" otherwise. * - a small "AI" corner badge when every secondary cell is in AI mode * (the global popup toggle is on). */ private refreshTriggerState; private isLocalized; private isGlobalAiEnabled; private toggleFromTrigger; private openPopup; private closePopup; private onDocumentMouseDown; private createPopupContainer; /** * Positions the popup relative to the trigger and picks an * appropriate `max-height`. * * Hard contract: the popup MUST always fit within the current * viewport. We never let it extend past the visible area — even on * normal scrollable pages — because relying on the page's own * scroll to cover the overflow (1) introduces a global vertical * scrollbar that wasn't there before the popup opened and (2) * looks broken inside `overflow: hidden` modals (PrimeNG * `DynamicDialog` etc.) where the dialog's body scroll is locked * and the popup's tail simply gets clipped. * * Algorithm: * - Pick "below" when `spaceBelow >= minHeight` OR * `spaceBelow >= spaceAbove`. Otherwise flip above. * - `height = min(spaceOnChosenSide, maxAllowed)`. No `minHeight` * floor: a shorter visible popup beats one that overflows the * viewport, especially on small screens. * - Horizontal: `left = clamp(rect.left, margin, viewportW - * popupWidth - margin)` so the popup never spills off the * sides either. * * Coordinates are computed in viewport space via * `getBoundingClientRect()` and then offset by `scrollX/Y` so the * popup (which is `position: absolute` on the append target — * usually `document.body`) lands at the right place even when an * ancestor scroll container is at a non-zero offset. */ private positionPopup; /** * rAF-throttled trampoline for resize/scroll events. We don't * reposition synchronously inside the listener because * `getBoundingClientRect()` + computed-style reads inside a * scroll/resize handler force a layout, and modern browsers emit * those events at 60+Hz. One reposition per animation frame is * smooth enough. */ private schedulePopupReposition; private clearPopupChildren; private renderClones; private renderGlobalToolbar; private renderPopupFooter; /** * Re-applies state to every toolbar widget (translate-all button + * AI status chip + clear button) plus the trigger-icon AI corner. * The whole toolbar shares one `isGlobalAiEnabled()` read so the * three stay in lockstep. */ private refreshToolbar; private refreshGlobalAiBadge; private refreshTranslateAllButton; private refreshClearButton; /** * When at least one secondary language has a different value than the * primary one (= localisation is "on"), the original host input (the one * sitting on the page, NOT the primary clone inside the popup) is made * read-only. The popup remains the designated place for editing every * language - including the primary one - so the primary clone stays fully * editable. The user has to click "Obriši prevode" before they can resume * typing into the host input again. */ private isPrimaryHostLocked; private shouldOpenPopupOnHostClick; private wireHostClickListener; private onHostLockedClick; private applyPrimaryEditability; /** * Picks the user-facing tooltip for the locked primary input. The * order is meaningful: `disabled` is the strongest reason, followed * by `originDisabled`, with the localization hint as the fallback. */ private resolvePrimaryLockTitle; private resolvePrimaryLockCursor; /** * Returns the native input/textarea owned by the host component itself * (not by any clone living inside the popup). KaduFormCoreComponent * exposes it via `NativeElement`, so we go straight through the public * contract instead of relying on a DOM query. */ private getHostNativeInput; private setInputReadonly; private getCloneNativeInput; /** * Mirrors {@link applyPrimaryEditability} for the cloned inputs that * live inside the popup. Default-language clone shares the lock * with the host input (driven by `disabled` / `originDisabled`); the * popup never honours the localization lock for the primary clone * because the popup is the sanctioned spot to edit every language. * Secondary clones only lock when `disabled` is true. */ private applyCloneEditability; /** * Single entry point for re-applying every disabled-related visual * after either Input changes at runtime, or the popup is opened with * an already-set Input. Re-uses the per-control refresh helpers so * styling rules stay in one place. */ private refreshDisabledState; /** * Resets every secondary cell to inherit the primary value again, cancels * any pending AI request and turns Vi mode back on for everyone. After * this call `isLocalized()` is false → primary inputs become editable * again and the trigger turns red. */ private clearTranslations; private renderRow; private buildRegenButton; /** * Renders the optional `KaduLanguage.flag` either as an icon-font span * (when the value looks like a CSS class string — e.g. `"fi fi-rs"` * from flag-icons or `"bi bi-flag"` from bootstrap-icons) or as plain * text content (when it's a unicode emoji or any string with chars * outside the safe class-name set). The wrapper span carries * `flex: 0 0 auto` so it never grows past its intrinsic width and a * fixed `min-width` so flag glyphs vertically align across rows even * when the icon font hasn't loaded yet. */ private buildFlagElement; private createClone; private copyHostInputs; private syncAllClones; private syncCloneFor; /** * Writes a value into a cloned host component and makes sure the new * value actually lands in the DOM. Three steps: * 1. Call the component's original `writeValue` so the bound field is * updated and ngModel/Angular forms wiring is honoured. * 2. Trigger a change detection pass on the component so the template * can sync the new value onto its /