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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { ScramInterceptor } from './scram-interceptor';
var TapEncryption = /** @class */ (function () {
    // private _sessionState: BehaviorSubject<EncryptionKeys>;
    function TapEncryption(tap) {
        var _this = this;
        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(function (event) {
            switch (event.type) {
                // Stop encryption on tap logout
                case 'tap-logout':
                    _this.stop();
                    break;
            }
        });
    }
    Object.defineProperty(TapEncryption.prototype, "isStarted", {
        get: function () {
            return this.scramInterceptor.options.encryption;
        },
        enumerable: true,
        configurable: true
    });
    TapEncryption.prototype.setEncryptionKeys = function (keys) {
        this.scramInterceptor.setEncryptionKeys(keys);
    };
    TapEncryption.prototype.setEncryptedFrameCounter = function (frameCounter) {
        this.scramInterceptor.setFrameCounter(frameCounter);
    };
    TapEncryption.prototype.getEncryptionOptions = function () {
        return this.scramInterceptor.options;
    };
    TapEncryption.prototype.refreshEncryptionInitializationVector = function () {
        return this.scramInterceptor.refreshInitializationVectors();
    };
    TapEncryption.prototype.setInitializationVectorRefreshPeriod = function (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
     */
    TapEncryption.prototype.start = function () {
        return __awaiter(this, void 0, void 0, function () {
            var keys;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.scramInterceptor.newSession()];
                    case 1:
                        keys = _a.sent();
                        this.scramInterceptor.resumeEncryption();
                        return [2 /*return*/, keys];
                }
            });
        });
    };
    /**
     * Disable encryption
     */
    TapEncryption.prototype.stop = function () {
        this.scramInterceptor.clearSession();
    };
    /**
     * Pause encryption
     */
    TapEncryption.prototype.pause = function () {
        this.scramInterceptor.pauseEncryption();
    };
    /**
     * Pause encryption
     */
    TapEncryption.prototype.resume = function () {
        this.scramInterceptor.resumeEncryption();
    };
    Object.defineProperty(TapEncryption.prototype, "sessionKey", {
        /**
         * Get current session key for encrypted communicatioin
         */
        get: function () {
            return this.scramInterceptor.sessionKey;
        },
        /**
         * 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: function (key) {
            this.scramInterceptor.sessionKey = key;
        },
        enumerable: true,
        configurable: true
    });
    return TapEncryption;
}());
export { TapEncryption };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFwLWVuY3J5cHRpb24uanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AaW90aXplL3RhcC9hdXRoLyIsInNvdXJjZXMiOlsibGliL3RhcC1lbmNyeXB0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUVBLE9BQU8sRUFBa0IsZ0JBQWdCLEVBQXFCLE1BQU0scUJBQXFCLENBQUM7QUFTMUY7SUFRSSwwREFBMEQ7SU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