import { PROFILE_TABS } from '../constants/profileTabs';

const getFilteredSettingsPageTabs = (dataPageTabs, settingsPageTabs) => {
    const dataPageTabValues = dataPageTabs.map(dataPageTab => dataPageTab.value);
    return settingsPageTabs.filter(
        ({ value }) => dataPageTabValues.includes(value)
    );
};

const getPageTabByValue = (pageTabs, pageTabValue) => pageTabs.filter(
    ({ value }) => value === pageTabValue
)[0];

const getIsVisible = (dataIsVisible, settingsIsVisible) => (
    dataIsVisible === undefined ? settingsIsVisible : dataIsVisible
);

export const buildCustomizedPageTabs = (dataPageTabs = [], settingsPageTabs = []) => {
    const filteredSettingsPageTabs = getFilteredSettingsPageTabs(dataPageTabs, settingsPageTabs);
    return filteredSettingsPageTabs.map(({
        label: settingsPageTabsLabel,
        value: settingsPageTabsValue,
        isVisible: settingsPageTabsIsVisible
    }) => {
        const {
            label: dataPageTabsLabel,
            href,
            value,
            selected,
            isVisible: dataPageTabsIsVisible
        } = getPageTabByValue(dataPageTabs, settingsPageTabsValue);
        const label = settingsPageTabsLabel || dataPageTabsLabel;
        return {
            href,
            isVisible: getIsVisible(dataPageTabsIsVisible, settingsPageTabsIsVisible),
            label,
            selected,
            value
        };
    });
};

export const isFeaturesTabVisible = pageTabs => {
    const featuresTab = getPageTabByValue(pageTabs, PROFILE_TABS.features.value);
    return featuresTab.isVisible;
};
