import { getUniqueName } from '../../../../../../../../../utils/get-unique-name';
import { addValues, deleteByPaths } from '../../../../../../../../commands';
const getDuplicate = (entity, parent) => {
    const duplicate = structuredClone(entity);
    duplicate.name = getUniqueName(duplicate.name, parent);
    return duplicate;
};
const isPathCorrect = (path, isStateDestination) => {
    if (isStateDestination) {
        return path.at(-2) === 'states';
    }
    return path.at(-2) === 'substates';
};
export const moveByPaths = (sourcePaths, destinationPath) => (dispatch, getState) => {
    const isStateDestination = destinationPath.at(-1) === 'states';
    const destination = getState(destinationPath);
    if (!Array.isArray(destination)) {
        return;
    }
    const filteredPaths = sourcePaths.filter((path) => isPathCorrect(path, isStateDestination));
    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(addValues(destinationPath, values));
        dispatch(deleteByPaths(filteredPaths, true));
    }
};
