import find from 'lodash/find';

const removeUndefinedPropertyValues = obj =>
    Object.keys(obj).reduce(
        (result, key) => (obj[key] !== undefined ? { ...result, [key]: obj[key] } : result),
        {}
    );

export const findCurrentFontItem = (fontList, matchCriteria) => {
    if (!fontList) {
        return null;
    }
    const match = find(fontList, removeUndefinedPropertyValues(matchCriteria));
    return match || find(fontList, { isDefault: true }) || fontList[0];
};

export const createFontItemProps = fontItem => ({
    family: fontItem.family,
    isCustomFont: fontItem.isCustomFont,
    customName: fontItem.customName,
    bold: fontItem.bold,
    italic: fontItem.italic,
    boldItalic: fontItem.boldItalic,
});
