
export const isSameProductLineChildEntry = (
    navigationBarChildrenData,
    targetProductLine
) => navigationBarChildrenData.some(navigationBarChildEntry => {
    const { customData } = navigationBarChildEntry;
    return (customData && customData.externalUUID) === targetProductLine.id;
});

export const getFormattedProductLinesData = (productLinesData = []) => productLinesData.map(val => ({
    isDisabled: false,
    isVisible: true,
    customData: {
        externalUUID: val.id
    },
    link: {
        isLocalized: true,
        type: 'INPUT',
        value: ''
    },
    title: {
        isLocalized: true,
        type: 'INPUT',
        value: val.label
    },
    type: 'CUSTOM_LINK',
    editorType: 'PRODUCT_LINES'
}));


export const getProductLineIds = (productLines = []) => (productLines ? productLines.map(productLine => productLine.id)
    : []);

export const getProductLineMap = (productLines = []) => {
    const productLinesMap = new Map();
    productLines && productLines.forEach(({ id, label }) => id && label && productLinesMap.set(id, label));
    return productLinesMap;
};

export const getNavigationBarPrevData = (productLines, navigationBarData = []) => {
    const productLineIds = getProductLineIds(productLines);
    const productLineMap = getProductLineMap(productLines);
    const navigationBarPrevData = [];

    navigationBarData && navigationBarData.forEach(val => {
        const navigationBarDataEntry = val;
        const { customData, children, title } = navigationBarDataEntry;
        if ((customData && productLineIds.includes(customData.externalUUID))
            || !customData) {
            if (children) {
                navigationBarDataEntry.children = children.filter(child => {
                    const { customData: customDataChild, title: childTitle } = child;
                    const id = customDataChild && customDataChild.externalUUID;
                    childTitle.value = productLineMap.get(id) === undefined ? childTitle.value : productLineMap.get(id);
                    return productLineIds.includes(id) || !customDataChild;
                });
            }
            const id = customData && customData.externalUUID;
            title.value = productLineMap.get(id) === undefined ? title.value : productLineMap.get(id);
            navigationBarPrevData.push(navigationBarDataEntry);
        }
    });
    return navigationBarPrevData;
};

export const isSameProductLineEntry = (targetProductLine, compareProductLine) => {
    const { customData, children } = compareProductLine;
    return (targetProductLine.id === (customData && customData.externalUUID)
        || (children && isSameProductLineChildEntry(children, targetProductLine)));
};

export const getOnlyProductLines = (
    productLines, navigationBarData, compareFunction
) => productLines.filter(productLinesEntry => !navigationBarData.some(
    navigationBarDataEntry => compareFunction(productLinesEntry, navigationBarDataEntry)
));
