import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Self, Optional, HostBinding, Input, Output, ViewChild, NgModule } from '@angular/core';
import * as i1 from '@angular/forms';
import { FormsModule } from '@angular/forms';
import * as i5 from '@angular/material/button';
import { MatButtonModule } from '@angular/material/button';
import * as i3 from '@deja-js/component/color-selector';
import { DejaColorSelectorModule } from '@deja-js/component/color-selector';
import * as i4 from '@deja-js/component/overlay';
import { DejaOverlayComponent, DejaOverlayModule } from '@deja-js/component/overlay';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { DejaConnectionPositionPair } from '@deja-js/component/core';
import { Color } from '@deja-js/component/core/graphics';

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaColorPickerComponent {
    constructor(elementRef, control, changeDetectorRef) {
        this.control = control;
        this.changeDetectorRef = changeDetectorRef;
        this._disabled = null;
        this.resetcolor = null;
        /** Déclenché lorsqu'une couleur est survolée par la souris. */
        this.colorhover = new EventEmitter();
        this._small = false;
        this._isOpen = false;
        this._positions = DejaConnectionPositionPair.default;
        this.onTouchedCallback = () => undefined;
        this.onChangeCallback = (_a) => undefined;
        if (this.control) {
            this.control.valueAccessor = this;
        }
        this.ownerElement = elementRef.nativeElement;
    }
    /** Retourne ou definit si la partie déroulante est visible. */
    set isOpen(value) {
        this._isOpen = coerceBooleanProperty(value) || null;
    }
    get isOpen() {
        return this._isOpen;
    }
    set positions(value) {
        this._positions = typeof value === 'string' ? DejaConnectionPositionPair.parse(value) : value;
    }
    get positions() {
        return this._positions;
    }
    /** Retourne ou définit la taille du bouton. */
    set small(value) {
        this._small = coerceBooleanProperty(value);
    }
    get small() {
        return this._small;
    }
    /** Retourne ou definit si le selecteur est desactivé. */
    set disabled(value) {
        this._disabled = coerceBooleanProperty(value);
        this.changeDetectorRef.markForCheck();
    }
    get disabled() {
        return this._disabled;
    }
    // ************* ControlValueAccessor Implementation **************
    // set accessor including call the onchange callback
    set value(value) {
        if (!Color.equals(value, this._value)) {
            this.writeValue(value);
            this.onChangeCallback(value);
        }
    }
    // get accessor
    get value() {
        return this._value;
    }
    // From ControlValueAccessor interface
    writeValue(value) {
        this._value = value;
        this.changeDetectorRef.markForCheck();
    }
    // From ControlValueAccessor interface
    registerOnChange(fn) {
        this.onChangeCallback = fn;
    }
    // From ControlValueAccessor interface
    registerOnTouched(fn) {
        this.onTouchedCallback = fn;
    }
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    // ************* End of ControlValueAccessor Implementation **************
    /** Affiche le color picker. */
    show(event) {
        if (!this.disabled) {
            this.dejaOverlayCmp.show(event);
            this.isOpen = true;
            this.changeDetectorRef.markForCheck();
        }
        return false;
    }
    /** Ferme le color picker. */
    close() {
        this.isOpen = false;
    }
    onColorChange(color) {
        this.isOpen = false;
        this.value = color;
    }
    // eslint-disable-next-line @typescript-eslint/naming-convention
    getStyle() {
        const backgroundColor = this.value?.toHex();
        // eslint-disable-next-line @typescript-eslint/naming-convention
        return backgroundColor ? { 'background-color': backgroundColor } : null;
    }
}
DejaColorPickerComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerComponent, deps: [{ token: i0.ElementRef }, { token: i1.NgControl, optional: true, self: true }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
DejaColorPickerComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.3", type: DejaColorPickerComponent, selector: "deja-color-picker", inputs: { colors: "colors", resetcolor: "resetcolor", isOpen: "isOpen", positions: "positions", small: "small", disabled: "disabled" }, outputs: { colorhover: "colorhover" }, host: { properties: { "attr.disabled": "this._disabled" } }, viewQueries: [{ propertyName: "dejaOverlayCmp", first: true, predicate: DejaOverlayComponent, descendants: true, static: true }], ngImport: i0, template: "<button type=\"button\" *ngIf=\"!small\" [disabled]=\"disabled\" id=\"colorbtn\" mat-fab (click)=\"show($event)\" [ngStyle]=\"getStyle()\"></button>\n<button type=\"button\" *ngIf=\"small\" [disabled]=\"disabled\" id=\"colorbtn\" mat-mini-fab (click)=\"show($event)\" [ngStyle]=\"getStyle()\"></button>\n<deja-overlay [ownerElement]=\"ownerElement\" overlayBackdropClass=\"cdk-overlay-dark-backdrop\" [isVisible]=\"isOpen\" (closed)=\"close()\" [positions]=\"positions\">\n    <deja-color-selector [resetcolor]=\"resetcolor\" [colors]=\"colors\" [ngModel]=\"value\" (colorhover)=\"colorhover.emit($event)\" (ngModelChange)=\"onColorChange($event)\"></deja-color-selector>\n</deja-overlay>\n", styles: ["deja-color-picker #colorbtn[disabled]{filter:grayscale(100%)}.deja-overlay-container deja-color-selector{overflow:hidden;max-width:24rem;min-height:15rem}\n"], dependencies: [{ kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "component", type: i3.DejaColorSelectorComponent, selector: "deja-color-selector", inputs: ["resetcolor", "disabled", "colors"], outputs: ["colorhover"] }, { kind: "component", type: i4.DejaOverlayComponent, selector: "deja-overlay", inputs: ["overlayBackdropClass", "overlayContainerClass", "overlayOffsetX", "overlayOffsetY", "isVisible", "hasBackdrop", "ownerElement", "positions", "positionsForMobile", "isMobile", "width", "widthForMobile"], outputs: ["visibleChange", "closed"] }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i5.MatButton, selector: "button[mat-button], button[mat-raised-button], button[mat-icon-button],             button[mat-fab], button[mat-mini-fab], button[mat-stroked-button],             button[mat-flat-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerComponent, decorators: [{
            type: Component,
            args: [{ changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, selector: 'deja-color-picker', template: "<button type=\"button\" *ngIf=\"!small\" [disabled]=\"disabled\" id=\"colorbtn\" mat-fab (click)=\"show($event)\" [ngStyle]=\"getStyle()\"></button>\n<button type=\"button\" *ngIf=\"small\" [disabled]=\"disabled\" id=\"colorbtn\" mat-mini-fab (click)=\"show($event)\" [ngStyle]=\"getStyle()\"></button>\n<deja-overlay [ownerElement]=\"ownerElement\" overlayBackdropClass=\"cdk-overlay-dark-backdrop\" [isVisible]=\"isOpen\" (closed)=\"close()\" [positions]=\"positions\">\n    <deja-color-selector [resetcolor]=\"resetcolor\" [colors]=\"colors\" [ngModel]=\"value\" (colorhover)=\"colorhover.emit($event)\" (ngModelChange)=\"onColorChange($event)\"></deja-color-selector>\n</deja-overlay>\n", styles: ["deja-color-picker #colorbtn[disabled]{filter:grayscale(100%)}.deja-overlay-container deja-color-selector{overflow:hidden;max-width:24rem;min-height:15rem}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i1.NgControl, decorators: [{
                    type: Self
                }, {
                    type: Optional
                }] }, { type: i0.ChangeDetectorRef }]; }, propDecorators: { _disabled: [{
                type: HostBinding,
                args: ['attr.disabled']
            }], colors: [{
                type: Input
            }], resetcolor: [{
                type: Input
            }], colorhover: [{
                type: Output
            }], dejaOverlayCmp: [{
                type: ViewChild,
                args: [DejaOverlayComponent, { static: true }]
            }], isOpen: [{
                type: Input
            }], positions: [{
                type: Input
            }], small: [{
                type: Input
            }], disabled: [{
                type: Input
            }] } });

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaColorPickerModule {
}
DejaColorPickerModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaColorPickerModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerModule, declarations: [DejaColorPickerComponent], imports: [CommonModule,
        DejaColorSelectorModule,
        DejaOverlayModule,
        FormsModule,
        MatButtonModule], exports: [DejaColorPickerComponent] });
DejaColorPickerModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerModule, imports: [CommonModule,
        DejaColorSelectorModule,
        DejaOverlayModule,
        FormsModule,
        MatButtonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaColorPickerModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DejaColorPickerComponent],
                    exports: [DejaColorPickerComponent],
                    imports: [
                        CommonModule,
                        DejaColorSelectorModule,
                        DejaOverlayModule,
                        FormsModule,
                        MatButtonModule
                    ]
                }]
        }] });

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

export { DejaColorPickerComponent, DejaColorPickerModule };
//# sourceMappingURL=deja-js-component-color-picker.mjs.map
