import * as i0 from '@angular/core';
import { Component, Input, NgModule } from '@angular/core';
import { ColorPickerModalComponent, ColorPickerModalModule } from '@aztrix/components/color-picker-modal';
import * as i1 from '@aztrix/components/overlay';
import { ModalSize } from '@aztrix/components/overlay';
import { contrastingFontColor, HelperPipesModule } from '@aztrix/helpers';
import { Subscription, ReplaySubject, combineLatest, switchMap, startWith, map } from 'rxjs';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from '@aztrix/icons';
import { IconsModule } from '@aztrix/icons';
import { ButtonModule } from '@aztrix/components/button';

class ColorSelectComponent {
    constructor(_elementRef, _overlay) {
        this._elementRef = _elementRef;
        this._overlay = _overlay;
        this.canClear = false;
        this._subscription = new Subscription();
        this._form$ = new ReplaySubject(1);
        this._predefinedColors$ = new ReplaySubject(1);
        this.customBackgroundColor$ = combineLatest([
            this._form$.pipe(switchMap((form) => form.valueChanges.pipe(startWith(form.value)))),
            this._predefinedColors$,
        ]).pipe(map(([value, predefinedColors]) => {
            return (predefinedColors || []).includes(value) ? '#fff' : value;
        }));
        this.customBackgroundFontColor$ = this.customBackgroundColor$.pipe(map((color) => contrastingFontColor(color)));
    }
    ngOnChanges(changes) {
        if (changes.form && this.form) {
            this._form$.next(this.form);
        }
        if (changes.predefinedColors) {
            this._predefinedColors$.next(this.predefinedColors);
        }
    }
    openColorPickerModal() {
        this._overlay.createModal(this._elementRef, ColorPickerModalComponent, {
            modalSize: ModalSize.EXTRA_SMALL,
            hasTitleBar: false,
            init: (modal) => {
                modal.value = this.form?.value || '';
                this._subscription.unsubscribe();
                this._subscription = modal.valueChange.subscribe((value) => {
                    this.setValue(value);
                });
            },
            close: () => {
                this._subscription.unsubscribe();
            },
        });
    }
    clear() {
        if (this.form) {
            this.form.setValue('');
        }
    }
    setValue(value) {
        if (this.form?.value !== value) {
            this.form?.setValue(value);
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectComponent, deps: [{ token: i0.ElementRef }, { token: i1.OverlayService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: ColorSelectComponent, selector: "ax-color-select", inputs: { form: "form", predefinedColors: "predefinedColors", canClear: "canClear" }, usesOnChanges: true, ngImport: i0, template: "<button class=\"color clear\" *ngIf=\"canClear\" [ngClass]=\"{selected: !form?.value}\" (click)=\"clear()\">\n  <ax-icon class=\"color-icon\" name=\"cancel\"></ax-icon>\n</button>\n<button\n  class=\"color\"\n  [ngClass]=\"{selected: color === form?.value}\"\n  *ngFor=\"let color of predefinedColors\"\n  [style.background]=\"color\"\n  (click)=\"setValue(color)\"\n></button>\n<button\n  class=\"color custom\"\n  [ngClass]=\"{selected: form?.value && !(predefinedColors || []).includes(form?.value || '')}\"\n  [style.background]=\"customBackgroundColor$ | async\"\n  [style.color]=\"customBackgroundFontColor$ | async\"\n  (click)=\"openColorPickerModal()\"\n>\n  <ax-icon class=\"color-icon\" name=\"tune-variant\"></ax-icon>\n</button>\n", styles: [":host{display:flex;align-items:center;width:100%;flex-wrap:wrap;gap:4px}.title{margin-top:.5rem}.color{display:flex;align-items:center;justify-content:center;width:50px;height:50px;border-radius:10px;border:1px solid #363f48;transition:border .4s cubic-bezier(.25,.8,.25,1)}.color.selected{border:3px solid var(--primary-color-500)}.color-icon{width:24px;height:24px}\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i3.IconComponent, selector: "ax-icon", inputs: ["name", "inline"] }, { kind: "pipe", type: i2.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-color-select', template: "<button class=\"color clear\" *ngIf=\"canClear\" [ngClass]=\"{selected: !form?.value}\" (click)=\"clear()\">\n  <ax-icon class=\"color-icon\" name=\"cancel\"></ax-icon>\n</button>\n<button\n  class=\"color\"\n  [ngClass]=\"{selected: color === form?.value}\"\n  *ngFor=\"let color of predefinedColors\"\n  [style.background]=\"color\"\n  (click)=\"setValue(color)\"\n></button>\n<button\n  class=\"color custom\"\n  [ngClass]=\"{selected: form?.value && !(predefinedColors || []).includes(form?.value || '')}\"\n  [style.background]=\"customBackgroundColor$ | async\"\n  [style.color]=\"customBackgroundFontColor$ | async\"\n  (click)=\"openColorPickerModal()\"\n>\n  <ax-icon class=\"color-icon\" name=\"tune-variant\"></ax-icon>\n</button>\n", styles: [":host{display:flex;align-items:center;width:100%;flex-wrap:wrap;gap:4px}.title{margin-top:.5rem}.color{display:flex;align-items:center;justify-content:center;width:50px;height:50px;border-radius:10px;border:1px solid #363f48;transition:border .4s cubic-bezier(.25,.8,.25,1)}.color.selected{border:3px solid var(--primary-color-500)}.color-icon{width:24px;height:24px}\n"] }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i1.OverlayService }], propDecorators: { form: [{
                type: Input
            }], predefinedColors: [{
                type: Input
            }], canClear: [{
                type: Input
            }] } });

class ColorSelectModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectModule, declarations: [ColorSelectComponent], imports: [CommonModule, HelperPipesModule, IconsModule, ButtonModule, ColorPickerModalModule], exports: [ColorSelectComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectModule, imports: [CommonModule, HelperPipesModule, IconsModule, ButtonModule, ColorPickerModalModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ColorSelectModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ColorSelectComponent],
                    imports: [CommonModule, HelperPipesModule, IconsModule, ButtonModule, ColorPickerModalModule],
                    exports: [ColorSelectComponent],
                }]
        }] });

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

export { ColorSelectComponent, ColorSelectModule };
//# sourceMappingURL=aztrix-components-color-select.mjs.map
