import * as i0 from '@angular/core';
import { InjectionToken, Directive, Inject, HostBinding, Input, HostListener, forwardRef, Component, NgModule } from '@angular/core';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import { BehaviorSubject, Subject, map, takeUntil } from 'rxjs';

const RESIZABLE = new InjectionToken('Resizable');

class BsResizeGlyphDirective {
    constructor(resizable) {
        this.positions = '';
        this.glyphClass = true;
        this.activeClass = false;
        this.isBusy = false;
        this.resizable = resizable;
    }
    set bsResizeGlyph(value) {
        this.positions = value.join(' ');
    }
    onMouseDown(ev) {
        ev.preventDefault();
        this.onPointerDown();
    }
    onTouchStart(ev) {
        ev.preventDefault();
        this.onPointerDown();
    }
    onMouseMove(ev) {
        this.onPointerMove({ clientX: ev.clientX, clientY: ev.clientY, preventDefault: () => ev.preventDefault() });
    }
    onTouchMove(ev) {
        if (ev.touches.length === 1) {
            this.onPointerMove({ clientX: ev.touches[0].clientX, clientY: ev.touches[0].clientY, preventDefault: () => ev.preventDefault() });
        }
    }
    onMouseUp(ev) {
        this.onPointerUp();
    }
    onTouchEnd(ev) {
        this.onPointerUp();
    }
    onPointerDown() {
        var _a, _b, _c, _d;
        let action = {
            positioning: this.resizable.positioning
        };
        const rect = this.resizable.element.nativeElement.getBoundingClientRect();
        const styles = window.getComputedStyle(this.resizable.element.nativeElement);
        const marginLeft = (this.resizable.positioning === 'absolute') ? undefined : parseFloat(styles.marginLeft.slice(0, -2));
        const marginRight = (this.resizable.positioning === 'absolute') ? undefined : parseFloat(styles.marginRight.slice(0, -2));
        const marginTop = (this.resizable.positioning === 'absolute') ? undefined : parseFloat(styles.marginTop.slice(0, -2));
        const marginBottom = (this.resizable.positioning === 'absolute') ? undefined : parseFloat(styles.marginBottom.slice(0, -2));
        if ((_a = this.positions) === null || _a === void 0 ? void 0 : _a.includes('start')) {
            action = Object.assign(Object.assign({}, action), { end: {
                    edge: rect.right,
                    size: rect.width,
                    margin: marginRight,
                    dragMargin: marginLeft
                } });
        }
        if ((_b = this.positions) === null || _b === void 0 ? void 0 : _b.includes('end')) {
            action = Object.assign(Object.assign({}, action), { start: {
                    edge: rect.left,
                    size: rect.width,
                    margin: marginLeft,
                    dragMargin: marginRight
                } });
        }
        if ((_c = this.positions) === null || _c === void 0 ? void 0 : _c.includes('top')) {
            action = Object.assign(Object.assign({}, action), { bottom: {
                    edge: rect.bottom,
                    size: rect.height,
                    margin: marginBottom,
                    dragMargin: marginTop
                } });
        }
        if ((_d = this.positions) === null || _d === void 0 ? void 0 : _d.includes('bottom')) {
            action = Object.assign(Object.assign({}, action), { top: {
                    edge: rect.top,
                    size: rect.height,
                    margin: marginTop,
                    dragMargin: marginBottom
                } });
        }
        this.resizable.resizeAction = action;
        this.activeClass = true;
    }
    onPointerMove(ev) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (this.resizable.resizeAction && !this.isBusy) {
            ev.preventDefault();
            this.isBusy = true;
            const rct = this.resizable.element.nativeElement.getBoundingClientRect();
            if (this.resizable.resizeAction.start && ((_a = this.positions) === null || _a === void 0 ? void 0 : _a.includes('end'))) {
                // Right glyph
                const x = (ev.clientX < rct.left + 10) ? rct.left + 10 : ev.clientX;
                switch (this.resizable.positioning) {
                    case 'inline':
                        {
                            const initalMargin = (_b = this.resizable.marginRight) !== null && _b !== void 0 ? _b : 0;
                            this.resizable.marginRight = initalMargin - (x - rct.right);
                        }
                        break;
                    case 'absolute':
                        {
                            this.resizable.width = x - rct.left;
                        }
                        break;
                }
            }
            else if (this.resizable.resizeAction.end && ((_c = this.positions) === null || _c === void 0 ? void 0 : _c.includes('start'))) {
                // Left glyph
                const x = (ev.clientX > rct.right - 10) ? rct.right - 10 : ev.clientX;
                switch (this.resizable.positioning) {
                    case 'inline':
                        {
                            const initalMargin = (_d = this.resizable.marginLeft) !== null && _d !== void 0 ? _d : 0;
                            this.resizable.marginLeft = initalMargin + x - rct.left;
                        }
                        break;
                    case 'absolute':
                        {
                            this.resizable.left = x;
                            this.resizable.width = this.resizable.resizeAction.end.edge - x;
                        }
                        break;
                }
            }
            if (this.resizable.resizeAction.top && ((_e = this.positions) === null || _e === void 0 ? void 0 : _e.includes('bottom'))) {
                // Bottom glyph
                const y = (ev.clientY < rct.top + 10) ? rct.top + 10 : ev.clientY;
                switch (this.resizable.positioning) {
                    case 'inline':
                        {
                            const initalMargin = (_f = this.resizable.marginBottom) !== null && _f !== void 0 ? _f : 0;
                            this.resizable.height = y - rct.top;
                            this.resizable.marginBottom = initalMargin - (y - rct.bottom);
                        }
                        break;
                    case 'absolute':
                        {
                            this.resizable.height = y - rct.top;
                        }
                        break;
                }
            }
            else if (this.resizable.resizeAction.bottom && ((_g = this.positions) === null || _g === void 0 ? void 0 : _g.includes('top'))) {
                // Top glyph
                const y = (ev.clientY > rct.bottom - 10) ? rct.bottom - 10 : ev.clientY;
                switch (this.resizable.positioning) {
                    case 'inline':
                        {
                            const initalMargin = (_h = this.resizable.marginTop) !== null && _h !== void 0 ? _h : 0;
                            this.resizable.height = rct.bottom - y;
                            this.resizable.marginTop = initalMargin + y - rct.top;
                        }
                        break;
                    case 'absolute':
                        {
                            this.resizable.top = y;
                            this.resizable.height = this.resizable.resizeAction.bottom.edge - y;
                        }
                        break;
                }
            }
            this.isBusy = false;
        }
    }
    onPointerUp() {
        this.resizable.resizeAction = undefined;
        this.activeClass = false;
    }
}
BsResizeGlyphDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizeGlyphDirective, deps: [{ token: RESIZABLE }], target: i0.ɵɵFactoryTarget.Directive });
BsResizeGlyphDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.7", type: BsResizeGlyphDirective, selector: "[bsResizeGlyph]", inputs: { bsResizeGlyph: "bsResizeGlyph" }, host: { listeners: { "mousedown": "onMouseDown($event)", "touchstart": "onTouchStart($event)", "document:mousemove": "onMouseMove($event)", "touchmove": "onTouchMove($event)", "document:mouseup": "onMouseUp($event)", "touchend": "onTouchEnd($event)" }, properties: { "class": "this.positions", "class.glyph": "this.glyphClass", "class.active": "this.activeClass" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizeGlyphDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsResizeGlyph]'
                }]
        }], ctorParameters: function () {
        return [{ type: undefined, decorators: [{
                        type: Inject,
                        args: [RESIZABLE]
                    }] }];
    }, propDecorators: { positions: [{
                type: HostBinding,
                args: ['class']
            }], glyphClass: [{
                type: HostBinding,
                args: ['class.glyph']
            }], activeClass: [{
                type: HostBinding,
                args: ['class.active']
            }], bsResizeGlyph: [{
                type: Input
            }], onMouseDown: [{
                type: HostListener,
                args: ['mousedown', ['$event']]
            }], onTouchStart: [{
                type: HostListener,
                args: ['touchstart', ['$event']]
            }], onMouseMove: [{
                type: HostListener,
                args: ['document:mousemove', ['$event']]
            }], onTouchMove: [{
                type: HostListener,
                args: ['touchmove', ['$event']]
            }], onMouseUp: [{
                type: HostListener,
                args: ['document:mouseup', ['$event']]
            }], onTouchEnd: [{
                type: HostListener,
                args: ['touchend', ['$event']]
            }] } });

