// (C) 2022-2026 GoodData Corporation
import { useEffect, useMemo } from "react";
import { UnexpectedSdkError, useExecutionDataView, } from "@gooddata/sdk-ui";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useWidgetFilters } from "../../../model/react/useWidgetFilters.js";
import { selectEnableExecutionCancelling } from "../../../model/store/config/configSelectors.js";
function createPendingResult() {
    return {
        error: undefined,
        result: undefined,
        status: "pending",
    };
}
function createLoadingResult() {
    return {
        error: undefined,
        result: undefined,
        status: "loading",
    };
}
function createErrorResult(error) {
    return {
        error,
        result: undefined,
        status: "error",
    };
}
function createSuccessResult(result) {
    return {
        error: undefined,
        result: result,
        status: "success",
    };
}
function computeDataViewResult(filterQueryTask, dataViewTask, rejectError) {
    const isPending = filterQueryTask.status === "pending" || dataViewTask.status === "pending";
    if (isPending) {
        return createPendingResult();
    }
    const isLoading = filterQueryTask.status === "running" || dataViewTask.status === "loading";
    if (isLoading) {
        return createLoadingResult();
    }
    const hasError = filterQueryTask.status === "error" || dataViewTask.status === "error";
    if (hasError) {
        return createErrorResult((filterQueryTask.error ?? dataViewTask.error));
    }
    if (filterQueryTask.status === "rejected") {
        return createErrorResult(rejectError);
    }
    return createSuccessResult(dataViewTask.result);
}
/**
 * This hook provides an easy way to read a data view from a custom widget. It resolves the appropriate filters
 * for the widget based on the filters currently set on the whole dashboard.
 *
 * @public
 */
export function useCustomWidgetExecutionDataView({ enableExecutionCancelling, widget, execution, onCancel, onError, onLoading, onPending, onSuccess, }) {
    const filterQueryTask = useWidgetFilters(widget);
    const enableExecutionCancellingFF = useDashboardSelector(selectEnableExecutionCancelling);
    const effectiveExecutionCancelling = enableExecutionCancelling ?? enableExecutionCancellingFF;
    const dataViewTask = useExecutionDataView({
        enableExecutionCancelling: effectiveExecutionCancelling,
        execution: execution
            ? {
                ...execution,
                filters: filterQueryTask.result,
            }
            : undefined,
        onCancel,
        onError,
        onLoading,
        onPending,
        onSuccess,
    });
    const rejectError = useMemo(() => {
        if (filterQueryTask.status === "rejected") {
            return new UnexpectedSdkError("The widget filter query was rejected");
        }
        return undefined;
    }, [filterQueryTask.status]);
    useEffect(() => {
        if (filterQueryTask.status === "rejected" && rejectError) {
            onError?.(rejectError);
        }
    }, [filterQueryTask.status, onError, rejectError]);
    useEffect(() => {
        if (filterQueryTask.status === "error") {
            onError?.(filterQueryTask.error);
        }
    }, [filterQueryTask.error, filterQueryTask.status, onError]);
    return computeDataViewResult(filterQueryTask, dataViewTask, rejectError);
}
