import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import { useEffect, useMemo, useState, useCallback, useContext, createContext, } from 'react';
import { filterNestedPaths } from '../../../utils/filter-nested-paths';
import { arraysEqual } from '../../../utils/arrays-equal';
import { HotkeysContext } from './hotkeys-provider';
import { HotkeysScopeContext } from './hotkeys-scope';
export const HotkeysSectionContext = createContext({
    clipboard: undefined,
    isCut: false,
    cut: () => { },
    copy: () => { },
    paste: () => { },
    remove: () => { },
});
export const HotkeysSectionProvider = ({ childrenFieldMap, rootPath, selectedPaths, onMoveTo, onCopyTo, onRemove, children, }) => {
    const scope = useContext(HotkeysScopeContext);
    const { addHotkeyListener, removeHotkeyListener } = useContext(HotkeysContext);
    const [clipboard, setClipboard] = useState();
    const [isCut, setIsCut] = useState(false);
    const handleCut = useCallback(() => {
        const paths = filterNestedPaths(selectedPaths);
        setClipboard(paths.length ? paths : undefined);
        setIsCut(true);
    }, [selectedPaths]);
    const handleCopy = useCallback(() => {
        const paths = filterNestedPaths(selectedPaths);
        setClipboard(paths.length ? paths : undefined);
        setIsCut(false);
    }, [selectedPaths]);
    const handlePaste = useCallback(() => {
        if (!clipboard) {
            return;
        }
        let destinationPath = rootPath;
        if (selectedPaths.length) {
            const shortestPath = selectedPaths.reduce((acc, path) => {
                if (path.length < acc.length) {
                    return path;
                }
                return acc;
            });
            const childrenField = childrenFieldMap[shortestPath.at(-2)];
            destinationPath = childrenField ? shortestPath.concat(childrenField) : rootPath;
        }
        if (destinationPath === undefined) {
            return;
        }
        if (isCut) {
            if (clipboard.every((path) => !arraysEqual(path.slice(0, -1), destinationPath))) {
                onMoveTo(clipboard, destinationPath);
            }
            setClipboard(undefined);
            setIsCut(false);
        }
        else {
            onCopyTo(clipboard, destinationPath);
        }
    }, [clipboard, isCut, selectedPaths, childrenFieldMap, rootPath, onMoveTo, onCopyTo]);
    const handleDelete = useCallback(() => {
        if (!selectedPaths.length) {
            return;
        }
        onRemove(selectedPaths);
    }, [selectedPaths, onRemove]);
    useEffect(() => {
        addHotkeyListener(scope, 'cut', handleCut);
        addHotkeyListener(scope, 'copy', handleCopy);
        addHotkeyListener(scope, 'paste', handlePaste);
        addHotkeyListener(scope, 'delete', handleDelete);
        return () => {
            removeHotkeyListener(scope, 'cut', handleCut);
            removeHotkeyListener(scope, 'copy', handleCopy);
            removeHotkeyListener(scope, 'paste', handlePaste);
            removeHotkeyListener(scope, 'delete', handleDelete);
        };
    }, [
        scope, addHotkeyListener, removeHotkeyListener,
        handleCut, handleCopy, handlePaste, handleDelete,
    ]);
    const currentContext = useMemo(() => ({
        clipboard,
        isCut,
        cut: handleCut,
        copy: handleCopy,
        paste: handlePaste,
        remove: handleDelete,
    }), [clipboard, isCut, handleCut, handleCopy, handlePaste, handleDelete]);
    return (_jsx(HotkeysSectionContext.Provider, { value: currentContext, children: children }));
};
