// (C) 2021-2026 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { DEFAULT_TAB_ID } from "./tabsState.js";
const selectSelf = createSelector((state) => state, (state) => state.tabs);
/**
 * Returns the full tabs state (including tabs array, activeTabLocalIdentifier, and shared state like attributesWithReferences).
 *
 * @internal
 */
export const selectTabsState = selectSelf;
/**
 * Returns all tabs with their configurations.
 *
 * @alpha
 */
export const selectTabs = createSelector(selectSelf, (tabsState) => {
    return tabsState.tabs;
});
/**
 * Returns the local identifier of the currently active tab.
 *
 * @alpha
 */
export const selectActiveTabLocalIdentifier = createSelector(selectSelf, (tabsState) => {
    return tabsState.activeTabLocalIdentifier;
});
/**
 * Returns the local identifier of the currently active tab or the default tab id
 *
 * @alpha
 */
export const selectActiveOrDefaultTabLocalIdentifier = createSelector(selectSelf, (tabsState) => {
    return tabsState.activeTabLocalIdentifier ?? DEFAULT_TAB_ID;
});
/**
 * Returns the currently active tab, or undefined if no tab is active or tabs are not present.
 *
 * @alpha
 */
export const selectActiveTab = createSelector(selectTabs, selectActiveTabLocalIdentifier, (tabs, activeTabLocalIdentifier) => {
    if (!tabs || !activeTabLocalIdentifier) {
        return undefined;
    }
    return tabs.find((tab) => tab.localIdentifier === activeTabLocalIdentifier);
});
/**
 * Returns a specific tab by its identifier.
 *
 * @alpha
 */
export const selectTabById = (tabId) => createSelector(selectTabs, (tabs) => {
    if (!tabs) {
        return undefined;
    }
    return tabs.find((tab) => tab.localIdentifier === tabId);
});
/**
 * Returns whether tabs are present in the dashboard.
 *
 * @alpha
 */
export const selectHasTabs = createSelector(selectTabs, (tabs) => {
    return tabs !== undefined && tabs.length > 0;
});
/**
 * Returns the local identifier of the first tab.
 *
 * @alpha
 */
export const selectFirstTabLocalIdentifier = createSelector(selectTabs, (tabs) => tabs?.[0]?.localIdentifier);
