import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Directive, HostBinding, Input, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Output, ContentChildren, NgModule } from '@angular/core';
import { coerceNumberProperty, coerceBooleanProperty } from '@angular/cdk/coercion';
import { Destroy } from '@deja-js/component/core';
import { Subject, filter, switchMap, of, fromEvent, shareReplay, mergeWith, map, tap, takeUntil, take } 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
 */
/**
 * Directive representing a panel in a Splitter Component
 */
class SplitAreaDirective {
    constructor(elementRef) {
        this.elementRef = elementRef;
        this._size = null;
        this.direction = 'horizontal';
        this._minSizePixel = 0;
    }
    /**
     * Size in percent of the current area
     */
    set size(value) {
        this._size = coerceNumberProperty(value);
    }
    get size() {
        const parentElement = this.elementRef.nativeElement.parentElement;
        const parentSizeInPixels = this.direction === 'horizontal' ? parentElement.offsetWidth : parentElement.offsetHeight;
        return 100 * this.sizeinPixels / parentSizeInPixels;
    }
    get sizeinPixels() {
        return this.direction === 'horizontal' ? this.elementRef.nativeElement.offsetWidth : this.elementRef.nativeElement.offsetHeight;
    }
    /**
     * Min size in percent of the current area
     */
    set minSizePixel(value) {
        this._minSizePixel = coerceNumberProperty(value);
    }
    get minSizePixel() {
        return this._minSizePixel;
    }
    get minWidth() {
        return this.direction === 'vertical' ? null : this._minSizePixel;
    }
    get minHeight() {
        return this.direction === 'horizontal' ? null : this._minSizePixel;
    }
}
SplitAreaDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: SplitAreaDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
SplitAreaDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.1.1", type: SplitAreaDirective, selector: "split-area", inputs: { size: "size", minSizePixel: "minSizePixel" }, host: { properties: { "style.order": "this.order", "style.flex-basis.%": "this._size", "style.min-width.px": "this.minWidth", "style.min-height.px": "this.minHeight" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: SplitAreaDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: 'split-area'
                }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { order: [{
                type: HostBinding,
                args: ['style.order']
            }], _size: [{
                type: HostBinding,
                args: ['style.flex-basis.%']
            }], size: [{
                type: Input
            }], minSizePixel: [{
                type: Input
            }], minWidth: [{
                type: HostBinding,
                args: ['style.min-width.px']
            }], minHeight: [{
                type: HostBinding,
                args: ['style.min-height.px']
            }] } });

/*
 *  @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
 */
/**
 * Splitter Component for Angular
 *
 * The splitter component allows to split horizontally or vertically, a container in N resizable part.
 */
