// (C) 2025-2026 GoodData Corporation
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useWorkspace } from "@gooddata/sdk-ui";
import { useAutomationActions } from "./actions/useAutomationActions.js";
import { useAutomationBulkActions } from "./actions/useAutomationBulkActions.js";
import { useAutomationColumns } from "./columns/useAutomationColumns.js";
import { AutomationsDefaultState } from "./constants.js";
import { useLoadAutomations } from "./data/useLoadAutomations.js";
import { useAutomationFilters } from "./filters/useAutomationFilters.js";
import { useAutomationsSmallLayout } from "./useAutomationsSmallLayout.js";
export const useAutomationsState = ({ type, scope, timezone, selectedColumnDefinitions, pageSize, availableFilters, preselectedFilters, externalInvalidationId, tableVariant, isMobileView, enableBulkActions, dashboardUrlBuilder, widgetUrlBuilder, editAutomation, onLoad, }) => {
    const [state, setState] = useState(AutomationsDefaultState);
    const workspace = useWorkspace();
    //prevent useEffect runs on mount
    const actionsRefFirstRun = useRef(true);
    const filtersRefFirstRun = useRef(true);
    const previousSkeletonItemsCountRef = useRef(0);
    const setPendingAction = useCallback((pendingAction) => {
        setState((state) => ({
            ...state,
            pendingAction,
        }));
    }, []);
    const selectedAutomations = useMemo(() => state.automations.filter((a) => state.selectedIds.has(a.id)), [state.automations, state.selectedIds]);
    const { isLoading: isActionLoading, deleteAutomation, bulkDeleteAutomations, unsubscribeFromAutomation, bulkUnsubscribeFromAutomations, pauseAutomation, resumeAutomation, bulkPauseAutomations, bulkResumeAutomations, } = useAutomationActions(type, scope);
    const availableBulkActions = useAutomationBulkActions({
        selected: selectedAutomations,
        automationsType: type,
        enabled: enableBulkActions,
        bulkDeleteAutomations,
        bulkUnsubscribeFromAutomations,
        bulkPauseAutomations,
        bulkResumeAutomations,
        setPendingAction,
    });
    const { columnDefinitions, includeAutomationResult, containerRef } = useAutomationColumns({
        type,
        timezone,
        selectedColumnDefinitions,
        automationsType: type,
        tableVariant,
        isMobileView,
        enableBulkActions,
        deleteAutomation,
        unsubscribeFromAutomation,
        pauseAutomation,
        resumeAutomation,
        editAutomation,
        dashboardUrlBuilder,
        widgetUrlBuilder,
        setPendingAction,
    });
    const { filters, dashboardFilterQuery, recipientsFilterQuery, externalRecipientsFilterQuery, createdByFilterQuery, statusFilterQuery, workspacesFilterQuery, } = useAutomationFilters(preselectedFilters, availableFilters);
    const { status: dataLoadingStatus, error } = useLoadAutomations({
        type,
        pageSize,
        state,
        dashboardFilterQuery,
        recipientsFilterQuery,
        externalRecipientsFilterQuery,
        workspacesFilterQuery,
        statusFilterQuery,
        createdByFilterQuery,
        includeAutomationResult,
        scope,
        onLoad,
        setState,
    });
    const isLoading = useMemo(() => {
        return dataLoadingStatus === "loading" || isActionLoading || state.isChainedActionInProgress;
    }, [dataLoadingStatus, isActionLoading, state.isChainedActionInProgress]);
    const skeletonItemsCount = useMemo(() => {
        let newCount = 0;
        if (state.hasNextPage) {
            newCount = Math.min(pageSize, state.totalItemsCount - state.automations.length) || pageSize;
        }
        // Prevent flickering of skeleton items count when processing sequential requests
        // since we already have a skeleton items count from the previous request and just need to refetch the data
        if (isLoading && previousSkeletonItemsCountRef.current > 0) {
            return previousSkeletonItemsCountRef.current;
        }
        else {
            previousSkeletonItemsCountRef.current = newCount;
            return newCount;
        }
    }, [state.hasNextPage, state.totalItemsCount, state.automations.length, pageSize, isLoading]);
    const resetState = useCallback(() => {
        setState((state) => ({
            ...state,
            automations: [],
            totalItemsCount: 0,
            previousAutomations: state.automations,
            previousTotalItemsCount: state.totalItemsCount,
            hasNextPage: true,
            selectedIds: new Set(),
            scrollToIndex: 0,
            isFiltersTooLarge: false,
            isChainedActionInProgress: false,
            page: 0,
            invalidationId: state.invalidationId + 1,
        }));
    }, []);
    // React to external invalidation ID changes (skip if 0 or undefined)
    useEffect(() => {
        if (externalInvalidationId) {
            resetState();
        }
    }, [externalInvalidationId, resetState]);
    useEffect(() => {
        if (filtersRefFirstRun.current) {
            filtersRefFirstRun.current = false;
            return;
        }
        resetState();
    }, [
        state.search,
        state.sortBy,
        state.sortDirection,
        dashboardFilterQuery.value,
        dashboardFilterQuery.type,
        recipientsFilterQuery.value,
        recipientsFilterQuery.type,
        createdByFilterQuery.value,
        createdByFilterQuery.type,
        workspacesFilterQuery.value,
        workspacesFilterQuery.type,
        statusFilterQuery.value,
        statusFilterQuery.type,
        externalRecipientsFilterQuery.value,
        externalRecipientsFilterQuery.type,
        type,
        resetState,
    ]);
    useEffect(() => {
        if (actionsRefFirstRun.current) {
            actionsRefFirstRun.current = false;
            return;
        }
        if (isActionLoading) {
            setState((state) => ({
                ...state,
                automations: [],
                hasNextPage: true,
                scrollToIndex: 0,
                isChainedActionInProgress: true,
            }));
        }
        if (!isActionLoading) {
            resetState();
        }
    }, [isActionLoading, resetState]);
    useEffect(() => {
        if (!isLoading) {
            setState((state) => ({
                ...state,
                scrollToIndex: undefined,
            }));
        }
    }, [isLoading]);
    const handleSort = (key) => {
        if (state.sortBy === key) {
            if (state.sortDirection === "asc") {
                setState({
                    ...state,
                    sortDirection: "desc",
                });
            }
            else {
                setState({
                    ...state,
                    sortDirection: "asc",
                });
            }
        }
        else {
            setState({
                ...state,
                sortBy: key,
                sortDirection: "asc",
            });
        }
    };
    const handleItemClick = (item) => {
        editAutomation(item, workspace, item.dashboard?.id);
    };
    const loadNextPage = useCallback(() => {
        setState((state) => ({
            ...state,
            page: state.page + 1,
        }));
    }, []);
    const searchHandler = useCallback((search) => {
        setState((state) => ({
            ...state,
            search,
        }));
    }, []);
    const setSelectedIds = useCallback((selectedIds) => {
        setState((state) => ({
            ...state,
            selectedIds: new Set(selectedIds),
        }));
    }, []);
    const selectedIds = useMemo(() => {
        return Array.from(state.selectedIds);
    }, [state.selectedIds]);
    const isFiltersOrSearchActive = useMemo(() => {
        return !!(state.search ||
            dashboardFilterQuery?.value ||
            recipientsFilterQuery?.value ||
            createdByFilterQuery?.value ||
            workspacesFilterQuery?.value ||
            statusFilterQuery?.value);
    }, [
        state.search,
        dashboardFilterQuery,
        recipientsFilterQuery,
        createdByFilterQuery,
        workspacesFilterQuery,
        statusFilterQuery,
    ]);
    const { setSearch, bulkActions, columns } = useAutomationsSmallLayout({
        searchHandler,
        search: state.search,
        availableBulkActions,
        columnDefinitions,
        tableVariant,
        automationsLength: state.automations.length,
    });
    return {
        state,
        filters,
        isLoading,
        error,
        skeletonItemsCount,
        columns,
        bulkActions,
        selectedIds,
        isFiltersOrSearchActive,
        containerRef,
        handleSort,
        handleItemClick,
        loadNextPage,
        setSearch,
        setSelectedIds,
        setPendingAction,
    };
};
//# sourceMappingURL=useAutomationsState.js.map