import { Extensions, parseFileName, Prefixes, readFile } from './file-names';
import { pcdDropper } from './pcd-dropper';
import { plyDropper } from './ply-dropper';
import { snapshotDropper } from './snapshot-dropper';
const createFileDropper = (extension, prefix) => {
    switch (prefix) {
        case Prefixes.Snapshot: {
            return snapshotDropper;
        }
    }
    switch (extension) {
        case Extensions.PCD: {
            return pcdDropper;
        }
        case Extensions.PLY: {
            return plyDropper;
        }
    }
    return undefined;
};
export const useFileDrop = (onSuccess, onError) => {
    let dropState = $state('inactive');
    // prevent default to allow drop
    const ondragenter = (event) => {
        event.preventDefault();
        dropState = 'hovering';
    };
    // prevent default to allow drop
    const ondragover = (event) => {
        event.preventDefault();
    };
    const ondragleave = (event) => {
        // only deactivate if really leaving the window
        if (event.relatedTarget === null) {
            dropState = 'inactive';
        }
    };
    const handleError = (error) => {
        onError(error);
        dropState = 'inactive';
    };
    const ondrop = (event) => {
        event.preventDefault();
        if (event.dataTransfer === null)
            return;
        const { files } = event.dataTransfer;
        let completed = 0;
        for (const file of files) {
            const fileName = parseFileName(file.name);
            if (!fileName.success) {
                handleError(fileName.error.message);
                continue;
            }
            const { extension, prefix } = fileName;
            const reader = new FileReader();
            reader.addEventListener('loadend', () => {
                completed += 1;
                if (completed === files.length) {
                    dropState = 'inactive';
                }
            });
            reader.addEventListener('error', (event) => {
                const error = event.target?.error?.message;
                console.error(`${file.name} failed to load.`, error);
                handleError(`${file.name} failed to load.`);
            });
            reader.addEventListener('load', async (event) => {
                const content = event.target?.result;
                const dropper = createFileDropper(extension, prefix);
                if (!dropper) {
                    handleError(`${file.name} is not a supported file type.`);
                    return;
                }
                const result = await dropper({
                    name: file.name,
                    extension,
                    prefix,
                    content,
                });
                if (result.success) {
                    onSuccess(result);
                }
                else {
                    handleError(result.error.message);
                }
            });
            readFile(file, reader, extension);
            dropState = 'loading';
        }
    };
    return {
        get dropState() {
            return dropState;
        },
        ondrop,
        ondragenter,
        ondragover,
        ondragleave,
    };
};
