"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var text_encoding_1 = require("text-encoding");
var sha1_1 = require("../utils/hash/sha1");
var string_1 = require("../utils/string");
var bigInt = require("big-integer");
var aes = require("aes-js");
var pako = require("pako");
var SubtitleDecryptor = /** @class */ (function () {
    function SubtitleDecryptor(id, iv, data) {
        this.id = id;
        this.iv = iv;
        this.data = data;
    }
    SubtitleDecryptor.prototype.getAuxKey = function (count, modulo, start) {
        var output = start.slice(0);
        for (var i = 0; i < count; i++) {
            output.push(output[output.length - 1] + output[output.length - 2]);
        }
        output = output.slice(2);
        return new Uint8Array(output.map(function (x) { return x % modulo + 33; }));
    };
    /**
     * Returns the key that's derived from the ID.
     */
    SubtitleDecryptor.prototype.getKey = function () {
        var key = bigInt(this.id);
        var num1 = bigInt(Math.floor(Math.pow(2, 25) * Math.sqrt(6.9)));
        var num2 = num1.xor(key).shiftLeft(5);
        var num3 = key.xor(num1);
        var num4 = num3.xor(num3.shiftRight(3)).xor(num2);
        var keyAux = this.getAuxKey(20, 97, [1, 2]);
        var num4Arr = new text_encoding_1.TextEncoder("ascii").encode(num4.toString());
        var shaData = new Uint8Array(keyAux.length + num4Arr.length);
        shaData.set(keyAux);
        shaData.set(num4Arr, keyAux.length);
        var sha1 = new sha1_1.SHA1();
        sha1.update(shaData);
        var shaHash = string_1.hexStringToByte(sha1.digest());
        var emptyArray = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
        var result = new Uint8Array(shaHash.length + emptyArray.length);
        result.set(shaHash);
        result.set(emptyArray, shaHash.length);
        return result;
    };
    /**
     * Returns the decrypted data.
     */
    SubtitleDecryptor.prototype.decrypt = function () {
        var aesCbc = new aes.ModeOfOperation.cbc(this.getKey(), this.iv);
        var decryptedData = aesCbc.decrypt(this.data);
        return pako.inflate(decryptedData);
    };
    return SubtitleDecryptor;
}());
exports.SubtitleDecryptor = SubtitleDecryptor;
//# sourceMappingURL=SubtitleDecryptor.js.map