// (C) 2023-2026 GoodData Corporation
import { createSelector } from "@reduxjs/toolkit";
import { invariant } from "ts-invariant";
const selectSelf = createSelector((state) => state, (state) => state.entitlements);
/**
 * Returns dashboard's entitlements.
 *
 * @remarks
 * It is expected that the selector is called only after the entitlements state
 * is correctly initialized. Invocations before initialization lead to invariant errors.
 *
 * @alpha
 */
export const selectEntitlements = createSelector(selectSelf, (state) => {
    invariant(state.entitlements, "attempting to access uninitialized entitlements state");
    return state.entitlements;
});
/**
 * @alpha
 */
export const selectEntitlementMaxAutomationRecipients = createSelector(selectEntitlements, (entitlements) => {
    return entitlements.find((entitlement) => entitlement.name === "AutomationRecipientCount");
});
/**
 * @alpha
 */
export const selectEntitlementUnlimitedAutomationRecipients = createSelector(selectEntitlements, (entitlements) => {
    return entitlements.find((entitlement) => entitlement.name === "UnlimitedAutomationRecipients");
});
/**
 * @alpha
 */
export const selectMaxAutomationRecipients = createSelector(selectEntitlementMaxAutomationRecipients, selectEntitlementUnlimitedAutomationRecipients, (maxRecipientsEntitlement, unlimitedEntitlement) => {
    if (unlimitedEntitlement) {
        return Infinity;
    }
    const DEFAULT_MAX_RECIPIENTS = 10;
    return maxRecipientsEntitlement?.value
        ? Number(maxRecipientsEntitlement.value)
        : DEFAULT_MAX_RECIPIENTS;
});
/**
 * @alpha
 */
export const selectEntitlementMaxAutomations = createSelector(selectEntitlements, (entitlements) => {
    return entitlements.find((entitlement) => entitlement.name === "AutomationCount");
});
/**
 * @alpha
 */
export const selectEntitlementUnlimitedAutomations = createSelector(selectEntitlements, (entitlements) => {
    return entitlements.find((entitlement) => entitlement.name === "UnlimitedAutomations");
});
/**
 * @alpha
 */
export const selectEntitlementMinimumRecurrenceMinutes = createSelector(selectEntitlements, (entitlements) => {
    return entitlements.find((entitlement) => entitlement.name === "ScheduledActionMinimumRecurrenceMinutes");
});
