import defaultJSON from "../../variants/themes/default.json";

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

export const mergeRadiuses = (
    defaultRadiusesTokens: NCoreUIKit.Radiuses,
    projectRadiusesTokens: NCoreUIKit.Radiuses
) => {
    return {
        ...defaultRadiusesTokens,
        ...projectRadiusesTokens
    };
};

export const mergeBorders = (
    defaultBordersTokens: NCoreUIKit.Borders,
    projectBordersTokens: NCoreUIKit.Borders
) => {
    return {
        ...defaultBordersTokens,
        ...projectBordersTokens
    };
};

export const mergeInlineSpaces = (
    defaultInlineSpacesTokens: NCoreUIKit.InlineSpaces,
    projectInlineSpacesTokens: NCoreUIKit.InlineSpaces
) => {
    return {
        ...defaultInlineSpacesTokens,
        ...projectInlineSpacesTokens
    };
};

export const mergeShapes = ({
    defaultShapes,
    projectShapes
}: {
    defaultShapes: NCoreUIKit.Shapes;
    projectShapes: NCoreUIKit.Shapes;
}) => {
    const defaultGapPropagations = Object.keys(defaultJSON.shapes.spaces) as (keyof NCoreUIKit.SpacesWithVariants)[];
    const defaultSharpness = Object.keys(defaultJSON.shapes.radiuses) as (keyof NCoreUIKit.RadiusesWithVariants)[];

    const defaultInlineSpaces = defaultShapes.inlineSpaces;
    const defaultRadiuses = defaultShapes.radiuses;
    const defaultBorders = defaultShapes.borders;
    const defaultSpaces = defaultShapes.spaces;

    const newShapes: NCoreUIKit.Shapes = {
        inlineSpaces: defaultInlineSpaces,
        radiuses: defaultRadiuses,
        borders: defaultBorders,
        spaces: defaultSpaces
    };

    if(!projectShapes) {
        return newShapes;
    }

    if(projectShapes.borders) {
        const projectBorders = projectShapes.borders;

        newShapes.borders = mergeBorders(defaultBorders, projectBorders);
    }

    if(projectShapes.inlineSpaces) {
        const projectInlineSpaces = projectShapes.inlineSpaces;

        newShapes.inlineSpaces = mergeInlineSpaces(defaultInlineSpaces, projectInlineSpaces);
    }

    if(projectShapes.spaces) {
        defaultGapPropagations.forEach((gP) => {
            if(projectShapes.spaces[gP]) {
                if(defaultSpaces[gP]) {
                    newShapes.spaces[gP] = mergeSpaces(defaultSpaces[gP], projectShapes.spaces[gP]);
                } else {
                    newShapes.spaces[gP] = projectShapes.spaces[gP];
                }
            }
        });
    }

    if(projectShapes.radiuses) {
        defaultSharpness.forEach((s) => {
            if(projectShapes.radiuses[s]) {
                if(defaultRadiuses[s]) {
                    newShapes.radiuses[s] = mergeRadiuses(defaultRadiuses[s], projectShapes.radiuses[s]);
                } else {
                    newShapes.radiuses[s] = projectShapes.radiuses[s];
                }
            }
        });
    }

    return newShapes;
};
