// class SetConfig

import {handlePreviewMessages} from "./handle_preview_messages.js";

class SetConfig {
    constructor() {
        let setConfigResolve;
        this.configPromise = new Promise((resolve, reject) => {
            setConfigResolve = resolve;
        });
        this.configResolve = setConfigResolve;
    }

    setConfig(config) {
        if (config?.ui?.getConfigFromMessage) {
            // Do not call configResolve in this case;
            // config will be received from a message
            handlePreviewMessages({
                setConfigFn: (cfg)=> {
                    cfg.ui_params = config.ui;
                    this.setConfig(cfg);
                    },
                storageKey: config.ui.storageKey
            });
            return;
        }

        this.config = config;

        // Inform promise that it has been resolved
        this.configResolve(config);
    }

    setConfigModule(modulePromise) {
        modulePromise.then(config => this.setConfig(config));
    }

    getConfig() {
        return this.config;
    }

    getConfigPromise() {
        return this.configPromise
    }

    getConfigAsync() {
        return this.configPromise
    }
    then(f) {
        return this.configPromise.then(f);
    }
}

export default SetConfig;