import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, Component, Output, ContentChild, HostListener, NgModule } from '@angular/core';
import { Destroy, KeyCodes } from '@deja-js/component/core';
import { fromEvent, filter, takeUntil } from 'rxjs';

/*
 *  @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
 */
/**
 * Simple dialog for Angular
 */
class DejaDialogComponent extends Destroy {
    /**
     * Constructor
     */
    constructor(elementRef) {
        super();
        /** Event emitted when dialog close action is called */
        this.closed = new EventEmitter();
        const element = elementRef.nativeElement;
        fromEvent(element.ownerDocument, 'keyup').pipe(filter(event => { var _a, _b; return !!(event.code === KeyCodes.Enter && ((_a = this.okButton) === null || _a === void 0 ? void 0 : _a._elementRef)) || !!(event.code === KeyCodes.Escape && ((_b = this.cancelButton) === null || _b === void 0 ? void 0 : _b._elementRef)); }), takeUntil(this.destroyed$)).subscribe(event => {
            if (event.code === KeyCodes.Enter) {
                this.okButton._elementRef.nativeElement.click();
            }
            else if (event.code === KeyCodes.Escape) {
                this.cancelButton._elementRef.nativeElement.click();
            }
        });
    }
    /**
     * Listen on click on dialogComponent.
     * If click is not inside the dialog, close action is called.
     *
     * @param event
     */
    close(event) {
        let close = true;
        let target = event.target;
        const element = event.currentTarget;
        // eslint-disable-next-line no-loops/no-loops
        while (target.parentElement && target !== element) {
            if (target.className === 'dialog') {
                close = false;
            }
            target = target.parentElement;
        }
        if (close) {
            this.closed.emit();
            event.preventDefault();
        }
    }
}
DejaDialogComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogComponent, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
DejaDialogComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.1", type: DejaDialogComponent, selector: "deja-dialog", outputs: { closed: "closed" }, host: { listeners: { "click": "close($event)" } }, queries: [{ propertyName: "okButton", first: true, predicate: ["okaction"], descendants: true }, { propertyName: "cancelButton", first: true, predicate: ["cancelaction"], descendants: true }], usesInheritance: true, ngImport: i0, template: "<div class=\"dialog\">\n    <ng-content></ng-content>\n</div>", styles: [":host{align-items:center;background-color:#0009;display:flex;height:100%;justify-content:center;left:0;position:fixed;top:0;width:100%;z-index:999}:host .dialog{z-index:1000}\n"] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogComponent, decorators: [{
            type: Component,
            args: [{ selector: 'deja-dialog', template: "<div class=\"dialog\">\n    <ng-content></ng-content>\n</div>", styles: [":host{align-items:center;background-color:#0009;display:flex;height:100%;justify-content:center;left:0;position:fixed;top:0;width:100%;z-index:999}:host .dialog{z-index:1000}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { closed: [{
                type: Output
            }], okButton: [{
                type: ContentChild,
                args: ['okaction']
            }], cancelButton: [{
                type: ContentChild,
                args: ['cancelaction']
            }], close: [{
                type: HostListener,
                args: ['click', ['$event']]
            }] } });

/*
 *  @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 DejaDialogModule {
}
DejaDialogModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaDialogModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogModule, declarations: [DejaDialogComponent], imports: [CommonModule], exports: [DejaDialogComponent] });
DejaDialogModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogModule, imports: [[
            CommonModule
        ]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.1", ngImport: i0, type: DejaDialogModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        DejaDialogComponent
                    ],
                    exports: [
                        DejaDialogComponent
                    ],
                    imports: [
                        CommonModule
                    ]
                }]
        }] });

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

export { DejaDialogComponent, DejaDialogModule };
//# sourceMappingURL=deja-js-component-dialog.mjs.map
