import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { render } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { newRelativeDashboardDateFilter } from "@gooddata/sdk-model";
import { AutomationsContextProvider, } from "../../../contexts/AutomationsContext.js";
const captured = vi.hoisted(() => ({
    lastConfig: undefined,
}));
vi.mock("react-intl", () => ({
    useIntl: () => ({
        formatMessage: ({ id }) => id,
    }),
}));
vi.mock("../../../../../model/react/DashboardStoreProvider.js", () => ({
    useDashboardSelector: vi.fn(() => {
        throw new Error("AutomationDateFilter must not read dashboard selectors directly");
    }),
}));
vi.mock("../../../../filterBar/dateFilter/DefaultDashboardDateFilter.js", () => ({
    DefaultDashboardDateFilter: vi.fn((props) => {
        captured.lastConfig = props.config;
        return null;
    }),
}));
import { AutomationDateFilter } from "./AutomationDateFilter.js";
function createAutomationsContextValue(overrides) {
    return {
        locale: "en-US",
        separators: { decimal: ".", thousand: "," },
        settings: undefined,
        catalogAttributes: [],
        catalogDateDatasets: overrides.catalogDateDatasets,
        catalogMeasures: [],
        dateFilterConfig: overrides.dateFilterConfig,
        dateFilterContextConfig: undefined,
        attributeFilterConfigs: [],
        attributeFilterConfigsByTab: {},
        attributeFilterSelectionTypeMap: new Map(),
        attributeFilterSelectionTypeMapByTab: {},
        dateFilterConfigs: [],
        dateFilterConfigsByTab: {},
        dateFilterConfigOverridesByTab: {},
        measureValueFilterConfigs: [],
        measureValueFilterConfigsByTab: {},
        commonDateFilterId: undefined,
        lockedFilters: [],
        hiddenFilters: [],
        availableFilters: [],
        automationFiltersByTab: [],
        defaultSelectedFilters: [],
        automationAvailableFilters: [],
        enableNewScheduledExport: false,
        maxAutomationsRecipients: 10,
        isExecutionTimestampMode: false,
        allowHourlyRecurrence: true,
        currentUser: { login: "test@example.com", ref: { identifier: "test" } },
        weekStart: "Sunday",
        timezone: undefined,
        isWhiteLabeled: false,
        isSecondaryTitleVisible: false,
        externalRecipient: undefined,
        features: {
            canCreateAutomation: false,
            enableAlertOncePerInterval: false,
            enableAnomalyDetectionAlert: false,
            enableAutomationManagement: false,
            canUseAiAssistant: false,
            enableComparisonInAlerting: false,
            enableExternalRecipients: false,
            enableAlertAttributes: false,
            canManageWorkspace: false,
        },
        getCatalogAttributeByRef: () => undefined,
        getAttributeFilterDisplayForm: () => undefined,
    };
}
describe("AutomationDateFilter", () => {
    const activeGranularities = ["GDC.time.date"];
    const tabGranularities = ["GDC.time.month"];
    const activeOptions = {
        allTime: {
            localIdentifier: "allTime",
            type: "allTime",
            name: "All time",
            visible: true,
        },
    };
    const tabOptions = {
        relativeForm: {
            fromLimit: -30,
            toLimit: 0,
            localIdentifier: "relative",
            type: "relativeForm",
            name: "Relative",
            visible: true,
        },
    };
    const filter = newRelativeDashboardDateFilter("GDC.time.date", -6, 0);
    it("uses tab-specific values from AutomationsContext without reading dashboard selectors", () => {
        captured.lastConfig = undefined;
        const value = createAutomationsContextValue({
            catalogDateDatasets: [],
            dateFilterConfig: {
                availableGranularities: activeGranularities,
                dateFilterOptions: activeOptions,
                getGranularitiesForTab: (tabId) => (tabId === "tab1" ? tabGranularities : []),
                getOptionsForTab: (tabId) => (tabId === "tab1" ? tabOptions : undefined),
            },
        });
        render(_jsx(AutomationsContextProvider, { value: value, children: _jsx(AutomationDateFilter, { filter: filter, onChange: vi.fn(), onDelete: vi.fn(), tabId: "tab1" }) }));
        expect(captured.lastConfig).toMatchObject({
            availableGranularities: tabGranularities,
            dateFilterOptions: tabOptions,
        });
    });
    it("falls back to active values when the tab-specific config is empty", () => {
        captured.lastConfig = undefined;
        const value = createAutomationsContextValue({
            catalogDateDatasets: [],
            dateFilterConfig: {
                availableGranularities: activeGranularities,
                dateFilterOptions: activeOptions,
                getGranularitiesForTab: () => [],
                getOptionsForTab: () => undefined,
            },
        });
        render(_jsx(AutomationsContextProvider, { value: value, children: _jsx(AutomationDateFilter, { filter: filter, onChange: vi.fn(), onDelete: vi.fn(), tabId: "missing-tab" }) }));
        expect(captured.lastConfig).toMatchObject({
            availableGranularities: activeGranularities,
            dateFilterOptions: activeOptions,
        });
    });
    it("falls back to active options when tab-specific options map is empty", () => {
        captured.lastConfig = undefined;
        const value = createAutomationsContextValue({
            catalogDateDatasets: [],
            dateFilterConfig: {
                availableGranularities: activeGranularities,
                dateFilterOptions: activeOptions,
                getGranularitiesForTab: () => tabGranularities,
                getOptionsForTab: () => ({}),
            },
        });
        render(_jsx(AutomationsContextProvider, { value: value, children: _jsx(AutomationDateFilter, { filter: filter, onChange: vi.fn(), onDelete: vi.fn(), tabId: "tab1" }) }));
        expect(captured.lastConfig).toMatchObject({
            availableGranularities: tabGranularities,
            dateFilterOptions: activeOptions,
        });
    });
});
