import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { getHierarchyRef, idRef, } from "@gooddata/sdk-model";
import { DRILL_TARGET_TYPE, isDrillToAttributeUrlConfig, isDrillToCustomUrlConfig, isDrillToDashboardConfig, isDrillToUrlConfig, } from "../../../../drill/types.js";
import { DrillTargetAttributeHierarchyItem } from "./DrillTargetAttributeHierarchyItem.js";
import { DrillTargetDashboardItem } from "./DrillTargetDashboardItem.js";
import { DrillTargetInsightItem } from "./DrillTargetInsightItem.js";
import { DrillTargetUrlItem } from "./DrillTargetUrlItem.js";
export function DrillTargets(props) {
    const { item, onDeleteInteraction } = props;
    const onDrillDownTargetSelect = (targetItem) => {
        const hierarchyRef = getHierarchyRef(targetItem);
        const drillDownItem = {
            attributeHierarchyRef: item.attributeHierarchyRef,
            type: "drillDownAttributeHierarchy",
            attributes: item.attributes,
            originLocalIdentifier: item.originLocalIdentifier,
            drillIntersectionIgnoredAttributes: item.drillIntersectionIgnoredAttributes ?? [],
        };
        props.onSetup(drillDownItem, { ...item, attributeHierarchyRef: hierarchyRef });
    };
    const onInsightTargetSelect = (targetItem) => {
        const drillConfigItem = {
            localIdentifier: item.localIdentifier,
            transition: "pop-up",
            origin: getOrigin(item),
            type: "drillToInsight",
            target: targetItem.insight.ref,
            drillIntersectionIgnoredAttributes: item.drillIntersectionIgnoredAttributes,
        };
        props.onSetup(drillConfigItem, { ...item, insightRef: targetItem.insight.ref });
    };
    const onDashboardTargetSelect = (targetItem, selectedTab) => {
        const dashboard = idRef(targetItem.identifier, "analyticalDashboard");
        const drillConfigItem = {
            localIdentifier: item.localIdentifier,
            transition: "in-place",
            origin: getOrigin(item),
            type: "drillToDashboard",
            target: dashboard,
            targetTabLocalIdentifier: selectedTab,
            drillIntersectionIgnoredAttributes: item.drillIntersectionIgnoredAttributes,
        };
        props.onSetup(drillConfigItem, { ...item, dashboard, dashboardTab: selectedTab });
    };
    const onCustomUrlTargetSelect = (urlDrillTarget) => {
        if (isDrillToCustomUrlConfig(urlDrillTarget)) {
            const drillConfigItem = {
                localIdentifier: item.localIdentifier,
                transition: "new-window",
                origin: getOrigin(item),
                type: "drillToCustomUrl",
                target: {
                    url: urlDrillTarget.customUrl,
                },
            };
            props.onSetup(drillConfigItem, { ...item, urlDrillTarget });
        }
        if (isDrillToAttributeUrlConfig(urlDrillTarget)) {
            const drillConfigItem = {
                localIdentifier: item.localIdentifier,
                transition: "new-window",
                origin: getOrigin(item),
                type: "drillToAttributeUrl",
                target: {
                    hyperlinkDisplayForm: urlDrillTarget.drillToAttributeDisplayForm,
                    displayForm: urlDrillTarget.insightAttributeDisplayForm,
                },
            };
            props.onSetup(drillConfigItem, { ...item, urlDrillTarget });
        }
    };
    switch (props.item.drillTargetType) {
        case DRILL_TARGET_TYPE.DRILL_TO_DASHBOARD:
            return (_jsx(DrillTargetDashboardItem, { selected: isDrillToDashboardConfig(item) ? item.dashboard : undefined, selectedTab: isDrillToDashboardConfig(item) ? item.dashboardTab : undefined, onSelect: onDashboardTargetSelect }));
        case DRILL_TARGET_TYPE.DRILL_TO_INSIGHT:
            return _jsx(DrillTargetInsightItem, { insight: item, onSelect: onInsightTargetSelect });
        case DRILL_TARGET_TYPE.DRILL_TO_URL:
            return (_jsx(DrillTargetUrlItem, { widgetRef: item.widgetRef, urlDrillTarget: isDrillToUrlConfig(item) ? item.urlDrillTarget : undefined, attributes: item.attributes, onSelect: onCustomUrlTargetSelect }));
        case DRILL_TARGET_TYPE.DRILL_DOWN:
            return (_jsx(DrillTargetAttributeHierarchyItem, { onSelect: onDrillDownTargetSelect, onDeleteInteraction: onDeleteInteraction, config: item }));
        case undefined:
            return null;
        default:
            unknownDrillTargetTypeReceived(props.item.drillTargetType);
    }
    function unknownDrillTargetTypeReceived(targetType) {
        throw new Error(`unknown drill target type: ${targetType}`);
    }
    return null;
}
function getOrigin(item) {
    return item.type === "attribute"
        ? {
            type: "drillFromAttribute",
            attribute: { localIdentifier: item.originLocalIdentifier },
        }
        : {
            type: "drillFromMeasure",
            measure: { localIdentifier: item.originLocalIdentifier },
        };
}
