import get from 'lodash/get';

export const patchViewImages = configuration => {
    const viewImages = get(configuration, 'visualizationData.viewImages');
    const viewIdentifiers = viewImages && Object.keys(viewImages);
    if (viewIdentifiers && viewIdentifiers.length) {
        return {
            ...configuration,
            visualizationData: {
                ...configuration.visualizationData,
                viewImages: viewIdentifiers.reduce(
                    (result, identifier) => ({
                        ...result,
                        // remove prefix to make it matchable with design views of design data
                        [identifier.replace(/^[^_]+_/, '')]: viewImages[identifier],
                    }),
                    {}
                ),
            },
        };
    }
    return configuration;
};
