import * as i1 from '@angular/cdk/a11y';
import { A11yModule } from '@angular/cdk/a11y';
import * as i0 from '@angular/core';
import { inject, Renderer2, DestroyRef, booleanAttribute, numberAttribute, ElementRef, Directive, ContentChildren, forwardRef, Input, ContentChild, NgModule } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { kbqInjectNativeElement, leftIconClassName, rightIconClassName, getNodesWithoutComments } from '@koobiq/components/core';
import { KbqIcon } from '@koobiq/components/icon';

const baseURLRegex = /^http(s)?:\/\//;
class KbqLink {
    /** Whether the link is disabled. */
    get disabled() {
        return this._disabled;
    }
    set disabled(value) {
        if (value !== this._disabled) {
            this._disabled = value;
            this.changeDetector.markForCheck();
        }
    }
    get tabIndex() {
        return this.disabled ? -1 : this._tabIndex;
    }
    set tabIndex(value) {
        this._tabIndex = value;
    }
    get hasIcon() {
        return !!this.icon;
    }
    set print(value) {
        this.printMode = value !== null;
        this._print = value;
        this.updatePrintUrl();
    }
    constructor(elementRef, focusMonitor, changeDetector) {
        this.elementRef = elementRef;
        this.focusMonitor = focusMonitor;
        this.changeDetector = changeDetector;
        this.renderer = inject(Renderer2);
        this.destroyRef = inject(DestroyRef);
        this.nativeElement = kbqInjectNativeElement();
        this._disabled = false;
        this._tabIndex = 0;
        this.pseudo = false;
        this.noUnderline = false;
        this.big = false;
        this.compact = false;
        this.useVisited = false;
        this.updateClassModifierForIcons = () => {
            this.icons.forEach(({ nativeElement }) => {
                this.renderer.removeClass(nativeElement, leftIconClassName);
                this.renderer.removeClass(nativeElement, rightIconClassName);
            });
            const filteredNodesWithoutComments = getNodesWithoutComments(this.nativeElement.childNodes);
            if (this.icons.length && filteredNodesWithoutComments.length > 1) {
                this.icons.forEach(({ nativeElement }) => {
                    const iconIndex = filteredNodesWithoutComments.findIndex((node) => node === nativeElement);
                    if (iconIndex === 0) {
                        this.renderer.addClass(nativeElement, leftIconClassName);
                    }
                    if (iconIndex === filteredNodesWithoutComments.length - 1) {
                        this.renderer.addClass(nativeElement, rightIconClassName);
                    }
                });
            }
        };
        this.updatePrintUrl();
    }
    ngAfterViewInit() {
        this.focusMonitor.monitor(this.elementRef.nativeElement, true);
    }
    ngOnDestroy() {
        this.focusMonitor.stopMonitoring(this.elementRef.nativeElement);
    }
    focus() {
        this.getHostElement().focus();
    }
    getHostElement() {
        return this.elementRef.nativeElement;
    }
    ngAfterContentInit() {
        this.updateClassModifierForIcons();
        this.icons.changes.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(this.updateClassModifierForIcons);
    }
    updatePrintUrl() {
        Promise.resolve().then(() => {
            this.printUrl = this._print || this.getHostElement().href?.replace(baseURLRegex, '');
        });
    }
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqLink, deps: [{ token: i0.ElementRef }, { token: i1.FocusMonitor }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive }); }
    /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "18.2.14", type: KbqLink, selector: "[kbq-link]", inputs: { disabled: ["disabled", "disabled", booleanAttribute], tabIndex: ["tabIndex", "tabIndex", numberAttribute], pseudo: ["pseudo", "pseudo", booleanAttribute], noUnderline: ["noUnderline", "noUnderline", booleanAttribute], big: ["big", "big", booleanAttribute], compact: ["compact", "compact", booleanAttribute], useVisited: ["useVisited", "useVisited", booleanAttribute], print: "print" }, host: { properties: { "class.kbq-link_no-underline": "noUnderline", "class.kbq-link_use-visited": "useVisited", "class.kbq-link_big": "big", "class.kbq-link_compact": "compact", "class.kbq-link_pseudo": "pseudo", "class.kbq-link_print": "printMode", "class.kbq-text-only": "!hasIcon", "class.kbq-text-with-icon": "hasIcon", "class.kbq-disabled": "disabled", "attr.disabled": "disabled || null", "attr.tabindex": "tabIndex", "attr.print": "printUrl" }, classAttribute: "kbq-link" }, queries: [{ propertyName: "icon", first: true, predicate: KbqIcon, descendants: true }, { propertyName: "icons", predicate: i0.forwardRef(() => KbqIcon), read: ElementRef }], exportAs: ["kbqLink"], ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqLink, decorators: [{
            type: Directive,
            args: [{
                    selector: '[kbq-link]',
                    exportAs: 'kbqLink',
                    host: {
                        class: 'kbq-link',
                        '[class.kbq-link_no-underline]': 'noUnderline',
                        '[class.kbq-link_use-visited]': 'useVisited',
                        '[class.kbq-link_big]': 'big',
                        '[class.kbq-link_compact]': 'compact',
                        '[class.kbq-link_pseudo]': 'pseudo',
                        '[class.kbq-link_print]': 'printMode',
                        '[class.kbq-text-only]': '!hasIcon',
                        '[class.kbq-text-with-icon]': 'hasIcon',
                        '[class.kbq-disabled]': 'disabled',
                        '[attr.disabled]': 'disabled || null',
                        '[attr.tabindex]': 'tabIndex',
                        '[attr.print]': 'printUrl'
                    }
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i1.FocusMonitor }, { type: i0.ChangeDetectorRef }], propDecorators: { icons: [{
                type: ContentChildren,
                args: [forwardRef(() => KbqIcon), { read: ElementRef }]
            }], disabled: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], tabIndex: [{
                type: Input,
                args: [{ transform: numberAttribute }]
            }], pseudo: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], noUnderline: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], big: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], compact: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], useVisited: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], print: [{
                type: Input
            }], icon: [{
                type: ContentChild,
                args: [KbqIcon]
            }] } });

class KbqLinkModule {
    /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqLinkModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    /** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: KbqLinkModule, declarations: [KbqLink], imports: [A11yModule], exports: [KbqLink] }); }
    /** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqLinkModule, imports: [A11yModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: KbqLinkModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [
                        A11yModule
                    ],
                    declarations: [KbqLink],
                    exports: [KbqLink]
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { KbqLink, KbqLinkModule, baseURLRegex };
//# sourceMappingURL=koobiq-components-link.mjs.map
