import { getUniqueName } from '../../../utils/get-unique-name';
import { addValues, deleteByPaths } from '..';
import { SCENE_PATH_LENGTH } from '../../../consts/paths';
const getDuplicate = (entity, parent) => {
    const duplicate = structuredClone(entity);
    duplicate.name = getUniqueName(duplicate.name, parent);
    return duplicate;
};
const isPathCorrect = (path, isActorDestination) => {
    if (isActorDestination) {
        return path.length > SCENE_PATH_LENGTH;
    }
    return path.length === SCENE_PATH_LENGTH;
};
export const moveByPaths = (sourcePaths, destinationPath) => (dispatch, getState) => {
    const isActorDestination = destinationPath.at(-1) !== 'scenes';
    const destination = getState(destinationPath);
    if (!Array.isArray(destination)) {
        return;
    }
    const filteredPaths = sourcePaths.filter((path) => isPathCorrect(path, isActorDestination));
    const { values } = filteredPaths.reduce((acc, path) => {
        const value = getState(path);
        if (value) {
            const duplicate = getDuplicate(value, acc.parent);
            acc.values.push(duplicate);
            acc.parent.push(duplicate);
        }
        return acc;
    }, { values: [], parent: destination.slice(0) });
    if (values.length) {
        dispatch(deleteByPaths(filteredPaths));
        dispatch(addValues(destinationPath, values, true));
    }
};
