// (C) 2025-2026 GoodData Corporation
import { invariant } from "ts-invariant";
import { ClientFormatterFacade } from "@gooddata/number-formatter";
import { areObjRefsEqual, dashboardAttributeFilterItemDisplayForm, isAttributeElementsByRef, isDashboardArbitraryAttributeFilter, isDashboardAttributeFilter, isDashboardMatchAttributeFilter, objRefToString, } from "@gooddata/sdk-model";
import { DateFilterHelpers } from "@gooddata/sdk-ui-filters";
import { DEFAULT_MEASURE_FORMAT } from "./const.js";
//Format value
export function formatValue(definition, value, separators) {
    const val = ClientFormatterFacade.formatValue(value, definition.metric.measure.format ?? DEFAULT_MEASURE_FORMAT, separators);
    return val.formattedValue;
}
//Format title
/**
 * @beta
 */
export function formatKeyDriverAnalysisDateRange(range, splitter) {
    const [from, to] = range ?? [null, null];
    // Not defined
    if (!from || !to) {
        return " - ";
    }
    // NOTE: Normalize server-supplied pattern so date-fns uses ISO week tokens (I/R/etc.)
    // instead of locale-based ones.
    const pattern = toIsoPattern(from.format?.pattern ?? "yyyy-MM-dd");
    return DateFilterHelpers.formatAbsoluteDateRange(new Date(from.date), new Date(to.date), pattern, splitter);
}
//Attribute filters
export function getOnlyRelevantFilters(all, relevantAttrs) {
    return all.filter((f) => {
        const displayForm = dashboardAttributeFilterItemDisplayForm(f);
        return relevantAttrs.some((r) => areObjRefsEqual(displayForm, r.attribute) ||
            r.displayForms.some((df) => areObjRefsEqual(displayForm, df.ref)));
    });
}
export function createNewAttributeFilter(attr, displayForm, id, value) {
    const displayFormOf = attr.displayForms.find((df) => areObjRefsEqual(df.ref, displayForm));
    invariant(displayFormOf);
    const dashboardFilter = {
        attributeFilter: {
            displayForm: displayFormOf.ref,
            negativeSelection: !value,
            attributeElements: {
                values: value ? [value] : [],
            },
            localIdentifier: `${objRefToString(displayFormOf.ref)}_${id}`,
            title: displayFormOf.title,
            selectionMode: "multi",
        },
    };
    return dashboardFilter;
}
export function updateExistingAttributeFilter(f, val, primaryDisplayForm) {
    if (isDashboardAttributeFilter(f)) {
        const attributeElements = isAttributeElementsByRef(f.attributeFilter.attributeElements)
            ? { uris: [val] }
            : { values: [val] };
        return {
            ...f,
            attributeFilter: {
                ...f.attributeFilter,
                negativeSelection: false,
                attributeElements,
            },
        };
    }
    if (isDashboardArbitraryAttributeFilter(f)) {
        return {
            attributeFilter: {
                displayForm: primaryDisplayForm ?? f.arbitraryAttributeFilter.displayForm,
                negativeSelection: false,
                attributeElements: { uris: [val] },
                localIdentifier: f.arbitraryAttributeFilter.localIdentifier,
                title: f.arbitraryAttributeFilter.title,
                selectionMode: "multi",
            },
        };
    }
    if (isDashboardMatchAttributeFilter(f)) {
        return {
            attributeFilter: {
                displayForm: primaryDisplayForm ?? f.matchAttributeFilter.displayForm,
                negativeSelection: false,
                attributeElements: { uris: [val] },
                localIdentifier: f.matchAttributeFilter.localIdentifier,
                title: f.matchAttributeFilter.title,
                selectionMode: "multi",
            },
        };
    }
    return f;
}
export function clearSummaryValue(definition) {
    return {
        ...(definition
            ? {
                fromValue: { ...definition?.range[0], value: undefined },
                toValue: { ...definition?.range[1], value: undefined },
            }
            : {}),
    };
}
const ISO_TOKEN_MAP = [
    [/(Y{1,4})/g, (match) => match.replace(/Y/g, "R")],
    [/(w{1,2})/g, (match) => match.replace(/w/g, "I")],
    [/(e{1,4})/g, (match) => match.replace(/e/g, "i")],
];
function toIsoPattern(pattern) {
    return ISO_TOKEN_MAP.reduce((acc, [regex, replacer]) => acc.replace(regex, replacer), pattern);
}
