import * as i0 from '@angular/core';
import { HostListener, Directive, Input, inject, HostBinding } from '@angular/core';
import { SdUtilities } from '@sd-angular/core/utilities/extensions';
import { Router } from '@angular/router';

class SdScrollDirective {
    elementRef;
    renderer;
    #overflowX = 'hidden';
    #overflowY = 'auto';
    onMouseOver() {
        this.#overflowX = 'auto';
        // this.#overflowY = 'auto';
        this.renderer.setStyle(this.elementRef.nativeElement, 'overflow-x', this.#overflowX);
        // this.renderer.setStyle(this.elementRef.nativeElement, 'overflow-y', this.#overflowY);
    }
    onMouseOut() {
        this.#overflowX = 'hidden';
        this.renderer.setStyle(this.elementRef.nativeElement, 'overflow-x', this.#overflowX);
    }
    constructor(elementRef, renderer) {
        this.elementRef = elementRef;
        this.renderer = renderer;
    }
    ngOnInit() {
        this.renderer.setStyle(this.elementRef.nativeElement, '-webkit-transform', 'translate3d(0, 0, 0)');
        this.renderer.setStyle(this.elementRef.nativeElement, 'overflow-x', this.#overflowX);
        this.renderer.setStyle(this.elementRef.nativeElement, 'overflow-y', this.#overflowY);
    }
    scrollTop = () => {
        setTimeout(() => {
            if (this.elementRef?.nativeElement) {
                this.elementRef.nativeElement.scrollTop = 0;
            }
        }, 1);
    };
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdScrollDirective, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.17", type: SdScrollDirective, isStandalone: true, selector: "[sdScroll]", host: { listeners: { "mouseover": "onMouseOver()", "mouseout": "onMouseOut()" } }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdScrollDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[sdScroll]',
                    standalone: true,
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.Renderer2 }], propDecorators: { onMouseOver: [{
                type: HostListener,
                args: ['mouseover']
            }], onMouseOut: [{
                type: HostListener,
                args: ['mouseout']
            }] } });

class SdDesktopDirective {
    templateRef;
    viewContainerRef;
    constructor(templateRef, viewContainerRef) {
        this.templateRef = templateRef;
        this.viewContainerRef = viewContainerRef;
        if (!SdUtilities.isMobile()) {
            this.viewContainerRef.createEmbeddedView(this.templateRef);
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdDesktopDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.17", type: SdDesktopDirective, isStandalone: true, selector: "[sdDesktop]", ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdDesktopDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[sdDesktop]',
                }]
        }], ctorParameters: () => [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }] });

class SdMobileDirective {
    templateRef;
    viewContainerRef;
    constructor(templateRef, viewContainerRef) {
        this.templateRef = templateRef;
        this.viewContainerRef = viewContainerRef;
        if (SdUtilities.isMobile()) {
            this.viewContainerRef.createEmbeddedView(this.templateRef);
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdMobileDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.17", type: SdMobileDirective, isStandalone: true, selector: "[sdMobile]", ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdMobileDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[sdMobile]',
                }]
        }], ctorParameters: () => [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }] });

