import {
    androidTypographyFixer
} from "../../utils";
import _defaultJSON from "../../variants/themes/default.json";

const defaultJSON = androidTypographyFixer(_defaultJSON);

export const mergeTypography = (
    defaultSpacesTokens: NCoreUIKit.Typography,
    projectSpacesTokens: NCoreUIKit.Typography
) => {
    return {
        ...defaultSpacesTokens,
        ...projectSpacesTokens
    };
};

export const mergeTypographyTokens = ({
    defaultTypographyTokens,
    projectTypographyTokens
}: {
    defaultTypographyTokens: NCoreUIKit.TypographyWithVariants;
    projectTypographyTokens: NCoreUIKit.TypographyWithVariants;
}) => {
    const defaultGapPropagations = Object.keys(defaultJSON.shapes.spaces) as (keyof NCoreUIKit.TypographyWithVariants)[];

    const defaultTypography = defaultTypographyTokens;

    const newTypography: NCoreUIKit.TypographyWithVariants = defaultTypography;

    if(!projectTypographyTokens) {
        return newTypography;
    }

    defaultGapPropagations.forEach((gP) => {
        if(projectTypographyTokens[gP]) {
            if(defaultTypography[gP]) {
                newTypography[gP] = mergeTypography(defaultTypography[gP], projectTypographyTokens[gP]);
            } else {
                newTypography[gP] = projectTypographyTokens[gP];
            }
        }
    });

    return newTypography;
};
