var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { ScramInterceptor } from './scram-interceptor';
export class TapEncryption {
    // private _sessionState: BehaviorSubject<EncryptionKeys>;
    constructor(tap) {
        this.tap = tap;
        this.scramInterceptor = new ScramInterceptor(this.tap.service.scram);
        this.tap.client.addInterceptor(this.scramInterceptor);
        // this._sessionState = new BehaviorSubject<EncryptionKeys>(this._scramInterceptor.options.keys);
        this.tap
            .events
            .subscribe(event => {
            switch (event.type) {
                // Stop encryption on tap logout
                case 'tap-logout':
                    this.stop();
                    break;
            }
        });
    }
    get isStarted() {
        return this.scramInterceptor.options.encryption;
    }
    setEncryptionKeys(keys) {
        this.scramInterceptor.setEncryptionKeys(keys);
    }
    setEncryptedFrameCounter(frameCounter) {
        this.scramInterceptor.setFrameCounter(frameCounter);
    }
    getEncryptionOptions() {
        return this.scramInterceptor.options;
    }
    refreshEncryptionInitializationVector() {
        return this.scramInterceptor.refreshInitializationVectors();
    }
    setInitializationVectorRefreshPeriod(period) {
        return this.scramInterceptor.setInitializationVectorRefreshPeriod(period);
    }
    // /**
    //  * Enable/Disable encrytion when communicating with a device
    //  * @param enable true if requests must be encrypted
    //  * @param resetSessionKey true if you want to reset the the session key.
    //  * @param refreshInitializationVector true if you want to change initialization vectors
    //  *
    //  * @deprecated use {@link encryption()} instead
    //  */
    // public async enableEncryption(
    //     enable: boolean,
    //     resetSessionKey = false,
    //     refreshInitializationVector = false
    // ): Promise<void> {
    //     return this.encryption(
    //         enable,
    //         resetSessionKey,
    //         refreshInitializationVector
    //     );
    // }
    // /**
    //  * Enable/Disable encrytion when communicating with a device
    //  * @param enable true if requests must be encrypted
    //  * @param resetSessionKey true if you want to reset the the session key.
    //  * @param refreshInitializationVector true if you want to change initialization vectors
    //  *
    //  */
    // public async encryption(
    //     enable: boolean,
    //     resetSessionKey = false,
    //     refreshInitializationVector = false
    // ): Promise<void> {
    //     await this.scramInterceptor.encryption(
    //         enable,
    //         resetSessionKey,
    //         refreshInitializationVector
    //     );
    // }
    /**
     * Enable encryption
     */
    start() {
        return __awaiter(this, void 0, void 0, function* () {
            const keys = yield this.scramInterceptor.newSession();
            this.scramInterceptor.resumeEncryption();
            return keys;
        });
    }
    /**
     * Disable encryption
     */
    stop() {
        this.scramInterceptor.clearSession();
    }
    /**
     * Pause encryption
     */
    pause() {
        this.scramInterceptor.pauseEncryption();
    }
    /**
     * Pause encryption
     */
    resume() {
        this.scramInterceptor.resumeEncryption();
    }
    /**
     * Setter for the session key
     * @param key if null, it will stop encryption and remove session key. If true it will update session key used for encryption
     */
    set sessionKey(key) {
        this.scramInterceptor.sessionKey = key;
    }
    /**
     * Get current session key for encrypted communicatioin
     */
    get sessionKey() {
        return this.scramInterceptor.sessionKey;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWVuY3J5cHRpb24uanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AaW90aXplL3RhcC9hdXRoLyIsInNvdXJjZXMiOlsibGliL3RhcC1lbmNyeXB0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUVBLE9BQU8sRUFBa0IsZ0JBQWdCLEVBQXFCLE1BQU0scUJBQXFCLENBQUM7QUFTMUYsTUFBTSxPQUFPLGFBQWE7SUFRdEIsMERBQTBEO0lBRTFELFlBQW9CLEdBQVE7UUFBUixRQUFHLEdBQUgsR0FBRyxDQUFLO1FBQ3hCLElBQUksQ0FBQyxnQkFBZ0IsR0FBRyxJQUFJLGdCQUFn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