import { FEAGlobals } from "../../../../common/Globals";
let UserPreferencesStore;
let WorkspaceManagementMenuStore;
let updateConstants;
const Actions = {
    initialize: function () {
        FEAGlobals.FSBL.Clients.WorkspaceClient.getWorkspaceActions((err, constants) => {
            if (constants)
                updateConstants = constants;
        });
        // Gets the workspace list and sets the value in the store.
        FEAGlobals.FSBL.Clients.WorkspaceClient.getWorkspaces(async (err, workspaces) => {
            if (workspaces) {
                await UserPreferencesStore.set(["WorkspaceList"], workspaces);
            }
        });
        // Get the activeWorkspace and set the value.
        FEAGlobals.FSBL.Clients.WorkspaceClient.getActiveWorkspace(async (err, activeWorkspace) => {
            if (activeWorkspace) {
                await UserPreferencesStore.set(["activeWorkspace"], activeWorkspace);
            }
        });
        /**
         * We listen here for any workspace updates, and pass them to the store.
         * **NOTE**: You may notice that the signature of this callback is different from the previous ones. In this case we receive a `response`, instead of `workspaces` or `activeWorkspace`. This is because this callback is for a `RouterClient` message. The functions above are callbacks to `WorkspaceClient` API calls.
         */
        FEAGlobals.FSBL.Clients.RouterClient.subscribe("Finsemble.WorkspaceService.update", async (err, response) => {
            if (response.data && response.data.activeWorkspace) {
                await UserPreferencesStore.set(["activeWorkspace"], response.data.activeWorkspace);
                if (updateConstants) {
                    switch (response.data.reason) {
                        case updateConstants.GET_ACTIVE_WORKSPACE:
                        case updateConstants.NEW_WORKSPACE:
                        case updateConstants.IMPORT:
                        case updateConstants.SAVE:
                        case updateConstants.SAVE_AS:
                        case updateConstants.REMOVE:
                        case updateConstants.SWITCH_TO:
                        case updateConstants.SET_WORKSPACE_ORDER:
                        case updateConstants.ADD_WINDOW:
                        case updateConstants.REMOVE_WINDOW:
                        case updateConstants.RENAME:
                            Actions.getWorkspaces();
                            break;
                        default:
                            break;
                    }
                }
            }
        });
    },
    getWorkspaces(cb = Function.prototype) {
        // Gets the workspace list and sets the value in the store.
        FEAGlobals.FSBL.Clients.WorkspaceClient.getWorkspaces(async (err, workspaces) => {
            await UserPreferencesStore.set(["WorkspaceList"], workspaces);
            cb();
        });
    },
    /**
     * By making the value of `finsemble.scheduledRestart` falsy, the application will remove any existing restart timers.
     * @deprecated use `setScheduledRestart(false)` instead.
     */
    disableScheduledRestart: () => {
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: "finsemble.scheduledRestart",
            value: false,
        });
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: "finsemble.scheduledCloseDiscardUnsavedChanges",
            value: false,
        });
    },
    /**
     *  Finsemble listens for this piece of config. When it changes, it will schedule a daily restart at that time.
     *  By making the value of `finsemble.scheduledRestart` falsy, the application will remove any existing restart timers.
     *  @deprecated use setScheduledClose
     */
    setScheduledRestart: (val) => {
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: "finsemble.scheduledRestart",
            value: val,
        });
    },
    /**
     *  Finsemble listens the config set by this function. When it changes, it will schedule a daily restart/shutdown at
     *  that time. By making the value falsy, the application will remove any existing restart/shutdown timers.
     *
     */
    setScheduledClose: (key, value) => {
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: key,
            value: value,
        });
    },
    /**
     * Allows download settings to persist after restart.
     * Saves an object containing the chosen location and a list of the last
     * three (or less) chosen locations to show in the dropdown
     */
    setDownloads: (key, value) => {
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: key,
            value: value,
        });
    },
    setDiscardUnsavedChangesPreference: (value) => {
        FEAGlobals.FSBL.Clients.ConfigClient.setPreference({
            field: "finsemble.scheduledCloseDiscardUnsavedChanges",
            value: value,
        });
    },
    getPreferences: (callback) => {
        FEAGlobals.FSBL.Clients.ConfigClient.getPreferences(callback);
    },
};
async function createLocalStore(done) {
    // While the order of these two functions is unimportant, they cannot be done in series as we don't want
    // to call the callback until both stores have been initiated. This keeps the initialization function
    // executing until both stores either have values, or fail to initialize.
    FEAGlobals.FSBL.Clients.DistributedStoreClient.createStore({ store: "Finsemble-UserPreferences-Local-Store" }, (preferencesErr, preferencesStore) => {
        if (preferencesErr) {
            FEAGlobals.FSBL.Clients.Logger.system.error("Error fetching user preferences data: ", preferencesErr);
        }
        if (preferencesStore) {
            UserPreferencesStore = preferencesStore;
        }
        done();
    });
}
/**
 * Initializes the store for the User Preferences Menu.
 */
function initialize(WorkspaceMngMenuStore, cb) {
    WorkspaceManagementMenuStore = WorkspaceMngMenuStore;
    createLocalStore(() => {
        Actions.initialize();
        cb(UserPreferencesStore);
    });
}
export { WorkspaceManagementMenuStore, initialize, UserPreferencesStore, Actions };
//# sourceMappingURL=UserPreferencesStore.js.map