// (C) 2019-2026 GoodData Corporation
import { endOfDay, format, startOfDay, subMonths } from "date-fns";
import { groupBy, isEmpty, max, min } from "lodash-es";
// TODO: this import was coming from internal/esm and was wrecking tests
const PLATFORM_DATE_FORMAT = "yyyy-MM-dd HH:mm";
/**
 * Converts date filter config - as stored on the backend - into the date filter options that are aimed for
 * consumption by the actual date filtering view components.
 *
 * @param config - date filter config from backend
 */
export function convertDateFilterConfigToDateFilterOptions(config) {
    const allTime = convertAllTime(config.allTime);
    const emptyValues = convertEmptyValues(config.emptyValues);
    const absoluteForm = convertAbsoluteForm(config.absoluteForm);
    const relativeForm = convertRelativeForm(config.relativeForm);
    const absolutePreset = convertAbsolutePresets(config.absolutePresets);
    const relativePreset = convertRelativePresets(config.relativePresets);
    return removeEmptyKeysFromDateFilterOptions({
        allTime,
        emptyValues,
        absoluteForm,
        absolutePreset,
        relativeForm,
        relativePreset,
    });
}
function convertAllTime(filter) {
    return (filter && {
        ...filter,
        type: "allTime",
    });
}
function convertEmptyValues(filter) {
    return (filter && {
        ...filter,
        type: "emptyValues",
    });
}
function convertAbsoluteForm(filter) {
    return (filter && {
        ...filter,
        from: format(startOfDay(subMonths(new Date(), 1)), PLATFORM_DATE_FORMAT),
        to: format(endOfDay(new Date()), PLATFORM_DATE_FORMAT),
        type: "absoluteForm",
    });
}
function convertRelativeForm(filter) {
    return (filter && {
        from: undefined,
        // we order the granularities anyway, this lets the user to config the default
        granularity: filter.availableGranularities[0],
        localIdentifier: filter.localIdentifier,
        name: filter.name,
        to: undefined,
        type: "relativeForm",
        visible: filter.visible,
    });
}
function convertAbsolutePresets(filters) {
    return filters?.map((preset) => sanitizeDateFilterOption({
        ...preset,
        type: "absolutePreset",
    }));
}
function convertRelativePresets(filters) {
    return (filters &&
        groupBy(filters.map((preset) => sanitizeDateFilterOption({
            ...preset,
            type: "relativePreset",
        })), (preset) => preset.granularity));
}
function removeEmptyKeysFromDateFilterOptions({ absoluteForm, absolutePreset, allTime, emptyValues, relativeForm, relativePreset, }) {
    return {
        ...(allTime && { allTime }),
        ...(emptyValues && { emptyValues }),
        ...(absoluteForm && { absoluteForm }),
        ...(!isEmpty(absolutePreset) && { absolutePreset }),
        ...(relativeForm && { relativeForm }),
        ...(!isEmpty(relativePreset) && { relativePreset }),
    };
}
function sanitizeDateFilterOption(option) {
    return {
        ...option,
        from: min([option.from, option.to]),
        to: max([option.from, option.to]),
    };
}
