import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { FormattedMessage } from "react-intl";
import { areObjRefsEqual, bucketsAttributes, insightBuckets } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectCatalogAttributeDisplayForms, selectCatalogDateDatasets, } from "../../../../model/store/catalog/catalogSelectors.js";
import { selectInsightByWidgetRef } from "../../../../model/store/insights/insightsSelectors.js";
import { DrillIntersectionIgnoredAttributesSelect, } from "./DrillIntersectionIgnoredAttributesSelect.js";
export function DrillIntersectionIgnoredAttributes({ item, onChange, drillTargetType, }) {
    const insight = useDashboardSelector(selectInsightByWidgetRef(item.widgetRef));
    const insightAttributes = bucketsAttributes(insight ? insightBuckets(insight) : []);
    const allCatalogDisplayForms = useDashboardSelector(selectCatalogAttributeDisplayForms);
    const allDateDatasets = useDashboardSelector(selectCatalogDateDatasets);
    const allCatalogDateAttributeDisplayForms = allDateDatasets
        .flatMap((ds) => ds.dateAttributes)
        .flatMap((da) => da.defaultDisplayForm);
    const displayForms = insightAttributes.map((idf) => {
        const catalogAttr = allCatalogDisplayForms.find((df) => areObjRefsEqual(df.ref, idf.attribute.displayForm));
        const catalogDateAttribute = allCatalogDateAttributeDisplayForms.find((df) => areObjRefsEqual(df.ref, idf.attribute.displayForm));
        const type = catalogDateAttribute ? "date" : "attribute";
        return {
            insightAttr: idf,
            catalogAttr: catalogAttr ?? catalogDateAttribute,
            type,
        };
    });
    const options = displayForms.map((df) => ({
        id: df.insightAttr.attribute.localIdentifier,
        title: df.catalogAttr?.title ?? "",
        type: df.type,
    }));
    const selection = item.drillIntersectionIgnoredAttributes?.map((id) => {
        const option = options.find((o) => o.id === id);
        return {
            id,
            title: option?.title ?? "",
            type: option?.type ?? "attribute",
        };
    }) ?? [];
    if (insightAttributes.length === 0) {
        return null;
    }
    return (_jsxs("div", { className: "gd-drill-intersection-ignored-attributes-select-section", children: [
            _jsx("div", { className: "gd-drill-intersection-ignored-attributes-select-label", children: _jsx(FormattedMessage, { id: "configurationPanel.drillConfig.drillIntersectionIgnoredAttributes.label" }) }), _jsx(DrillIntersectionIgnoredAttributesSelect, { drillTargetType: drillTargetType, initialSelection: selection, options: options, onApply: (selection) => {
                    onChange(selection.map((s) => s.id));
                } })
        ] }));
}
