import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import { useContext, useMemo, useState, useEffect, } from 'react';
import { EngineContext, InspectedEntityContext, EntitySelectionContext } from '../../../../providers';
import { useConfig, useStore } from '../../../../hooks';
import { EventType } from '../../../../../events';
import { Tree } from '../tree';
import { getSavedSelectedSceneId } from '../../../../../utils/get-saved-selected-scene-id';
import { getIdByPath } from '../../../../../utils/get-id-by-path';
import { CHILDREN_FIELD_MAP } from '../../consts';
import { parseScenes, getInspectedKey, getSelectedPaths } from './utils';
export const ScenesTree = ({ className, onDrop }) => {
    const { world } = useContext(EngineContext);
    const { path: inspectedEntityPath } = useContext(InspectedEntityContext);
    const { paths: selectedEntitiesPaths } = useContext(EntitySelectionContext);
    const store = useStore();
    const scenes = useConfig('scenes');
    const [selectedScene, setSelectedScene] = useState(() => getSavedSelectedSceneId(store));
    useEffect(() => {
        const handleSceneChange = (event) => {
            if (event.sceneId !== selectedScene) {
                setSelectedScene(event.sceneId);
            }
        };
        world.addEventListener(EventType.SelectScene, handleSceneChange);
        return () => world.removeEventListener(EventType.SelectScene, handleSceneChange);
    }, [selectedScene]);
    const inactiveSelectedSceneId = useMemo(() => {
        const selectedIds = selectedEntitiesPaths.map((path) => getIdByPath(path));
        return selectedScene && !selectedIds.includes(selectedScene)
            ? selectedScene
            : undefined;
    }, [selectedScene, selectedEntitiesPaths]);
    const treeData = useMemo(() => parseScenes(scenes, inactiveSelectedSceneId), [scenes, inactiveSelectedSceneId]);
    return (_jsx(Tree, { className: className, treeData: treeData, inspectedKey: getInspectedKey(inspectedEntityPath), selectedPaths: getSelectedPaths(selectedEntitiesPaths), persistentStorageKey: "explorer.tab.scenes", draggable: true, onDrop: onDrop, childrenFieldMap: CHILDREN_FIELD_MAP }));
};
