"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Terminal3Vc = void 0;
const BigDecimal_1 = __importDefault(require("../shared/BigDecimal"));
const InvalidVcFormat_1 = require("./error/InvalidVcFormat");
const ErrorDecodingVc_1 = require("./error/ErrorDecodingVc");
const InvalidVcDates_1 = require("./error/InvalidVcDates");
const MissingVcHolder_1 = require("./error/MissingVcHolder");
const MissingVcIssuer_1 = require("./error/MissingVcIssuer");
class Terminal3Vc {
    static vcFromBase64(base64) {
        try {
            const jsonString = Buffer.from(base64, "base64").toString("utf-8");
            const parsedData = JSON.parse(jsonString);
            if (!parsedData || typeof parsedData !== "object") {
                throw new InvalidVcFormat_1.InvalidVcFormat();
            }
            return parsedData;
        }
        catch (error) {
            throw new ErrorDecodingVc_1.ErrorDecodingVc(error);
        }
    }
    static extractHolder(signedCredential) {
        const holder = signedCredential.credentialSubject.id.split(":").pop();
        if (!holder)
            throw new MissingVcHolder_1.MissingVcHolder();
        return holder;
    }
    static extractIssuer(signedCredential) {
        const issuer = signedCredential.issuer.split(":").pop();
        if (!issuer)
            throw new MissingVcIssuer_1.MissingVcIssuer();
        return issuer;
    }
    static checkValidDates(signedCredential) {
        this.setDefaultDates(signedCredential);
        this.validateDateOrder(signedCredential);
        return signedCredential;
    }
    static setDefaultDates(signedCredential) {
        signedCredential.validFrom = signedCredential.validFrom
            ? Date.parse(signedCredential.validFrom).toString()
            : Date.now().toString();
        signedCredential.validUntil =
            signedCredential.validUntil && signedCredential.validUntil.trim() !== ""
                ? Date.parse(signedCredential.validUntil).toString()
                : (Date.now() + 100 * 365 * 24 * 60 * 60 * 1000).toString();
    }
    static validateDateOrder(signedCredential) {
        if (!signedCredential.validFrom || !signedCredential.validUntil)
            throw new InvalidVcDates_1.InvalidVcDates();
        if (BigDecimal_1.default.fromString(signedCredential.validFrom).isGreaterThan(BigDecimal_1.default.fromString(signedCredential.validUntil))) {
            throw new InvalidVcDates_1.InvalidVcDates();
        }
    }
}
exports.Terminal3Vc = Terminal3Vc;