class BsResizableComponent {
    constructor(element) {
        //#region Positioning
        this.positioning$ = new BehaviorSubject('inline');
        this.classes = true;
        this.hostClass = null;
        this.destroyed$ = new Subject();
        this.element = element;
        this.hostPosition$ = this.positioning$.pipe(map((positioning) => {
            switch (positioning) {
                case 'absolute': return 'position-absolute';
                case 'inline': return 'position-relative';
            }
        }));
        this.wrapperPosition$ = this.positioning$.pipe(map((positioning) => {
            switch (positioning) {
                case 'absolute': return ['position-relative', 'h-100'];
                case 'inline': return [];
            }
        }));
        this.hostPosition$.pipe(takeUntil(this.destroyed$))
            .subscribe(hostPosition => this.hostClass = hostPosition);
    }
    get positioning() {
        return this.positioning$.value;
    }
    set positioning(value) {
        this.positioning$.next(value);
    }
    //#endregion
    set presetPosition(value) {
        if (this.positioning === 'inline') {
            throw 'presetPosition currently only supported in absolute positioning';
        }
        this.width = value.width;
        this.height = value.height;
        this.left = value.left;
        this.top = value.top;
        this.marginTop = this.marginBottom = this.marginLeft = this.marginRight = undefined;
    }
    ngOnDestroy() {
        this.destroyed$.next(true);
    }
}
BsResizableComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizableComponent, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
BsResizableComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.2.7", type: BsResizableComponent, selector: "bs-resizable", inputs: { positioning: "positioning", presetPosition: "presetPosition" }, host: { properties: { "style.margin-left.px": "this.marginLeft", "style.margin-right.px": "this.marginRight", "style.margin-top.px": "this.marginTop", "style.margin-bottom.px": "this.marginBottom", "style.width.px": "this.width", "style.height.px": "this.height", "style.left.px": "this.left", "style.top.px": "this.top", "class.d-block": "this.classes", "class.border": "this.classes", "class": "this.hostClass" } }, providers: [
        { provide: RESIZABLE, useExisting: forwardRef(() => BsResizableComponent) }
    ], ngImport: i0, template: "<div [class]=\"wrapperPosition$ | async\" class=\"h-100\">\n    <div class=\"cursor-nw-resize\" [bsResizeGlyph]=\"['top', 'start']\"></div>\n    <div class=\"cursor-n-resize\" [bsResizeGlyph]=\"['top']\"></div>\n    <div class=\"cursor-ne-resize\" [bsResizeGlyph]=\"['top', 'end']\"></div>\n    <div class=\"cursor-e-resize\" [bsResizeGlyph]=\"['end']\"></div>\n    <div class=\"cursor-se-resize\" [bsResizeGlyph]=\"['bottom', 'end']\"></div>\n    <div class=\"cursor-s-resize\" [bsResizeGlyph]=\"['bottom']\"></div>\n    <div class=\"cursor-sw-resize\" [bsResizeGlyph]=\"['bottom', 'start']\"></div>\n    <div class=\"cursor-w-resize\" [bsResizeGlyph]=\"['start']\"></div>\n    <ng-content></ng-content>\n</div>", styles: [".glyph{position:absolute;width:8px;height:8px;transition:background-color .2s ease-in-out;border-radius:4px;z-index:20}.glyph.top{top:-4px}.glyph.start{left:-4px}.glyph.bottom{bottom:-4px}.glyph.end{right:-4px}.glyph.start:not(.top):not(.bottom),.glyph.end:not(.top):not(.bottom){top:4px;height:calc(100% - 8px)}.glyph.top:not(.start):not(.end),.glyph.bottom:not(.start):not(.end){left:4px;width:calc(100% - 8px)}.glyph:hover,.glyph.active{background-color:#1389fd}:host{min-width:2rem;min-height:2rem}\n"], dependencies: [{ kind: "directive", type: BsResizeGlyphDirective, selector: "[bsResizeGlyph]", inputs: ["bsResizeGlyph"] }, { kind: "pipe", type: i2.AsyncPipe, name: "async" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizableComponent, decorators: [{
            type: Component,
            args: [{ selector: 'bs-resizable', providers: [
                        { provide: RESIZABLE, useExisting: forwardRef(() => BsResizableComponent) }
                    ], template: "<div [class]=\"wrapperPosition$ | async\" class=\"h-100\">\n    <div class=\"cursor-nw-resize\" [bsResizeGlyph]=\"['top', 'start']\"></div>\n    <div class=\"cursor-n-resize\" [bsResizeGlyph]=\"['top']\"></div>\n    <div class=\"cursor-ne-resize\" [bsResizeGlyph]=\"['top', 'end']\"></div>\n    <div class=\"cursor-e-resize\" [bsResizeGlyph]=\"['end']\"></div>\n    <div class=\"cursor-se-resize\" [bsResizeGlyph]=\"['bottom', 'end']\"></div>\n    <div class=\"cursor-s-resize\" [bsResizeGlyph]=\"['bottom']\"></div>\n    <div class=\"cursor-sw-resize\" [bsResizeGlyph]=\"['bottom', 'start']\"></div>\n    <div class=\"cursor-w-resize\" [bsResizeGlyph]=\"['start']\"></div>\n    <ng-content></ng-content>\n</div>", styles: [".glyph{position:absolute;width:8px;height:8px;transition:background-color .2s ease-in-out;border-radius:4px;z-index:20}.glyph.top{top:-4px}.glyph.start{left:-4px}.glyph.bottom{bottom:-4px}.glyph.end{right:-4px}.glyph.start:not(.top):not(.bottom),.glyph.end:not(.top):not(.bottom){top:4px;height:calc(100% - 8px)}.glyph.top:not(.start):not(.end),.glyph.bottom:not(.start):not(.end){left:4px;width:calc(100% - 8px)}.glyph:hover,.glyph.active{background-color:#1389fd}:host{min-width:2rem;min-height:2rem}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { positioning: [{
                type: Input
            }], presetPosition: [{
                type: Input
            }], marginLeft: [{
                type: HostBinding,
                args: ['style.margin-left.px']
            }], marginRight: [{
                type: HostBinding,
                args: ['style.margin-right.px']
            }], marginTop: [{
                type: HostBinding,
                args: ['style.margin-top.px']
            }], marginBottom: [{
                type: HostBinding,
                args: ['style.margin-bottom.px']
            }], width: [{
                type: HostBinding,
                args: ['style.width.px']
            }], height: [{
                type: HostBinding,
                args: ['style.height.px']
            }], left: [{
                type: HostBinding,
                args: ['style.left.px']
            }], top: [{
                type: HostBinding,
                args: ['style.top.px']
            }], classes: [{
                type: HostBinding,
                args: ['class.d-block']
            }, {
                type: HostBinding,
                args: ['class.border']
            }], hostClass: [{
                type: HostBinding,
                args: ['class']
            }] } });

class BsResizableModule {
}
BsResizableModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizableModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
BsResizableModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.7", ngImport: i0, type: BsResizableModule, declarations: [BsResizableComponent,
        BsResizeGlyphDirective], imports: [CommonModule], exports: [BsResizableComponent,
        BsResizeGlyphDirective] });
BsResizableModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizableModule, imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsResizableModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        BsResizableComponent,
                        BsResizeGlyphDirective
                    ],
                    imports: [
                        CommonModule
                    ],
                    exports: [
                        BsResizableComponent,
                        BsResizeGlyphDirective
                    ]
                }]
        }] });

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

export { BsResizableComponent, BsResizableModule, BsResizeGlyphDirective, RESIZABLE };
//# sourceMappingURL=mintplayer-ng-bootstrap-resizable.mjs.map
