// (C) 2025-2026 GoodData Corporation
import { CronExpressionParser } from "cron-parser";
import { ErrorCodes, buildAutomationUrl, convertError, navigate } from "@gooddata/sdk-ui";
import { ARITHMETIC_OPERATORS, COMPARISON_OPERATORS, RELATIVE_OPERATORS } from "./constants.js";
import { messages } from "./messages.js";
export const getWorkspaceId = (automation, fallbackWorkspaceId) => {
    return automation.workspace?.id ?? fallbackWorkspaceId;
};
export const defaultEditAutomation = (automation, workspaceId, dashboardId) => {
    const targetWorkspaceId = getWorkspaceId(automation, workspaceId);
    navigate(buildAutomationUrl({
        workspaceId: targetWorkspaceId,
        dashboardId,
        automationId: automation.id,
    }));
};
const trimString = (value) => {
    const trimmed = value?.trim();
    return trimmed || undefined;
};
export const getRecipientName = (recipient) => trimString(recipient.name) ||
    ("email" in recipient ? trimString(recipient.email) : undefined) ||
    trimString(recipient.id) ||
    "";
export const getNextRunFromCron = (cron) => {
    if (!cron) {
        return undefined;
    }
    return CronExpressionParser.parse(cron).next().toDate().toISOString();
};
export const getFirstAutomationExportDefinition = (automation) => {
    if (automation.exportDefinitions?.length) {
        return automation.exportDefinitions[0];
    }
    return undefined;
};
export const getWidgetId = (automation, type) => {
    if (type === "schedule") {
        const exportDefinition = getFirstAutomationExportDefinition(automation);
        if (exportDefinition?.requestPayload?.type === "visualizationObject") {
            return exportDefinition?.requestPayload?.content?.widget ?? "";
        }
        return "";
    }
    return automation?.metadata?.widget ?? "";
};
export const getWidgetName = (automation, type) => {
    if (type === "schedule") {
        const exportDefinition = getFirstAutomationExportDefinition(automation);
        if (exportDefinition?.requestPayload?.type === "visualizationObject") {
            return exportDefinition?.title ?? "";
        }
        return "";
    }
    return automation?.details?.widgetName ?? "";
};
export const isRequestHeaderTooLargeError = (error) => {
    const sdkError = convertError(error);
    if (sdkError.getMessage() === ErrorCodes.HEADERS_TOO_LARGE) {
        return true;
    }
    return error instanceof Error && error.message === ErrorCodes.HEADERS_TOO_LARGE;
};
/**
 * @internal
 */
export const getComparisonOperatorTitle = (operator, intl) => {
    const titleByOperator = {
        [COMPARISON_OPERATORS.COMPARISON_OPERATOR_LESS_THAN]: messages.comparisonOperatorLessThan.id,
        [COMPARISON_OPERATORS.COMPARISON_OPERATOR_LESS_THAN_OR_EQUAL_TO]: messages.comparisonOperatorLessThanOrEquals.id,
        [COMPARISON_OPERATORS.COMPARISON_OPERATOR_GREATER_THAN]: messages.comparisonOperatorGreaterThan.id,
        [COMPARISON_OPERATORS.COMPARISON_OPERATOR_GREATER_THAN_OR_EQUAL_TO]: messages.comparisonOperatorGreaterThanOrEquals.id,
    };
    return intl.formatMessage({ id: titleByOperator[operator] });
};
export function getAnomalyDetectionOperatorTitle(title, _sensitivity, _granularity, intl) {
    return [title, intl.formatMessage({ id: messages.anomalyDetectionOperator.id })].join(" ");
}
/**
 * @internal
 */
export const getRelativeOperatorTitle = (operator, art, intl) => {
    const titleByOperator = {
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_CHANGE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_INCREASE_BY}`]: messages.comparisonOperatorChangeIncreasesBy.id,
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_CHANGE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_DECREASE_BY}`]: messages.comparisonOperatorChangeDecreasesBy.id,
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_CHANGE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_CHANGES_BY}`]: messages.comparisonOperatorChangeChangesBy.id,
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_DIFFERENCE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_INCREASE_BY}`]: messages.comparisonOperatorDifferenceIncreasesBy.id,
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_DIFFERENCE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_DECREASE_BY}`]: messages.comparisonOperatorDifferenceDecreasesBy.id,
        [`${ARITHMETIC_OPERATORS.ARITHMETIC_OPERATOR_DIFFERENCE}.${RELATIVE_OPERATORS.RELATIVE_OPERATOR_CHANGES_BY}`]: messages.comparisonOperatorDifferenceChangesBy.id,
    };
    return intl.formatMessage({ id: titleByOperator[`${art}.${operator}`] });
};
//# sourceMappingURL=utils.js.map