import { ConfigurationBindings } from '../base/ConfigurationBindings.js';
/**
 * Application configuration bindings that handles storing application configuration
 * on the web platform.
 */
export class WebConfigurationBindings extends ConfigurationBindings {
    constructor(app, workerUrl) {
        super(app);
        this.workerUrl = workerUrl;
        this.local = {
            get: async (key) => this.invoke('local.get', key),
            set: async (key, value) => this.invoke('local.set', key, value),
            delete: async (key) => this.invoke('local.delete', key),
        };
        this.session = {
            get: async (key) => this.invoke('session.get', key),
            set: async (key, value) => this.invoke('session.set', key, value),
            delete: async (key) => this.invoke('session.delete', key),
        };
        this.telemetry = {
            read: async () => this.invoke('telemetry.read'),
            set: async (config) => this.invoke('telemetry.set', config),
        };
        this.environment = {
            add: async (env, init = {}) => this.invoke('environment.add', env, init),
            update: async (env) => this.invoke('environment.update', env),
            read: async (id) => this.invoke('environment.read', id),
            remove: async (id) => this.invoke('environment.remove', id),
            setDefault: async (id) => this.invoke('environment.setDefault', id),
            list: async () => this.invoke('environment.list')
        };
        // 
    }
    async initialize() {
        await super.initialize();
        await this.createWorker();
    }
    async createWorker() {
        return new Promise((resolve, reject) => {
            const worker = new SharedWorker(this.workerUrl, {
                type: 'module',
                name: 'AppConfig',
            });
            worker.port.start();
            worker.onerror = () => reject(new Error('Unable to initialize the IO process.'));
            this.worker = worker;
            const binder = this._ioHandler.bind(this);
            worker.port.addEventListener('message', function init() {
                worker.port.removeEventListener('message', init);
                worker.port.addEventListener('message', binder);
                resolve();
            });
            setTimeout(() => {
                worker.port.postMessage('init');
            }, 1);
        });
    }
    _ioHandler(e) {
        if (e.data === 'ready') {
            return;
        }
        const event = e.data;
        this.handleIoMessage(event);
    }
    sendIoCommand(cmd) {
        const { worker } = this;
        if (!worker) {
            throw new Error(`IO not ready.`);
        }
        worker.port.postMessage(cmd);
    }
}
//# sourceMappingURL=WebConfigurationBindings.js.map