import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { useMemo } from "react";
import { useIntl } from "react-intl";
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectAccessibleDashboards } from "../../../../../model/store/accessibleDashboards/accessibleDashboardsSelectors.js";
import { selectInaccessibleDashboards } from "../../../../../model/store/inaccessibleDashboards/inaccessibleDashboardsSelectors.js";
import { DashboardList } from "../../../../dashboardList/DashboardList.js";
import { DrillTargetDashboardTabSelector } from "./DrillTargetDashboardTabSelector.js";
const buildDashboardItems = (dashboards, forbiddenDashboards, intl, selected) => {
    const isAvailableDashboardSelected = dashboards.some((dashboard) => areObjRefsEqual(dashboard.ref, selected));
    if (!selected || isAvailableDashboardSelected) {
        return dashboards;
    }
    const selectedForbiddenItem = forbiddenDashboards.find(({ ref }) => areObjRefsEqual(ref, selected));
    if (selectedForbiddenItem === undefined) {
        return dashboards;
    }
    const { title, accessibilityLimitation } = selectedForbiddenItem;
    const forbiddenItem = {
        ...selectedForbiddenItem,
        title: accessibilityLimitation === "forbidden"
            ? intl.formatMessage({ id: "configurationPanel.drillConfig.forbiddenDashboard" })
            : title,
    };
    return [forbiddenItem, ...dashboards];
};
export function DrillTargetDashboardItem({ onSelect, selected, selectedTab, }) {
    const intl = useIntl();
    const dashboards = useDashboardSelector(selectAccessibleDashboards);
    const inaccessibleDashboards = useDashboardSelector(selectInaccessibleDashboards);
    const dashboardItems = useMemo(() => {
        return buildDashboardItems(dashboards, inaccessibleDashboards, intl, selected);
    }, [dashboards, inaccessibleDashboards, intl, selected]);
    // Find the selected dashboard and check if it has tabs
    const selectedDashboard = useMemo(() => {
        return selected ? dashboardItems.find((item) => areObjRefsEqual(item.ref, selected)) : undefined;
    }, [dashboardItems, selected]);
    // Check if selected dashboard has tabs (tabs property exists on dashboard items)
    const dashboardTabs = useMemo(() => {
        return selectedDashboard?.tabs || [];
    }, [selectedDashboard]);
    const handleTabSelect = (tabId) => {
        if (selectedDashboard) {
            onSelect(selectedDashboard, tabId);
        }
    };
    const handleDashboardSelect = (dashboard) => {
        // Check if this dashboard has tabs
        const dashboardWithTabs = dashboards.find((d) => areObjRefsEqual(d.ref, dashboard.ref));
        const tabs = dashboardWithTabs?.tabs || [];
        // If dashboard has tabs, default to first tab, otherwise no tab
        const defaultTabId = tabs.length > 1 ? tabs[0].localIdentifier : undefined;
        onSelect(dashboard, defaultTabId);
    };
    // Get the effective selected tab (use provided or default to first tab)
    const effectiveSelectedTab = selectedTab || (dashboardTabs.length > 0 ? dashboardTabs[0]?.localIdentifier : undefined);
    return (_jsxs(_Fragment, { children: [
            _jsx(DashboardList, { onSelect: handleDashboardSelect, dashboards: dashboardItems, selected: selected }), selected && dashboardTabs.length > 1 ? (_jsx(DrillTargetDashboardTabSelector, { tabs: dashboardTabs, selectedTabId: effectiveSelectedTab, onSelect: handleTabSelect })) : null] }));
}
