// (C) 2025-2026 GoodData Corporation
import { useMemo, useState } from "react";
import { isExportDefinitionDashboardRequestPayload, } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../model/react/DashboardStoreProvider.js";
import { selectAutomationCommonDateFilterId, selectAutomationFiltersByTab, selectDashboardFiltersWithoutCrossFiltering, } from "../../model/store/filtering/dashboardFilterSelectors.js";
import { removeIgnoredWidgetFilters } from "../../model/utils/widgetFilters.js";
import { useAutomationVisibleFilters, useAutomationVisibleFiltersByTab, } from "./hooks/useAutomationVisibleFilters.js";
import { getDefaultSelectedFiltersByTabForExistingAutomation, useDefaultSelectedFiltersForExistingAutomation, } from "./hooks/useDefaultSelectedFiltersForExistingAutomation.js";
import { useDefaultSelectedFiltersForNewAutomation } from "./hooks/useDefaultSelectedFiltersForNewAutomation.js";
/**
 * Checks if automation has all-time date filters stored in flat structure.
 */
function hasAllTimeDateFiltersInFlatStructure(automationToEdit) {
    return automationToEdit?.metadata?.visibleFilters?.some((f) => f.isAllTimeDateFilter) ?? false;
}
/**
 * Checks if automation has filters stored in export definitions (flat structure).
 */
function hasFiltersInExportDefinitions(automationToEdit) {
    return (automationToEdit?.exportDefinitions?.some((exportDefinition) => {
        return !!exportDefinition.requestPayload.content.filters;
    }) ?? false);
}
/**
 * Checks if automation has all-time date filters stored per tab.
 */
function hasAllTimeDateFiltersPerTab(automationToEdit) {
    if (!automationToEdit?.metadata?.visibleFiltersByTab) {
        return false;
    }
    return Object.values(automationToEdit.metadata.visibleFiltersByTab).some((tabFilters) => tabFilters.some((f) => f.isAllTimeDateFilter));
}
/**
 * Checks if automation has filters stored per tab in export definitions.
 */
function hasFiltersByTabInExportDefinitions(automationToEdit) {
    return (automationToEdit?.exportDefinitions?.some((exportDefinition) => {
        return (isExportDefinitionDashboardRequestPayload(exportDefinition.requestPayload) &&
            !!exportDefinition.requestPayload.content.filtersByTab);
    }) ?? false);
}
/**
 * Determines if the automation has any stored filters (either flat or per-tab structure).
 */
function checkAreFiltersStored(automationToEdit) {
    // Check old flat filters structure
    const hasSavedSomeAllTimeDateFilters = hasAllTimeDateFiltersInFlatStructure(automationToEdit);
    const hasSavedFiltersInExportDefinitions = hasFiltersInExportDefinitions(automationToEdit);
    // Check new per-tab filters structure
    const hasSavedSomeAllTimeDateFiltersPerTab = hasAllTimeDateFiltersPerTab(automationToEdit);
    const hasSavedFiltersByTabInExportDefinitions = hasFiltersByTabInExportDefinitions(automationToEdit);
    // Filters are stored if either old OR new structure has filters
    return (hasSavedFiltersInExportDefinitions ||
        hasSavedSomeAllTimeDateFilters ||
        hasSavedFiltersByTabInExportDefinitions ||
        hasSavedSomeAllTimeDateFiltersPerTab);
}
/**
 * Hook to get data for AutomationFiltersSelect component.
 */
