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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWVuY3J5cHRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9hdXRoL3NyYy9saWIvdGFwLWVuY3J5cHRpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O0FBRUEsT0FBTyxFQUVMLGdCQUFnQixHQUVqQixNQUFNLHFCQUFxQixDQUFDO0FBUTdCLE1BQU0sT0FBTyxhQUFhO0lBT3hCLDBEQUEwRDtJQUUxRCxZQUFvQixHQUFRO1FBQVIsUUFBRyxHQUFILEdBQUcsQ0FBSztRQUMxQixJQUFJLENBQUMsZ0JBQWdCLEdBQUcsSUFBSSxnQkFBZ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