const MAX_SCORE_VALUE = 10;
const SCORE_MULTIPLIER = 2;
/**
 * Transforms the API response from the scores endpoint into a more usable format.
 *
 * @param data
 * @param scoresMenu
 * @returns
 */
export function transformScoresAPIResponse(data, scoresMenu) {
    if (typeof (data === null || data === void 0 ? void 0 : data.location) === "undefined" || !scoresMenu)
        return {};
    const scores = {};
    Object.keys(data.location).forEach((key) => {
        var _a;
        const singleScoreData = (_a = data === null || data === void 0 ? void 0 : data.location) === null || _a === void 0 ? void 0 : _a[key];
        const category = scoresMenu.categories.find(([_key, categoryData]) => categoryData.scores.some(([scoreKey]) => scoreKey === key));
        let singleScoreValue = parseFloat((singleScoreData === null || singleScoreData === void 0 ? void 0 : singleScoreData.value) || "0") * SCORE_MULTIPLIER;
        if (Number.isNaN(singleScoreValue)) {
            singleScoreValue = 0;
        }
        else if (singleScoreValue > MAX_SCORE_VALUE) {
            singleScoreValue = MAX_SCORE_VALUE;
        }
        const geographies = {};
        if (typeof (data === null || data === void 0 ? void 0 : data.geographies) !== "undefined") {
            data.geographies.forEach((geography) => {
                var _a;
                if (typeof (geography === null || geography === void 0 ? void 0 : geography.scores) !== "undefined" &&
                    typeof geography.scores[key] !== "undefined") {
                    const geographyValue = parseFloat(((_a = geography.scores[key]) === null || _a === void 0 ? void 0 : _a.value) || "0") *
                        SCORE_MULTIPLIER;
                    if (typeof (geography === null || geography === void 0 ? void 0 : geography.geog_id) === "string") {
                        geographies[geography.geog_id.split("_")[0]] = {
                            text: geography.name,
                            value: Math.round(geographyValue * 10) / 10,
                        };
                    }
                }
            });
        }
        scores[key] = {
            text: singleScoreData === null || singleScoreData === void 0 ? void 0 : singleScoreData.text,
            value: Math.round(singleScoreValue * 10) / 10,
            geographies,
            categoryKey: category ? category[0] : null,
            categoryOptions: category ? category[1] : null,
        };
    });
    return scores;
}
//# sourceMappingURL=index.js.map