import React, { useState, useEffect } from "react";
import { FEAGlobals } from "../../../../../common/Globals";
import { Button } from "../../../common/Button";
import { UserPreferencesStore } from "../../stores/UserPreferencesStore";
export const ResetPreferences = () => {
    const restartRequestedKey = "restartAfterResettingPreferences";
    const [showRestartLabel, setShowRestartLabel] = useState(false);
    const resetPreferences = async () => {
        const { data } = await FEAGlobals.FSBL.Clients.ConfigClient.getPreferences();
        for (const [key] of Object.entries(data !== null && data !== void 0 ? data : {})) {
            await FEAGlobals.FSBL.Clients.ConfigClient.unsetPreference({ field: key });
        }
        // set auxiliary preference to track if the restart happens
        await (UserPreferencesStore === null || UserPreferencesStore === void 0 ? void 0 : UserPreferencesStore.set([restartRequestedKey], true));
    };
    const resetToDefault = () => {
        FEAGlobals.FSBL.Clients.DialogManager.open("yesNo", {
            title: "Reset preferences?",
            monitor: "primary",
            question: "Are you sure you want to reset all preferences?",
            showNegativeButton: false,
        }, async (err, response) => {
            if (response && response.choice === "affirmative") {
                resetPreferences();
                setShowRestartLabel(true);
                // To keep the consistent UI of the General Tab resets the component state to default when user reset preferences
                await (UserPreferencesStore === null || UserPreferencesStore === void 0 ? void 0 : UserPreferencesStore.set(["resetToDefault"], null));
            }
        });
    };
    useEffect(() => {
        UserPreferencesStore === null || UserPreferencesStore === void 0 ? void 0 : UserPreferencesStore.get([restartRequestedKey], (err, value) => {
            if (value === true) {
                setShowRestartLabel(true);
            }
        });
    }, []);
    return (React.createElement("div", { className: "reset-preferences" },
        React.createElement(Button, { onClick: resetToDefault, fashion: "danger", text: "Reset preferences" }),
        showRestartLabel && (React.createElement("span", { className: "reset-preferences-label" }, "Finsemble needs to be restarted for the changes to take effect"))));
};
export default ResetPreferences;
//# sourceMappingURL=ResetPreferences.js.map