import * as i0 from '@angular/core';
import { Input, Directive, NgModule } from '@angular/core';
import { colorIsLight, contrast } from '@aztrix/helpers';

class ContrastDirective {
    constructor(_elementRef) {
        this._elementRef = _elementRef;
        this.color = '';
    }
    ngAfterViewInit() {
        const element = this._elementRef.nativeElement;
        const backgroundColor = this.getInheritedBackgroundColor(element);
        if (this.color === '') {
            if (colorIsLight(backgroundColor)) {
                if (element.classList.contains('ax-contrast-light')) {
                    element.classList.remove('ax-contrast-light');
                }
                if (!element.classList.contains('ax-contrast-dark')) {
                    element.classList.add('ax-contrast-dark');
                }
            }
            else {
                if (element.classList.contains('ax-contrast-dark')) {
                    element.classList.remove('ax-contrast-dark');
                }
                if (!element.classList.contains('ax-contrast-light')) {
                    element.classList.add('ax-contrast-light');
                }
            }
        }
        else {
            const colorKey = this._contrastColorKey(backgroundColor);
            element.style.color = `var(${colorKey})`;
        }
    }
    getInheritedBackgroundColor(el) {
        const divElement = document.createElement('div');
        document.head.appendChild(divElement);
        const backgroundColor = getComputedStyle(divElement).backgroundColor;
        document.head.removeChild(divElement);
        return this._traverseParentBackgroundColor(el, backgroundColor);
    }
    _traverseParentBackgroundColor(el, defaultBackgroundColor) {
        const backgroundColor = getComputedStyle(el).backgroundColor;
        if (backgroundColor !== defaultBackgroundColor) {
            return backgroundColor;
        }
        if (!el.parentElement) {
            return backgroundColor;
        }
        return this._traverseParentBackgroundColor(el.parentElement, defaultBackgroundColor);
    }
    _contrastColorKey(color) {
        const styles = getComputedStyle(this._elementRef.nativeElement);
        const paletteColors = ['500', '400', '600', '300', '700', '200', '800', '100', '900'].map((ratio) => {
            const key = `--${this.color}-color-${ratio}`;
            return { key, value: styles.getPropertyValue(`--${this.color}-color-${ratio}`) };
        });
        for (const paletteColor of paletteColors) {
            if (contrast(color, paletteColor.value) >= 4.5) {
                return paletteColor.key;
            }
        }
        return colorIsLight(color) ? `--${this.color}-color-900` : `--${this.color}-color-100`;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ContrastDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.12", type: ContrastDirective, selector: "[axContrast]", inputs: { color: ["axContrast", "color"] }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ContrastDirective, decorators: [{
            type: Directive,
            args: [{ selector: '[axContrast]' }]
        }], ctorParameters: () => [{ type: i0.ElementRef }], propDecorators: { color: [{
                type: Input,
                args: ['axContrast']
            }] } });

class ContrastModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ContrastModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: ContrastModule, declarations: [ContrastDirective], exports: [ContrastDirective] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ContrastModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ContrastModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ContrastDirective],
                    exports: [ContrastDirective],
                }]
        }] });

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

export { ContrastDirective, ContrastModule };
//# sourceMappingURL=aztrix-components-contrast.mjs.map
