import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { FormattedMessage } from "react-intl";
import { areObjRefsEqual, filterObjRef, insightMeasures, isLocalIdRef, measureAlias, measureItem, measureLocalId, measureTitle, objRefToString, } from "@gooddata/sdk-model";
import { MeasureValueFilterDetailsBubble } from "@gooddata/sdk-ui-filters/internal";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectAllCatalogDisplayFormsMap, selectAllCatalogMeasuresMap, } from "../../../../../model/store/catalog/catalogSelectors.js";
import { selectInsightByWidgetRef } from "../../../../../model/store/insights/insightsSelectors.js";
import { DropdownSectionHeader } from "../DropdownSectionHeader.js";
import { DisplayFormParam } from "./DisplayFormParam.js";
import { Parameter } from "./Parameter.js";
export function InsightParametersSection({ attributeDisplayForms, loadingAttributeDisplayForms, insightFilters, insightMeasureValueFilters, widgetRef, intl, onAdd, }) {
    const catalogDisplayForms = useDashboardSelector(selectAllCatalogDisplayFormsMap);
    const catalogMeasuresMap = useDashboardSelector(selectAllCatalogMeasuresMap);
    const insight = useDashboardSelector(selectInsightByWidgetRef(widgetRef));
    const measures = insight ? insightMeasures(insight) : [];
    return (_jsx(_Fragment, { children: (attributeDisplayForms && attributeDisplayForms.length > 0) ||
            loadingAttributeDisplayForms ||
            (insightFilters && insightFilters.length > 0) ||
            (insightMeasureValueFilters && insightMeasureValueFilters.length > 0) ? (_jsxs(_Fragment, { children: [
                _jsx(DropdownSectionHeader, { children: _jsx(FormattedMessage, { id: "configurationPanel.drillIntoUrl.editor.insightParametersSectionLabel" }) }), loadingAttributeDisplayForms ? (_jsx("div", { className: "gd-drill-to-url-section-loading s-drill-to-custom-url-attr-section-loading", children: _jsx("div", { className: "gd-spinner small" }) })) : (attributeDisplayForms?.map((item) => (_jsx(DisplayFormParam, { item: item.displayForm, onAdd: onAdd }, item.displayForm.id)))), insightFilters?.map((filter, index) => {
                    const displayFormRef = filterObjRef(filter);
                    const df = catalogDisplayForms.get(displayFormRef);
                    return (df && (_jsx(DisplayFormParam, { item: df, onAdd: () => onAdd(`{attribute_filter_selection(${df.id})}`), iconClassName: "gd-icon-filter", isFilter: true }, index)));
                }), insightMeasureValueFilters?.map((filter) => {
                    const { measure, localIdentifier } = filter.measureValueFilter;
                    const insightMeasure = getInsightMeasure(measure, measures);
                    const insightMeasureItem = insightMeasure ? measureItem(insightMeasure) : undefined;
                    const catalogMeasure = isLocalIdRef(measure)
                        ? insightMeasureItem && catalogMeasuresMap.get(insightMeasureItem)
                        : catalogMeasuresMap.get(measure);
                    const measureIdentifier = objRefToString(measure);
                    const resolvedTitle = getInsightMeasureTitle(insightMeasure) ??
                        catalogMeasure?.measure.title ??
                        measureIdentifier;
                    return (_jsx(Parameter, { name: resolvedTitle, description: measureIdentifier, detailTrigger: _jsx(MeasureValueFilterDetailsBubble, { title: resolvedTitle, requestHandler: catalogMeasure ? async () => catalogMeasure.measure : undefined }), iconClassName: "gd-icon-filter", onAdd: () => {
                            onAdd(`{mvf_condition(${measureIdentifier})}`);
                        }, intl: intl }, localIdentifier ?? measureIdentifier));
                })] })) : null }));
}
function getInsightMeasureTitle(measure) {
    return measure ? (measureAlias(measure) ?? measureTitle(measure)) : undefined;
}
function getInsightMeasure(measureRef, measures) {
    if (isLocalIdRef(measureRef)) {
        return measures.find((measure) => measureLocalId(measure) === measureRef.localIdentifier);
    }
    return measures.find((measure) => {
        const item = measureItem(measure);
        return item ? areObjRefsEqual(item, measureRef) : false;
    });
}
