// (C) 2024-2026 GoodData Corporation
import { isEqual, omit, pick } from "lodash-es";
import { isExportDefinitionDashboardRequestPayload, isExportDefinitionVisualizationObjectRequestPayload, isFilter, isFilterContextItem, } from "@gooddata/sdk-model";
export const isDashboardAutomation = (automation) => {
    if (!automation) {
        return false;
    }
    return (automation.exportDefinitions?.some((exportDefinition) => {
        return isExportDefinitionDashboardRequestPayload(exportDefinition.requestPayload);
    }) ?? false);
};
export const isVisualisationAutomation = (automation) => {
    if (!automation) {
        return false;
    }
    return (automation.exportDefinitions?.some((exportDefinition) => {
        return isExportDefinitionVisualizationObjectRequestPayload(exportDefinition.requestPayload);
    }) ?? false);
};
export const isCsvVisualizationAutomation = (automation) => {
    if (!automation) {
        return false;
    }
    return automation.exportDefinitions?.some(isCsvVisualizationExportDefinition) ?? false;
};
export const isCsvVisualizationExportDefinition = (exportDefinition) => {
    if (!exportDefinition) {
        return false;
    }
    return (isExportDefinitionVisualizationObjectRequestPayload(exportDefinition.requestPayload) &&
        exportDefinition.requestPayload.format === "CSV");
};
export const isXlsxVisualizationAutomation = (automation) => {
    if (!automation) {
        return false;
    }
    return automation.exportDefinitions?.some(isXlsxVisualizationExportDefinition) ?? false;
};
export const isXlsxVisualizationExportDefinition = (exportDefinition) => {
    if (!exportDefinition) {
        return false;
    }
    return (isExportDefinitionVisualizationObjectRequestPayload(exportDefinition.requestPayload) &&
        exportDefinition.requestPayload.format === "XLSX");
};
export const getAutomationDashboardFilters = (automation) => {
    if (!automation) {
        return undefined;
    }
    return automation.exportDefinitions?.find((exportDefinition) => {
        return isExportDefinitionDashboardRequestPayload(exportDefinition.requestPayload);
    })?.requestPayload?.content.filters?.filter((f) => isFilterContextItem(f));
};
/**
 * Extracts dashboard filters structured by tab from automation metadata object.
 * Returns filtersByTab from the first dashboard export definition.
 */
export const getAutomationDashboardFiltersByTab = (automation) => {
    if (!automation) {
        return undefined;
    }
    return automation.exportDefinitions?.find((exportDefinition) => {
        return isExportDefinitionDashboardRequestPayload(exportDefinition.requestPayload);
    })?.requestPayload?.content.filtersByTab;
};
export const getAutomationVisualizationFilters = (automation) => {
    if (!automation) {
        return { executionFilters: undefined, filterContextItems: undefined };
    }
    const executionFilters = automation.exportDefinitions?.find((exportDefinition) => {
        return isExportDefinitionVisualizationObjectRequestPayload(exportDefinition.requestPayload);
    })?.requestPayload?.content.filters?.filter((f) => isFilter(f));
    const filterContextItems = automation.exportDefinitions?.find((exportDefinition) => {
        return isExportDefinitionVisualizationObjectRequestPayload(exportDefinition.requestPayload);
    })?.requestPayload?.content.filters?.filter((f) => isFilterContextItem(f));
    return {
        executionFilters: (executionFilters ?? []).length > 0 ? executionFilters : undefined,
        filterContextItems: (filterContextItems ?? []).length > 0 ? filterContextItems : undefined,
    };
};
export const getAutomationAlertFilters = (automation) => {
    if (!automation) {
        return undefined;
    }
    return automation.alert?.execution?.filters?.filter(isFilter);
};
const sortByFormat = (a, b) => a.requestPayload.format.localeCompare(b.requestPayload.format);
export const areAutomationsEqual = (originalAutomation, updatedAutomation) => {
    const automationWithoutExportDefinitions = omit(updatedAutomation, "exportDefinitions");
    const origAutomationWithoutExportDefinitions = omit(originalAutomation, "exportDefinitions");
    // We only want to compare requestPayload and title of exportDefinitions, rest may be omitted as it is just arbitrary
    // metadata that is not relevant for the comparison and causes false positive results when comparing new and old def.
    // Sorting is done just to avoid false positive result of different order of export definitions.
    const automationExportDefinitions = updatedAutomation.exportDefinitions
        ?.map((exportDefinition) => pick(exportDefinition, ["requestPayload", "title"]))
        .sort(sortByFormat);
    const origAutomationExportDefinitions = originalAutomation.exportDefinitions
        ?.map((exportDefinition) => pick(exportDefinition, ["requestPayload", "title"]))
        .sort(sortByFormat);
    return (isEqual(automationWithoutExportDefinitions, origAutomationWithoutExportDefinitions) &&
        isEqual(automationExportDefinitions, origAutomationExportDefinitions));
};
export const convertCurrentUserToAutomationRecipient = (users, user) => {
    const foundUser = users.find((u) => u.login === user.login);
    return convertUserToAutomationRecipient(foundUser ?? user);
};
export const convertCurrentUserToWorkspaceUser = (users, user) => {
    const foundUser = users.find((u) => u.login === user.login);
    return (foundUser ?? {
        email: user.email ?? "",
        fullName: user.fullName,
        status: "ENABLED",
        login: user.login,
        lastName: user.lastName,
        firstName: user.firstName,
        uri: user.login,
        ref: user.ref,
    });
};
export const convertUserToAutomationRecipient = (user) => {
    return {
        id: user.login,
        email: user.email,
        name: user.fullName,
        type: "user",
    };
};
export const convertExternalRecipientToAutomationRecipient = (externalRecipient) => {
    return {
        id: externalRecipient,
        email: externalRecipient,
        name: externalRecipient,
        type: "externalUser",
    };
};
