import { Subscription } from 'rxjs';
import { ContextValues } from 'cdecode/client';
import { handleRequests, ClientThreadContextShape, Connection } from '@workbench-stack/extension-api';


/** @internal */
export class ClientContext implements ClientThreadContextShape {

    private subscription = new Subscription();

    /**
     * Context keys set by this server. To ensure that context values are cleaned up, all context properties that
     * the server set are cleared upon deinitialization. This errs on the side of clearing too much (if another
     * server set same context keys after this server, then those keys would also be cleared when this server's
     * client deinstializes).
     */
    private keys = new Set<string>();

    constructor(connection: Connection, private udpateContext: (udpates: ContextValues) => void) {
        handleRequests(connection, 'context', this);
    }

    public $acceptContextUpdates(updates: ContextValues): void {
        for (const key of Object.keys(updates)) {
            this.keys.add(key);
        }
        this.udpateContext(updates);
    }

    public unsubscribe(): void {
        /**
         * Clear all context properties whose keys were ever set by the server. See {@link ClientContext#keys}.
         */
        const updates: ContextValues = {};
        for (const key of this.keys) {
            updates[key] = null;
        }
        this.keys.clear();
        this.udpateContext(updates);

        this.subscription.unsubscribe();
    }
}
