// (C) 2019-2026 GoodData Corporation
import { useCallback } from "react";
import { saveDashboardAs } from "../../../model/commands/dashboard.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useDashboardCommandProcessing, } from "../../../model/react/useDashboardCommandProcessing.js";
import { selectBackendCapabilities } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectLocale } from "../../../model/store/config/configSelectors.js";
import { selectDashboardTitle } from "../../../model/store/meta/metaSelectors.js";
import { selectIsInEditMode } from "../../../model/store/renderMode/renderModeSelectors.js";
import { selectIsDashboardSaving } from "../../../model/store/saving/savingSelectors.js";
export const useSaveAs = (props) => {
    const { onSubmit, onSubmitSuccess, onSubmitError } = props;
    const locale = useDashboardSelector(selectLocale);
    const dashboardTitle = useDashboardSelector(selectDashboardTitle);
    const capabilities = useDashboardSelector(selectBackendCapabilities);
    const isDashboardSaving = useDashboardSelector(selectIsDashboardSaving);
    const isInEditMode = useDashboardSelector(selectIsInEditMode);
    const saveAsCommandProcessing = useDashboardCommandProcessing({
        commandCreator: saveDashboardAs,
        errorEvent: "GDC.DASH/EVT.COMMAND.FAILED",
        successEvent: "GDC.DASH/EVT.COPY_SAVED",
        onError: (event) => {
            onSubmitError?.(event.payload.error);
        },
        onSuccess: (event) => {
            onSubmitSuccess?.(event.payload.dashboard);
        },
        onBeforeRun: (cmd) => {
            onSubmit?.(cmd.payload.title, cmd.payload.switchToCopy);
        },
    });
    const handleSaveAs = useCallback((title, switchToDashboard = false, useOriginalFilterContext = true) => {
        saveAsCommandProcessing.run(title, switchToDashboard, useOriginalFilterContext);
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []);
    return {
        locale,
        dashboardTitle,
        isKpiWidgetEnabled: capabilities.supportsKpiWidget ?? false,
        isDashboardLoaded: true,
        isDashboardSaving,
        isInEditMode,
        handleSaveAs,
        saveAsStatus: saveAsCommandProcessing.status,
    };
};
