import { get, set, del } from 'idb-keyval';
import { ConfigurationBindings, TelemetryKey, } from '../base/ConfigurationBindings.js';
import { ConfigBroadcastChannel } from '../../lib/config/Config.js';
/**
 * The background worker supporting application configuration.
 * It stores user local and session configuration, telemetry configuration,
 * and, in the future, any other data stored by the application locally.
 *
 * Data that are stored on the server (through the API) should not be stored here.
 *
 * The logic uses a `BroadcastChannel` to populate the changes across opened tabs and contexts.
 */
export class WebConfigurationBindings extends ConfigurationBindings {
    broadcast = new BroadcastChannel(ConfigBroadcastChannel);
    sendIoCommand() {
        //
    }
    local = {
        /**
         * Reads a previously stored local value.
         *
         * @param key The key under which the property was stored.
         * @returns The configuration value or undefined when not found.
         */
        get: async (key) => {
            const value = await get(key);
            if (value) {
                return value.value;
            }
            return undefined;
        },
        /**
         * Sets a config property that is permanently stored.
         *
         * @param key The key under to store the value.
         * @param value The value to store. If this is not a primitive it will be serialized with `JSON.stringify()`.
         */
        set: async (key, value) => {
            const storedValue = {
                type: typeof value,
                value,
            };
            await set(key, storedValue);
            this.broadcast.postMessage({
                path: 'local.set',
                key,
                value,
            });
        },
        /**
         * Deletes previously stored local value.
         *
         * @param key The key under which the property was stored.
         */
        delete: async (key) => {
            await del(key);
            this.broadcast.postMessage({
                path: 'local.delete',
                key,
            });
        },
    };
    session = {
        get: async (key) => {
            const data = sessionStorage.getItem(key);
            if (data === null) {
                return undefined;
            }
            let restored;
            try {
                const parsed = JSON.parse(data);
                restored = parsed.value;
            }
            catch {
                // ...
            }
            return restored;
        },
        set: async (key, value) => {
            const storedValue = {
                type: typeof value,
                value,
            };
            sessionStorage.setItem(key, JSON.stringify(storedValue));
            this.broadcast.postMessage({
                path: 'session.set',
                key,
                value,
            });
        },
        delete: async (key) => {
            sessionStorage.removeItem(key);
            this.broadcast.postMessage({
                path: 'session.delete',
                key,
            });
        },
    };
    telemetry = {
        read: async () => {
            let data = (await get(TelemetryKey));
            if (!data) {
                data = {
                    level: 'noting',
                };
            }
            return data;
        },
        set: async (config) => {
            await set(TelemetryKey, config);
            this.broadcast.postMessage({
                path: 'telemetry.set',
                value: config,
            });
        },
    };
}
//# sourceMappingURL=WebConfigurationBindings.js.map