class DejaSplitterComponent extends Destroy {
    /**
     * Constructor
     */
    constructor(changeDetectorRef, elementRef) {
        super();
        /**
         * Event triggered when the user start to drag the cursor
         */
        this.dragStart = new EventEmitter(false);
        /**
         * Event triggered during the cursor's drag
         */
        this.dragProgress = new EventEmitter(false);
        /**
         * Event triggered when the user stop to drag the cursor
         */
        this.dragEnd = new EventEmitter(false);
        this.splitting = null;
        this._direction = 'horizontal';
        this._disabled = null;
        this.startDragging$ = new Subject();
        this.areas = new Array();
        this._gutterSize = 10;
        this.startDragging$.pipe(filter(() => !this.disabled), switchMap(draggingEvent => {
            const areaA = this.areas.find(a => a.order === draggingEvent.index - 1);
            const areaB = this.areas.find(a => a.order === draggingEvent.index + 1);
            const mouseEvent = draggingEvent.event;
            if (!areaA || !areaB) {
                return of(mouseEvent);
            }
            const startPos = this.direction === 'horizontal' ? mouseEvent.pageX || mouseEvent.screenX : mouseEvent.pageY || mouseEvent.screenY;
            const containerSizeInPixels = this.direction === 'horizontal' ? elementRef.nativeElement.offsetWidth : elementRef.nativeElement.offsetHeight;
            const startSizeInPixelsA = areaA.sizeinPixels;
            const startSizeInPixelsB = areaB.sizeinPixels;
            this.dragStart.emit();
            const mouseMove$ = fromEvent(document, 'mousemove').pipe(shareReplay({ bufferSize: 1, refCount: true }));
            const stopDragging$ = mouseMove$.pipe(filter(event => event.buttons !== 1), mergeWith(fromEvent(document, 'mouseup'), fromEvent(document, 'touchend'), fromEvent(document, 'touchcancel')), map(event => {
                this.dragEnd.emit();
                return event;
            }), shareReplay({ bufferSize: 1, refCount: true }));
            const drag$ = mouseMove$.pipe(filter(event => event.buttons === 1), mergeWith(fromEvent(document, 'touchmove')), tap(event => {
                const pos = this.direction === 'horizontal' ? event.pageX || event.screenX : event.pageY || event.screenY;
                const diffInPixels = startPos - pos;
                areaA.size = Math.min(100, Math.max(0, 100 * (startSizeInPixelsA - diffInPixels) / containerSizeInPixels));
                areaB.size = Math.min(100, Math.max(0, 100 * (startSizeInPixelsB + diffInPixels) / containerSizeInPixels));
                this.dragProgress.emit();
            }), takeUntil(stopDragging$));
            return stopDragging$.pipe(take(1), mergeWith(drag$));
        }), takeUntil(this.destroyed$)).subscribe(event => {
            event.preventDefault();
            this.splitting = event.type !== 'mouseup' && event.type !== 'touchend' && event.type !== 'touchcancel' || null;
            changeDetectorRef.markForCheck();
            return false;
        });
    }
    /**
     * Direction of the split
     * Can be `horizontal` or `vertical`
     */
    set direction(direction) {
        this._direction = direction;
        this.ensureDirections();
    }
    get direction() {
        return this._direction;
    }
    /**
    * Size of the gutter in pixels
    * By default `10px`
    */
    set gutterSize(gutterSize) {
        this._gutterSize = coerceNumberProperty(gutterSize);
    }
    get gutterSize() {
        return this._gutterSize;
    }
    get styleFlexDirection() {
        return this.direction === 'horizontal' ? 'row' : 'column';
    }
    set spliterAreas(spliterAreas) {
        this.areas = spliterAreas.toArray();
        this.areas.forEach((area, index) => area.order = 2 * index);
        this.ensureDirections();
    }
    /** Retourne ou definit si le selecteur est desactivé. */
    set disabled(value) {
        this._disabled = coerceBooleanProperty(value) || null;
    }
    get disabled() {
        return this._disabled;
    }
    ensureDirections() {
        this.areas.forEach(area => area.direction = this.direction);
    }
}
DejaSplitterComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: DejaSplitterComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
DejaSplitterComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.1", type: DejaSplitterComponent, selector: "deja-splitter", inputs: { direction: "direction", gutterSize: "gutterSize", disabled: "disabled" }, outputs: { dragStart: "dragStart", dragProgress: "dragProgress", dragEnd: "dragEnd" }, host: { properties: { "style.flex-direction": "this.styleFlexDirection", "attr.splitting": "this.splitting", "attr.direction": "this._direction", "attr.disabled": "this._disabled" } }, queries: [{ propertyName: "spliterAreas", predicate: SplitAreaDirective }], usesInheritance: true, ngImport: i0, template: "<ng-content></ng-content>\n<ng-template ngFor let-area [ngForOf]=\"areas\" let-index=\"index\" let-last=\"last\">\n    <div class=\"split-gutter\" *ngIf=\"last === false\" [style.order]=\"index*2+1\" [style.flex-basis.px]=\"gutterSize\" (mousedown)=\"startDragging$.next({ event: $event, index: index*2+1 })\" (touchstart)=\"startDragging$.next({ event: $event, index: index*2+1 })\">\n    </div>\n</ng-template>\n", styles: ["deja-splitter{display:flex;flex-wrap:nowrap;justify-content:flex-start;overflow:hidden}deja-splitter split-area{flex:1 1 auto;overflow:hidden}deja-splitter split-area[splitting] *{user-select:none}deja-splitter .split-gutter{flex-grow:0;flex-shrink:0;background-position:50%;background-repeat:no-repeat;content:none;cursor:default;background-image:none}deja-splitter[direction=horizontal] split-area{width:100%}deja-splitter[direction=horizontal]:not([disabled]) .split-gutter{cursor:col-resize;content:\" \";background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==)}deja-splitter[direction=vertical] split-area{height:100%}deja-splitter[direction=vertical]:not([disabled]) .split-gutter{cursor:row-resize;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFCAMAAABl/6zIAAAABlBMVEUAAADMzMzIT8AyAAAAAXRSTlMAQObYZgAAABRJREFUeAFjYGRkwIMJSeMHlBkOABP7AEGzSuPKAAAAAElFTkSuQmCC)}\n"], dependencies: [{ kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: DejaSplitterComponent, decorators: [{
            type: Component,
            args: [{ changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, selector: 'deja-splitter', template: "<ng-content></ng-content>\n<ng-template ngFor let-area [ngForOf]=\"areas\" let-index=\"index\" let-last=\"last\">\n    <div class=\"split-gutter\" *ngIf=\"last === false\" [style.order]=\"index*2+1\" [style.flex-basis.px]=\"gutterSize\" (mousedown)=\"startDragging$.next({ event: $event, index: index*2+1 })\" (touchstart)=\"startDragging$.next({ event: $event, index: index*2+1 })\">\n    </div>\n</ng-template>\n", styles: ["deja-splitter{display:flex;flex-wrap:nowrap;justify-content:flex-start;overflow:hidden}deja-splitter split-area{flex:1 1 auto;overflow:hidden}deja-splitter split-area[splitting] *{user-select:none}deja-splitter .split-gutter{flex-grow:0;flex-shrink:0;background-position:50%;background-repeat:no-repeat;content:none;cursor:default;background-image:none}deja-splitter[direction=horizontal] split-area{width:100%}deja-splitter[direction=horizontal]:not([disabled]) .split-gutter{cursor:col-resize;content:\" \";background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==)}deja-splitter[direction=vertical] split-area{height:100%}deja-splitter[direction=vertical]:not([disabled]) .split-gutter{cursor:row-resize;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFCAMAAABl/6zIAAAABlBMVEUAAADMzMzIT8AyAAAAAXRSTlMAQObYZgAAABRJREFUeAFjYGRkwIMJSeMHlBkOABP7AEGzSuPKAAAAAElFTkSuQmCC)}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }]; }, propDecorators: { dragStart: [{
                type: Output
            }], dragProgress: [{
                type: Output
            }], dragEnd: [{
                type: Output
            }], direction: [{
                type: Input
            }], gutterSize: [{
                type: Input
            }], styleFlexDirection: [{
                type: HostBinding,
                args: ['style.flex-direction']
            }], spliterAreas: [{
                type: ContentChildren,
                args: [SplitAreaDirective]
            }], splitting: [{
                type: HostBinding,
                args: ['attr.splitting']
            }], _direction: [{
                type: HostBinding,
                args: ['attr.direction']
            }], _disabled: [{
                type: HostBinding,
                args: ['attr.disabled']
            }], 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
 */

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

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

export { DejaSplitterComponent, DejaSplitterModule, SplitAreaDirective };
//# sourceMappingURL=deja-js-component-v2-splitter.mjs.map
