import React, { useEffect, useState } from "react";
import ScheduledClose from "../general/ScheduledClose";
import DownloadPreferences from "../general/DownloadPreferences";
import { Actions, UserPreferencesStore } from "../../stores/UserPreferencesStore";
import { Checkbox } from "../../../common/Checkbox";
import { FEAGlobals } from "../../../../../common/Globals";
import ResetPreferences from "../general/ResetPreferences";
const General = () => {
    const [discardUnsavedChanges, setDiscardUnsavedChanges] = useState(false);
    const setDefaultState = () => {
        setDiscardUnsavedChanges(false);
    };
    // To keep the consistent UI of the General Tab resets the component state to default when user reset preferences
    useEffect(() => {
        UserPreferencesStore === null || UserPreferencesStore === void 0 ? void 0 : UserPreferencesStore.addListener({ field: "resetToDefault" }, setDefaultState);
        return () => {
            UserPreferencesStore === null || UserPreferencesStore === void 0 ? void 0 : UserPreferencesStore.removeListener({ field: "resetToDefault" }, setDefaultState);
        };
    }, []);
    const toggleDiscardUnsavedChanges = () => {
        setDiscardUnsavedChanges(!discardUnsavedChanges);
        Actions.setDiscardUnsavedChangesPreference(!discardUnsavedChanges);
    };
    const setDiscardValue = (err, response) => {
        var _a;
        let value = false;
        if (err) {
            FEAGlobals.FSBL.Clients.Logger.system.error("Error fetching user preferences data: ", err);
        }
        else {
            value = (_a = response === null || response === void 0 ? void 0 : response["finsemble.scheduledCloseDiscardUnsavedChanges"]) !== null && _a !== void 0 ? _a : false;
        }
        setDiscardUnsavedChanges(value);
    };
    useEffect(() => {
        Actions.getPreferences(setDiscardValue);
    }, []);
    return (React.createElement("div", { style: { marginTop: -7 } },
        React.createElement(ScheduledClose, { getPreferences: Actions.getPreferences, setPreferences: Actions.setScheduledClose, configKey: "finsemble.scheduledRestart", label: "Restart" }),
        React.createElement(ScheduledClose, { getPreferences: Actions.getPreferences, setPreferences: Actions.setScheduledClose, configKey: "finsemble.scheduledShutdown", label: "Shutdown" }),
        React.createElement("span", { className: "general-row" },
            React.createElement(Checkbox, { onClick: toggleDiscardUnsavedChanges, checked: discardUnsavedChanges, label: "Discard unsaved workspace changes" })),
        React.createElement("div", { className: "download-preferences-container" },
            React.createElement(DownloadPreferences, { getPreferences: Actions.getPreferences, setPreferences: Actions.setDownloads, configKey: "finsemble.downloads" })),
        React.createElement("span", { className: "general-row" },
            React.createElement(ResetPreferences, null))));
};
export default General;
//# sourceMappingURL=General.js.map