import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { Bubble, BubbleHoverTrigger, Button } from "@gooddata/sdk-ui-kit";
import { messages } from "../../../../../locales.js";
import { saveDashboard } from "../../../../../model/commands/dashboard.js";
import { useDashboardDispatch, useDashboardSelector, } from "../../../../../model/react/DashboardStoreProvider.js";
import { dispatchAndWaitFor } from "../../../../../model/store/_infra/dispatchAndWaitFor.js";
import { selectIsDashboardDirty } from "../../../../../model/store/meta/metaSelectors.js";
import { selectIsInEditMode } from "../../../../../model/store/renderMode/renderModeSelectors.js";
import { selectIsDashboardSaving } from "../../../../../model/store/saving/savingSelectors.js";
import { selectLayoutHasAnalyticalWidgets } from "../../../../../model/store/tabs/layout/layoutSelectors.js";
import { selectCanSaveDashboard, selectIsPrivateDashboard, } from "../../../../../model/store/topBar/topBarSelectors.js";
/**
 * @internal
 */
export function useSaveButtonProps() {
    const dispatch = useDashboardDispatch();
    // In some cases, when you click the save button (for example immediately after you insert an insight)
    // and the dashboard is still processing some related requests to this insight, it can take few seconds,
    // before the CMD.SAVE starts to process (as dashboard can process only 1 command at a time).
    // We want to show the loading indicator immediately and avoid any double-save race conditions,
    // so add a local state for this case.
    const [optimisticIsSaving, setOptimisticIsSaving] = useState(false);
    const onSaveClick = useCallback(() => {
        setOptimisticIsSaving(true);
        dispatchAndWaitFor(dispatch, saveDashboard())
            .then(() => {
            setOptimisticIsSaving(false);
        })
            .catch(() => {
            setOptimisticIsSaving(false);
        });
    }, [dispatch]);
    const isEditing = useDashboardSelector(selectIsInEditMode);
    const isSavingDashboard = useDashboardSelector(selectIsDashboardSaving);
    const isPrivateDashboard = useDashboardSelector(selectIsPrivateDashboard);
    const isEmptyDashboard = !useDashboardSelector(selectLayoutHasAnalyticalWidgets); // we need at least one non-custom widget there
    const canSaveDashboard = useDashboardSelector(selectCanSaveDashboard);
    const isDashboardDirty = useDashboardSelector(selectIsDashboardDirty);
    const isSaving = isSavingDashboard || optimisticIsSaving;
    const isVisible = isEditing;
    const isEnabled = isDashboardDirty && !isEmptyDashboard && canSaveDashboard;
    let buttonTitle = messages.controlButtonsSaveAndPublishTitle;
    if (isPrivateDashboard) {
        buttonTitle = messages.controlButtonsSaveAsPrivateTitle;
    }
    if (!canSaveDashboard) {
        buttonTitle = messages.controlButtonsSaveAndPublishNoChanges;
    }
    if (isEmptyDashboard) {
        buttonTitle = messages.controlButtonsSaveAndPublishEmpty;
    }
    return {
        isVisible,
        isEnabled,
        isSaving,
        buttonTitle,
        onSaveClick,
    };
}
/**
 * @internal
 */
export function DefaultSaveButton({ isVisible, isEnabled, isSaving, buttonTitle, onSaveClick, }) {
    const intl = useIntl();
    if (!isVisible) {
        return null;
    }
    if (isSaving) {
        // While save is in progress, the save button needs to be disabled and show 'Saving...'
        return (_jsx(Button, { className: "gd-button-action s-saving_button", value: intl.formatMessage({ id: "controlButtons.saving.value" }), disabled: true, onClick: () => { } }));
    }
    return (_jsxs(BubbleHoverTrigger, { children: [
            _jsx(Button, { className: "gd-button-action save-publish-button s-save_button", value: intl.formatMessage(messages.controlButtonsSaveValue), onClick: onSaveClick, disabled: !isEnabled }), _jsx(Bubble, { alignPoints: [{ align: "bc tr" }], arrowOffsets: { "bc tr": [10, 20] }, alignTo: `.save-publish-button`, children: _jsx(FormattedMessage, { ...buttonTitle }) })
        ] }));
}
//# sourceMappingURL=DefaultSaveButton.js.map