"use strict";
/**
 * This script is responsible to handle the data stored on QR-Codes.
 * For detailed information, please take a look at DEVELOPMENT.md in
 * the projects root directory.
 */
exports.__esModule = true;
var Format = /** @class */ (function () {
    /**
     * Format handler
     * @param text The unparsed text provided on a QR-Code
     */
    function Format(text) {
        // Example: %session_key%§xa|mondei1|passwords_not2134|email_too@example.com|§q|(§a)§gh|(uncompressed)value
        //          Google|mondei1|passwords_not2134|email_either@example.com|https://accounts.google.com|(2fa_backup)245131,...|(uncompressed)value
        this.text = null;
        this.format = {
            title: "",
            username: "",
            password: "",
            email: "",
            loginURL: null
        };
        this.text = text;
    }
    Format.prototype.isCompatible = function () {
        return true;
    };
    Format.prototype.decrypt = function () {
        if (this.isCompatible()) {
        }
    };
    Format.prototype.parse = function () {
        var parts = this.text.split('|');
        // Parse static values
        this.format.title = parts[0];
        // Check if a decryption key exists at the start
        if (this.format.title.startsWith('%')) {
            this.format.decryptionKey = "";
            // Extract
            for (var i = 1; i < this.format.title.length; i++) {
                var char = this.format.title.charAt(i);
                if (char != '%') {
                    this.format.decryptionKey += char;
                }
                else {
                    break;
                }
            }
            // Remove decryption key from first value
            this.format.title = this.format.title.substr(this.format.decryptionKey.length + 2, this.format.title.length); // + 2 because of the two '%'
        }
        this.format.username = parts[1];
        this.format.password = parts[2];
        this.format.email = parts[3];
        this.format.loginURL = new URL(parts[4]);
        console.log(this.format);
    };
    Format.prototype.build = function (data) {
        if (data == undefined)
            data = this.format;
        var raw = "";
        if (data.decryptionKey != undefined) {
            raw += "%" + data.decryptionKey + "%";
        }
        raw += data.title + "|";
        raw += data.username + "|";
        raw += data.password + "|";
        raw += data.email + "|";
        raw += data.loginURL + "|";
        if (data.custom != undefined) {
            data.custom.forEach(function (element) {
                raw += "(" + element.key + ")" + element.value + "|";
            });
        }
        raw = raw.substr(0, raw.length - 1); // Remove the last |
        return raw;
    };
    return Format;
}());
exports.Format = Format;
