// (C) 2021-2026 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { selectSupportsCrossFiltering } from "../backendCapabilities/backendCapabilitiesSelectors.js";
import { selectEnableAutomationManagement, selectEnableDashboardShareDialogLink, selectEnableKDCrossFiltering, selectEnableKPIDashboardExportPDF, selectEnableSlideshowExports, selectIsExport, selectIsReadOnly, selectIsSaveAsNewButtonHidden, selectIsShareButtonHidden, selectIsWhiteLabeled, } from "../config/configSelectors.js";
import { selectCanEditDashboardPermission, selectCanEditLockedDashboardPermission, selectCanShareDashboardPermission, selectCanShareLockedDashboardPermission, } from "../dashboardPermissions/dashboardPermissionsSelectors.js";
import { selectListedDashboardsMap } from "../listedDashboards/listedDashboardsSelectors.js";
import { selectIsDashboardLoading } from "../loading/loadingSelectors.js";
import { selectDashboardLockStatus, selectDashboardRef, selectDisableFilterViews, selectIsDashboardDirty, selectIsDashboardPrivate, selectIsNewDashboard, } from "../meta/metaSelectors.js";
import { selectCanCreateAnalyticalDashboard, selectCanExportPdf, } from "../permissions/permissionsSelectors.js";
import { selectIsInEditMode, selectIsInViewMode } from "../renderMode/renderModeSelectors.js";
import { selectIsLayoutEmpty } from "../tabs/layout/layoutSelectors.js";
import { selectMenuButtonItemsVisibility } from "../ui/uiSelectors.js";
/**
 * @internal
 */
export const selectCanEnterEditMode = createSelector(selectCanEditDashboardPermission, selectCanEditLockedDashboardPermission, selectDashboardLockStatus, selectIsReadOnly, (canEditDashboardPermission, hasEditLockedDashboardPermission, isLocked, isReadOnly) => !isReadOnly && canEditDashboardPermission && (!isLocked || hasEditLockedDashboardPermission));
/**
 * @internal
 */
export const selectCanEnterEditModeAndIsLoaded = createSelector(selectIsDashboardLoading, selectCanEnterEditMode, (isLoading, canEnterEditMode) => !isLoading && canEnterEditMode);
/**
 * @internal
 */
export const selectIsPrivateDashboard = createSelector(selectIsDashboardPrivate, selectIsNewDashboard, (isPrivate, isCreatingNewDashboard) => isCreatingNewDashboard || isPrivate);
/**
 * @internal
 */
export function selectCanSaveDashboard(state) {
    return selectIsNewDashboard(state) ? !selectIsLayoutEmpty(state) : selectIsDashboardDirty(state);
}
/**
 * @internal
 */
export const selectIsCurrentDashboardVisibleInList = createSelector(selectDashboardRef, selectListedDashboardsMap, (currentDashboardRef, dashboardsList) => Boolean(currentDashboardRef && dashboardsList.has(currentDashboardRef)));
/**
 * @internal
 */
export const selectCrossFilteringEnabledAndSupported = createSelector(selectIsInEditMode, selectEnableKDCrossFiltering, selectSupportsCrossFiltering, (isEdit, enableCrossFiltering, supportsCrossFiltering) => {
    return isEdit && enableCrossFiltering && supportsCrossFiltering;
});
export const selectHasSharePermissions = createSelector(selectCanShareDashboardPermission, selectCanShareLockedDashboardPermission, selectDashboardLockStatus, (canShareDashboardPermission, canShareLockedDashboardPermission, isLocked) => canShareDashboardPermission && (!isLocked || canShareLockedDashboardPermission));
/**
 *
 * @internal
 */
export const selectIsShareGrantVisible = createSelector(selectEnableDashboardShareDialogLink, selectHasSharePermissions, (enableDashboardShareDialogLink, hasSharePermissions) => 
// If dashboardShareDialogLink feature is disabled always show grant options.
// Otherwise decide based on user share permissions
!enableDashboardShareDialogLink || hasSharePermissions);
/**
 *
 * @internal
 */
export const selectIsDashboardShareLinkVisible = createSelector(selectEnableDashboardShareDialogLink, selectIsWhiteLabeled, (enableDashboardShareDialogLink, isWhiteLabeled) => enableDashboardShareDialogLink && !isWhiteLabeled);
/**
 * @internal
 */
