import React, { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useDemographics } from "~/sdks/LocalDemographics/context";
import { Chart } from "~/components/deprecated";
export const PopulationByAgeGroup = () => {
    const { t } = useTranslation("localDemographics");
    const { demographicsState: { population_age: populationAge }, } = useDemographics();
    const populationAgeFormatted = useMemo(() => {
        if (!populationAge) {
            return null;
        }
        if ((populationAge === null || populationAge === void 0 ? void 0 : populationAge.variables.length) > 12) {
            const keys = populationAge === null || populationAge === void 0 ? void 0 : populationAge.variables.map((e) => {
                const labels = e.label.split(" ");
                labels.splice(1, 1);
                return labels;
            });
            const mergedPopulation = [];
            keys.forEach((element, index) => {
                if (index === 0 || index % 2 === 0) {
                    mergedPopulation.push({
                        variable: `age_${element[0]}_${keys[index + 1][1]}`,
                        value: populationAge.variables[index].value +
                            populationAge.variables[index + 1].value,
                        label: `${element[0]} to ${keys[index + 1][1]}`,
                    });
                }
            });
            return mergedPopulation;
        }
        return populationAge.variables;
    }, [populationAge]);
    const dataset = useMemo(() => ({
        labels: (populationAgeFormatted === null || populationAgeFormatted === void 0 ? void 0 : populationAgeFormatted.map((variable) => variable.label)) ||
            [],
        data: (populationAgeFormatted === null || populationAgeFormatted === void 0 ? void 0 : populationAgeFormatted.map((variable) => Number((variable.value * 100).toFixed(1)))) || [],
    }), [populationAgeFormatted]);
    const chartOptions = useMemo(() => ({
        title: t("data.population.graphs.populationByAgeGroup.title"),
        labels: dataset.labels,
        data: dataset.data,
        unit: "%",
    }), [dataset]);
    return (React.createElement(Chart.Root, Object.assign({}, chartOptions),
        React.createElement(Chart.BarChart, null)));
};
//# sourceMappingURL=index.js.map