class SdHoverCopyDirective {
    el;
    renderer;
    copyText;
    sdHoverCopyDisabled = false;
    #copyButton = null;
    #tooltip;
    #defaultTooltip = 'Sao chép';
    constructor(el, renderer) {
        this.el = el;
        this.renderer = renderer;
    }
    // https://onemount.atlassian.net/browse/SM-2287
    // Hiện tại khi sdHoverCopyDisabled = true, directive chỉ dùng opacity: 0 và pointerEvents: 'none' để ẩn nút, nhưng điều này không hoàn toàn ngăn chặn được việc click trong một số trường hợp.
    // Giải pháp: Remove khỏi DOM nếu column không được enable
    ngOnInit() {
        if (!this.sdHoverCopyDisabled) {
            this.#createAndAppendCopyButton();
        }
    }
    ngOnChanges(changes) {
        if (changes['sdHoverCopyDisabled']) {
            if (!this.sdHoverCopyDisabled) {
                // Enable - create and show button if not exists
                if (!this.#copyButton) {
                    this.#createAndAppendCopyButton();
                }
            }
            else {
                // Disable - remove button from DOM
                this.#removeCopyButton();
            }
        }
    }
    #createAndAppendCopyButton() {
        const parent = this.el.nativeElement;
        this.renderer.setStyle(parent, 'position', 'relative');
        // Create button
        this.#copyButton = this.renderer.createElement('button');
        this.renderer.setStyle(this.#copyButton, 'position', 'absolute');
        this.renderer.setStyle(this.#copyButton, 'top', '50%');
        this.renderer.setStyle(this.#copyButton, 'transform', 'translateY(-50%)');
        this.renderer.setStyle(this.#copyButton, 'right', '4px');
        this.renderer.setStyle(this.#copyButton, 'display', 'none');
        this.renderer.setStyle(this.#copyButton, 'z-index', '10');
        this.renderer.setStyle(this.#copyButton, 'background', 'transparent');
        this.renderer.setStyle(this.#copyButton, 'border', 'none');
        this.renderer.setStyle(this.#copyButton, 'cursor', 'pointer');
        this.renderer.setStyle(this.#copyButton, 'padding', '1px');
        this.renderer.setStyle(this.#copyButton, 'border-radius', '3px');
        this.renderer.setStyle(this.#copyButton, 'padding', '5px');
        this.renderer.setStyle(this.#copyButton, 'line-height', '1');
        this.renderer.setStyle(this.#copyButton, 'background-color', 'var(--sd-black100)');
        // Add inline SVG icon
        const svg = this.renderer.createElement('svg', 'svg');
        this.renderer.addClass(svg, 'text-secondary');
        this.renderer.setAttribute(svg, 'width', '14');
        this.renderer.setAttribute(svg, 'height', '14');
        this.renderer.setAttribute(svg, 'viewBox', '0 0 24 24');
        this.renderer.setAttribute(svg, 'fill', 'none');
        this.renderer.setAttribute(svg, 'xmlns', 'http://www.w3.org/2000/svg');
        const path = this.renderer.createElement('path', 'svg');
        this.renderer.setAttribute(path, 'd', 'M16 1H4C2.9 1 2 1.9 2 3V17H4V3H16V1ZM20 5H8C6.9 5 6 5.9 6 7V21C6 22.1 6.9 23 8 23H20C21.1 23 22 22.1 22 21V7C22 5.9 21.1 5 20 5ZM20 21H8V7H20V21Z');
        this.renderer.setAttribute(path, 'fill', 'currentColor');
        this.renderer.appendChild(svg, path);
        this.renderer.appendChild(this.#copyButton, svg);
        // Tooltip
        this.#tooltip = this.renderer.createElement('span');
        this.renderer.setProperty(this.#tooltip, 'innerText', this.#defaultTooltip);
        this.renderer.setStyle(this.#tooltip, 'position', 'absolute');
        this.renderer.setStyle(this.#tooltip, 'bottom', '100%');
        this.renderer.setStyle(this.#tooltip, 'left', '50%');
        this.renderer.setStyle(this.#tooltip, 'transform', 'translateX(-50%)');
        this.renderer.setStyle(this.#tooltip, 'marginBottom', '4px');
        this.renderer.setStyle(this.#tooltip, 'background', '#333');
        this.renderer.setStyle(this.#tooltip, 'color', '#fff');
        this.renderer.setStyle(this.#tooltip, 'padding', '2px 6px');
        this.renderer.setStyle(this.#tooltip, 'borderRadius', '4px');
        this.renderer.setStyle(this.#tooltip, 'fontSize', '12px');
        this.renderer.setStyle(this.#tooltip, 'whiteSpace', 'nowrap');
        this.renderer.setStyle(this.#tooltip, 'opacity', '0');
        this.renderer.setStyle(this.#tooltip, 'transition', 'opacity 0.2s');
        this.renderer.setStyle(this.#tooltip, 'pointerEvents', 'none');
        this.renderer.setStyle(this.#tooltip, 'userSelect', 'none');
        this.renderer.appendChild(this.#copyButton, this.#tooltip);
        this.renderer.appendChild(parent, this.#copyButton);
        // Listen click
        this.renderer.listen(this.#copyButton, 'click', () => {
            console.log('click', this.copyText);
            if (this.copyText && !this.sdHoverCopyDisabled) {
                SdUtilities.copyToClipboard(String(this.copyText));
                this.#showTooltip('Copied');
                setTimeout(() => this.#hideTooltip(), 1000);
            }
        });
    }
    #removeCopyButton() {
        if (this.#copyButton) {
            this.renderer.removeChild(this.el.nativeElement, this.#copyButton);
            this.#copyButton = null;
        }
    }
    #showTooltip(message) {
        this.renderer.setProperty(this.#tooltip, 'innerText', message);
        this.renderer.setStyle(this.#tooltip, 'opacity', '1');
    }
    #hideTooltip() {
        this.renderer.setProperty(this.#tooltip, 'innerText', this.#defaultTooltip);
        this.renderer.setStyle(this.#tooltip, 'opacity', '0');
    }
    onMouseEnter() {
        if (!this.sdHoverCopyDisabled && this.#copyButton) {
            this.renderer.setStyle(this.#copyButton, 'display', 'block');
        }
    }
    onMouseLeave() {
        if (this.#copyButton) {
            this.renderer.setStyle(this.#copyButton, 'display', 'none');
            this.#hideTooltip();
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdHoverCopyDirective, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.17", type: SdHoverCopyDirective, isStandalone: true, selector: "[sdHoverCopy]", inputs: { copyText: ["sdHoverCopy", "copyText"], sdHoverCopyDisabled: "sdHoverCopyDisabled" }, host: { listeners: { "mouseenter": "onMouseEnter()", "mouseleave": "onMouseLeave()" } }, usesOnChanges: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdHoverCopyDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[sdHoverCopy]',
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.Renderer2 }], propDecorators: { copyText: [{
                type: Input,
                args: [{ alias: 'sdHoverCopy', required: true }]
            }], sdHoverCopyDisabled: [{
                type: Input
            }], onMouseEnter: [{
                type: HostListener,
                args: ['mouseenter']
            }], onMouseLeave: [{
                type: HostListener,
                args: ['mouseleave']
            }] } });

class SdHrefDirective {
    #router = inject(Router);
    // Nhận vào url đã được xử lý (từ Pipe của bạn)
    url;
    // Tự động bind giá trị url vào thuộc tính href của thẻ <a>
    get href() {
        return this.url || 'javascript:;';
    }
    onClick(event) {
        if (!this.url)
            return;
        if (this.url.startsWith('http')) {
            // Nếu là link ngoài -> Mở tab mới và ngăn chặn hành vi mặc định của thẻ a (để không chuyển trang hiện tại)
            window.open(this.url, '_blank');
            event.preventDefault();
        }
        else {
            // Nếu là link nội bộ -> Ngăn chặn full-page reload, dùng Angular Router để điều hướng
            event.preventDefault();
            const [path, queryString] = this.url.split('?');
            // Bạn có thể dùng hàm SdUtilities.parseQueryParams của bạn ở đây,
            // hoặc dùng cách native URLSearchParams như sau:
            const params = new URLSearchParams(queryString || '');
            const queryParams = {};
            params.forEach((value, key) => (queryParams[key] = value));
            this.#router.navigate([path], { queryParams });
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdHrefDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.17", type: SdHrefDirective, isStandalone: true, selector: "a[sdHref]", inputs: { url: ["sdHref", "url"] }, host: { listeners: { "click": "onClick($event)" }, properties: { "attr.href": "this.href" } }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdHrefDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: 'a[sdHref]',
                    standalone: true,
                }]
        }], propDecorators: { url: [{
                type: Input,
                args: ['sdHref']
            }], href: [{
                type: HostBinding,
                args: ['attr.href']
            }], onClick: [{
                type: HostListener,
                args: ['click', ['$event']]
            }] } });

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

export { SdDesktopDirective, SdHoverCopyDirective, SdHrefDirective, SdMobileDirective, SdScrollDirective };
//# sourceMappingURL=sd-angular-core-directives.mjs.map