export const selectIsShareButtonVisible = createSelector(selectHasSharePermissions, selectIsCurrentDashboardVisibleInList, selectIsReadOnly, selectIsInEditMode, selectIsShareButtonHidden, selectEnableDashboardShareDialogLink, selectIsWhiteLabeled, (hasSharePermissions, isCurrentDashboardVisibleInList, isReadOnly, isInEditMode, isShareButtonHidden, enableDashboardShareDialogLink, isWhiteLabeled) => 
// If dashboardShareDialogLink feature is enabled, show share button regardless of user share permissions
((enableDashboardShareDialogLink && !isWhiteLabeled) || hasSharePermissions) &&
    isCurrentDashboardVisibleInList &&
    !isReadOnly &&
    !isInEditMode &&
    !isShareButtonHidden);
/**
 * @internal
 */
export function selectIsAutomationDialogSecondaryTitleVisible(state) {
    return selectEnableAutomationManagement(state);
}
/**
 * @internal
 */
export const selectIsSaveAsNewButtonVisible = createSelector(selectIsSaveAsNewButtonHidden, selectCanEnterEditModeAndIsLoaded, selectCanCreateAnalyticalDashboard, selectIsExport, selectIsReadOnly, (isSaveAsButtonHidden, isDashboardEditable, canCreateDashboard, isExport, isReadOnly) => {
    /*
     * The reasoning behind this condition is as follows. Do not show separate Save As button if:
     *
     *
     * 1.  The feature is not enabled or
     * 2.  If is disabled by config
     * 3.  If the dashboard can be edited; in this case, the save as option is part of the dropdown menu;
     *     it is somewhat more hidden
     * 4.  dashboard is not in export mode
     * 5.  If the user cannot create dashboards - e.g. does not have permissions to do so (is viewer for example).
     * 6.  If the dashboard is in read-only mode.
     */
    return (!isSaveAsButtonHidden && !isDashboardEditable && !isExport && canCreateDashboard && !isReadOnly);
});
/**
 * Selects whether the common export is available.
 *
 * @internal
 */
export const selectCommonExportAvailable = createSelector(selectIsInViewMode, selectCanExportPdf, (isInViewMode, canExport) => {
    return isInViewMode && canExport;
});
/**
 * Selects whether the slideshow export is available.
 *
 * @internal
 */
export const selectSlideShowExportAvailable = createSelector(selectCommonExportAvailable, selectEnableSlideshowExports, (canExport, isSlideshowExportsEnabled) => {
    return canExport && isSlideshowExportsEnabled;
});
/**
 * Selects whether the slideshow export is visible.
 *
 * @internal
 */
export const selectSlideShowExportVisible = createSelector(selectSlideShowExportAvailable, selectMenuButtonItemsVisibility, (isSlideshowExportsAvailable, menuButtonItemsVisibility) => {
    return (isSlideshowExportsAvailable &&
        (menuButtonItemsVisibility.pdfExportButton ??
            menuButtonItemsVisibility.excelExportButton ??
            menuButtonItemsVisibility.powerPointExportButton ??
            true));
});
/**
 * Selects whether the PDF export is visible.
 *
 * @internal
 */
export const selectPdfExportVisible = createSelector(selectCommonExportAvailable, selectEnableKPIDashboardExportPDF, selectMenuButtonItemsVisibility, (canExport, isKPIDashboardExportPDFEnabled, menuButtonItemsVisibility) => {
    return (canExport &&
        !!isKPIDashboardExportPDFEnabled &&
        (menuButtonItemsVisibility.pdfExportButton ?? true));
});
/**
 * Selects whether the save as new button is visible.
 *
 * @internal
 */
export const selectSaveAsVisible = createSelector(selectIsSaveAsNewButtonVisible, selectCanCreateAnalyticalDashboard, selectIsSaveAsNewButtonHidden, selectMenuButtonItemsVisibility, (isStandaloneSaveAsNewButtonVisible, canCreateDashboard, isSaveAsNewHidden, menuButtonItemsVisibility) => {
    return (!isStandaloneSaveAsNewButtonVisible &&
        canCreateDashboard &&
        !isSaveAsNewHidden &&
        (menuButtonItemsVisibility.saveAsNewButton ?? true));
});
/**
 * Selects whether the settings button is visible.
 *
 * @internal
 */
export const selectSettingsVisible = createSelector(selectIsInEditMode, selectMenuButtonItemsVisibility, (isInEditMode, menuButtonItemsVisibility) => {
    return isInEditMode && (menuButtonItemsVisibility.settingsButton ?? true);
});
/**
 * Selects whether the delete button is visible.
 *
 * @internal
 */
export const selectDeleteVisible = createSelector(selectIsInEditMode, selectMenuButtonItemsVisibility, (isInEditMode, menuButtonItemsVisibility) => {
    return isInEditMode && (menuButtonItemsVisibility.deleteButton ?? true);
});
/**
 * Selects whether the filter views are visible.
 *
 * @internal
 */
export const selectFilterViewsVisible = createSelector(selectDisableFilterViews, (disableFilterViews) => {
    return !disableFilterViews;
});