export const useAutomationFiltersSelect = ({ widget, automationToEdit, }) => {
    const availableFilters = useDashboardSelector(selectDashboardFiltersWithoutCrossFiltering);
    const availableFiltersWithoutIgnoredWidgetFilters = removeIgnoredWidgetFilters(availableFilters, widget);
    const availableFiltersAsVisibleFilters = useAutomationVisibleFilters(availableFiltersWithoutIgnoredWidgetFilters);
    const filtersForNewAutomation = useDefaultSelectedFiltersForNewAutomation(widget);
    const filtersForExistingAutomation = useDefaultSelectedFiltersForExistingAutomation(automationToEdit, availableFiltersAsVisibleFilters, widget);
    // Get filters per tab - only applicable for whole dashboard automations (no widget)
    const allFiltersByTab = useDashboardSelector(selectAutomationFiltersByTab);
    const availableFiltersByTab = allFiltersByTab.reduce((acc, tab) => {
        acc[tab.tabId] = tab.availableFilters;
        return acc;
    }, {});
    const availableFiltersAsVisibleFiltersByTab = useAutomationVisibleFiltersByTab(availableFiltersByTab);
    // Safe to get from active tab selector as it's a consistent identifier
    const commonDateFilterId = useDashboardSelector(selectAutomationCommonDateFilterId);
    // Only provide filtersPerTab for whole dashboard automations when there are multiple tabs
    const isDashboardAutomation = !widget;
    const hasMultipleTabs = allFiltersByTab.length > 1;
    const filtersByTab = isDashboardAutomation && hasMultipleTabs ? allFiltersByTab : undefined;
    const areFiltersStored = useMemo(() => checkAreFiltersStored(automationToEdit), [automationToEdit]);
    // Store filters or not? (checkbox to use latest saved dashboard filters vs "freeze" filters state as is)
    const [storeFilters, setStoreFilters] = useState(areFiltersStored);
    const isDashboardAutomationWithoutStoredFilters = useMemo(() => {
        return !widget && !storeFilters;
    }, [widget, storeFilters]);
    // State of current automation filters to display (and potentially save).
    const [editedAutomationFilters, setEditedAutomationFilters] = useState(automationToEdit && !isDashboardAutomationWithoutStoredFilters
        ? filtersForExistingAutomation
        : filtersForNewAutomation);
    const filtersByTabForNewAutomation = getDefaultSelectedFiltersFromFiltersByTab(filtersByTab);
    const filtersByTabForExistingAutomation = useMemo(() => {
        if (!filtersByTab || !automationToEdit || isDashboardAutomationWithoutStoredFilters) {
            return undefined;
        }
        const filtersByTabForExistingAutomation = getDefaultSelectedFiltersByTabForExistingAutomation(automationToEdit, availableFiltersByTab, commonDateFilterId);
        if (filtersByTabForExistingAutomation) {
            return filtersByTabForExistingAutomation;
        }
        return undefined;
    }, [
        filtersByTab,
        automationToEdit,
        isDashboardAutomationWithoutStoredFilters,
        commonDateFilterId,
        availableFiltersByTab,
    ]);
    // State for edited filters per tab (only used when dashboard tabs are enabled)
    const [editedAutomationFiltersByTab, setEditedAutomationFiltersByTab] = useState(automationToEdit && !isDashboardAutomationWithoutStoredFilters
        ? filtersByTabForExistingAutomation
        : filtersByTabForNewAutomation);
    return {
        storeFilters,
        setStoreFilters,
        //flat filters
        editedAutomationFilters,
        setEditedAutomationFilters,
        availableFilters: availableFiltersWithoutIgnoredWidgetFilters,
        availableFiltersAsVisibleFilters: availableFiltersAsVisibleFilters,
        filtersForNewAutomation,
        // filters by tab data
        editedAutomationFiltersByTab,
        setEditedAutomationFiltersByTab,
        filtersByTab, // includes all filters related parameters
        availableFiltersAsVisibleFiltersByTab,
    };
};
export function getDefaultSelectedFiltersFromFiltersByTab(filtersByTab) {
    if (!filtersByTab) {
        return undefined;
    }
    return filtersByTab.reduce((acc, tab) => {
        acc[tab.tabId] = tab.defaultSelectedFilters;
        return acc;
    }, {});
}
export function getAvailableFiltersFromFiltersByTab(filtersByTab) {
    if (!filtersByTab) {
        return {};
    }
    return filtersByTab.reduce((acc, tab) => {
        acc[tab.tabId] = tab.availableFilters;
        return acc;
    }, {});
}
//# sourceMappingURL=useAutomationFiltersSelect.js.map