const omitKeys = ["key", "total", "subtotal_12_months"];
function getDateFromKey(date) {
    const jsDate = new Date();
    const [year, month] = date.split("-");
    jsDate.setUTCFullYear(Number(year));
    jsDate.setUTCMonth(Number(month) - 1);
    jsDate.setUTCDate(1);
    return jsDate;
}
export function formatDataForGraph(marketData, median, yearlyChange) {
    const graphDataKeys = [];
    const categories = [];
    const cleanData = Object.entries(marketData).filter(([key]) => !omitKeys.includes(key));
    const lastMonth = cleanData[cleanData.length - 1][0];
    // Get lastMonth from endpoint response to generate two arrays
    // graphDataKeys: last 12 dates in format yyyy-mm to compare with endpoint response
    // categories: x-axis short version of the 12 previous Months from lastMonth
    for (let i = 0; i <= 12; i += 1) {
        const lastMonthDate = getDateFromKey(lastMonth);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - i);
        graphDataKeys.push(lastMonthDate.toISOString().slice(0, 7));
        categories.push(lastMonthDate.toLocaleString("default", { month: "short" }));
    }
    // Reverse both objects to display graph from oldest to newest date
    graphDataKeys.reverse();
    categories.reverse();
    // Loop through the last 12 months, IF endpoint response has information add it to data
    // IF not return null to show a gap on the graph
    const data = graphDataKeys.map((graphKey) => {
        const hasData = cleanData.find(([key, _value]) => key === graphKey);
        let value = null;
        if (hasData) {
            value = Math.round(median ? hasData[1].median : hasData[1].raw_count);
        }
        return value;
    });
    const indicatorColor = yearlyChange && yearlyChange >= 0
        ? "var(--ll-color-primary)"
        : "var(--ll-color-error)";
    return {
        categories,
        series: [
            {
                data,
                color: indicatorColor,
            },
        ],
    };
}
//# sourceMappingURL=formatDataForGraph.js.map