// (C) 2024-2025 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { isExportDefinitionVisualizationObjectRequestPayload, } from "@gooddata/sdk-model";
import { createMemoizedSelector } from "../_infra/selectors.js";
const selectSelf = createSelector((state) => state, (state) => state.automations);
/**
 * Returns workspace automations.
 *
 * @alpha
 */
export const selectDashboardUserAutomations = createSelector(selectSelf, (state) => {
    return state.userAutomations;
});
/**
 * Returns workspace automations count.
 *
 * @alpha
 */
export const selectAllAutomationsCount = createSelector(selectSelf, (state) => {
    return state.allAutomationsCount;
});
/**
 * Returns workspace alerts.
 *
 * @alpha
 */
export const selectDashboardUserAutomationAlerts = createSelector(selectSelf, (state) => {
    return state.userAutomations.filter((automation) => !!automation.alert);
});
/**
 * Returns workspace schedules.
 *
 * @alpha
 */
export const selectDashboardUserAutomationSchedules = createSelector(selectSelf, (state) => {
    return state.userAutomations.filter((automation) => !!automation.schedule && !automation.alert);
});
/**
 * Returns workspace alerts for current dashboard, widget and user context.
 *
 * @alpha
 */
export const selectDashboardUserAutomationAlertsInContext = createMemoizedSelector((widgetLocalIdentifier) => createSelector(selectDashboardUserAutomationAlerts, (alerts) => {
    return alerts.filter((alert) => alert.metadata?.widget === widgetLocalIdentifier || !widgetLocalIdentifier);
}));
/**
 * Returns workspace schedules for current dashboard, widget and user context.
 *
 * @alpha
 */
export const selectDashboardUserAutomationSchedulesInContext = createMemoizedSelector((widgetLocalIdentifier) => createSelector(selectDashboardUserAutomationSchedules, (schedules) => {
    return schedules.filter((schedule) => {
        const isTiedToWidget = schedule.exportDefinitions?.some((exportDefinition) => {
            const requestPayload = exportDefinition.requestPayload;
            return (isExportDefinitionVisualizationObjectRequestPayload(requestPayload) &&
                requestPayload.content.widget === widgetLocalIdentifier);
        });
        return isTiedToWidget || !widgetLocalIdentifier;
    });
}));
/**
 * Returns workspace automations loading
 *
 * @alpha
 */
export const selectAutomationsIsLoading = createSelector(selectSelf, (state) => {
    return state.isLoading;
});
/**
 * Returns workspace automations loading
 *
 * @alpha
 */
export const selectAutomationsIsInitialized = createSelector(selectSelf, (state) => {
    return state.isInitialized;
});
/**
 * Returns organization automations error
 *
 * @alpha
 */
export const selectAutomationsError = createSelector(selectSelf, (state) => {
    return state.error;
});
//# sourceMappingURL=automationsSelectors.js.map