import { preventDefaultAndStopPropagation } from "./file-validation.util";
import { FileUploadState } from "../types";
export class DragHandler {
    constructor() {
        this.currentState = FileUploadState.DEFAULT;
    }
    setErrorMessage(callback) {
        this.errorMessageCallback = callback;
    }
    setIsDisabled(callback) {
        this.isDisabledCallback = callback;
    }
    setValuesInDragHandler(state) {
        this.currentState = state;
    }
    onFileDrop(callback) {
        this.fileDropCallback = callback;
    }
    onStateChange(callback) {
        this.stateChangeCallback = callback;
    }
    setState(state) {
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.())
            return;
        this.currentState = state;
        this.stateChangeCallback?.(state);
    }
    dragEnter(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.()) {
            return;
        }
        this.setState(FileUploadState.DRAG);
    }
    dragLeave(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.()) {
            return;
        }
        this.setState(FileUploadState.DEFAULT);
    }
    dragOver(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.()) {
            return;
        }
    }
    drop(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.()) {
            return;
        }
        this.setState(FileUploadState.DEFAULT);
        this.handleFiles(e);
    }
    handleDocumentDrop(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED)
            return;
        this.setState(FileUploadState.DEFAULT);
    }
    preventDragOver(e) {
        preventDefaultAndStopPropagation(e);
    }
    preventDrop(e) {
        preventDefaultAndStopPropagation(e);
    }
    handleFiles(e) {
        preventDefaultAndStopPropagation(e);
        if (this.currentState === FileUploadState.DISABLED || this.isDisabledCallback?.())
            return;
        let files = null;
        if ('dataTransfer' in e && e.dataTransfer) {
            files = e.dataTransfer.files;
        }
        if (!files && e.target instanceof HTMLInputElement) {
            files = e.target.files;
        }
        if (files && files.length > 0) {
            const newFiles = Array.from(files);
            this.fileDropCallback?.(newFiles);
        }
        if (e.target instanceof HTMLInputElement) {
            e.target.value = "";
        }
    }
}
//# sourceMappingURL=drag-drop.util.js.map