import { Directive, EventEmitter, HostListener, Output } from '@angular/core';

const LONG_PRESS_TIMEOUT = 500;

export interface LongPressEvent {
    touchEnd?: boolean;
}

@Directive({
    selector: '[redactionLongPress]',
})
export class LongPressDirective {
    @Output() longPress = new EventEmitter();
    private _touchTimeout: any;

    @HostListener('window:keydown.control')
    @HostListener('window:keydown.meta')
    touchStart() {
        this._touchTimeout = setTimeout(() => {
            this.longPress.emit({});
        }, LONG_PRESS_TIMEOUT);
    }

    @HostListener('window:keyup.control')
    @HostListener('window:keyup.meta')
    touchEnd() {
        clearTimeout(this._touchTimeout);
        this.longPress.emit({ touchEnd: true });
    }
}
