import * as KeetaNet from '@keetanetwork/keetanet-client';
import * as Signing from '../../lib/utils/signing.js';
import { KeetaAnchorUserError } from '../../lib/error.js';
class KeetaKYCAnchorVerificationNotFoundError extends KeetaAnchorUserError {
    static name = 'KeetaKYCAnchorVerificationNotFoundError';
    KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID;
    static KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID = '79d7036f-58ad-4eec-b64c-26bdf98cd3c1';
    logLevel = 'DEBUG';
    constructor(message) {
        super(message ?? 'Verification ID not found');
        this.statusCode = 400;
        Object.defineProperty(this, 'KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID', {
            value: KeetaKYCAnchorVerificationNotFoundError.KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID', KeetaKYCAnchorVerificationNotFoundError.KeetaKYCAnchorVerificationNotFoundErrorObjectTypeID));
    }
}
class KeetaKYCAnchorCertificateNotFoundError extends KeetaAnchorUserError {
    static name = 'KeetaKYCAnchorCertificateNotFoundError';
    KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID;
    static KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID = '6f4d9620-df33-468e-ae03-898d4fa67aeb';
    constructor(message) {
        super(message ?? 'Certificate not found (pending)');
        this.statusCode = 404;
        Object.defineProperty(this, 'KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID', {
            value: KeetaKYCAnchorCertificateNotFoundError.KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID', KeetaKYCAnchorCertificateNotFoundError.KeetaKYCAnchorCertificateNotFoundErrorObjectTypeID));
    }
}
class KeetaKYCAnchorCertificatePaymentRequired extends KeetaAnchorUserError {
    static name = 'KeetaKYCAnchorCertificatePaymentRequired';
    KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID;
    static KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID = '92b2481d-b29b-4fa2-a295-3a47d99c0869';
    amount;
    token;
    constructor(cost, message) {
        super(message ?? 'Payment required for certificate');
        this.statusCode = 402;
        this.amount = BigInt(cost.amount);
        this.token = KeetaNet.lib.Account.toAccount(cost.token);
        Object.defineProperty(this, 'KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID', {
            value: KeetaKYCAnchorCertificatePaymentRequired.KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID', KeetaKYCAnchorCertificatePaymentRequired.KeetaKYCAnchorCertificatePaymentRequiredObjectTypeID));
    }
    asErrorResponse(contentType) {
        let message = this.message;
        if (contentType === 'application/json') {
            message = JSON.stringify({
                ok: false,
                code: 'KEETA_ANCHOR_KYC_PAYMENT_REQUIRED',
                data: {
                    cost: {
                        amount: `0x${this.amount.toString(16)}`,
                        token: this.token.publicKeyString.get()
                    }
                },
                error: this.message
            });
        }
        return ({
            error: message,
            statusCode: this.statusCode,
            contentType: contentType
        });
    }
    toJSON() {
        return ({
            ok: false,
            retryable: this.retryable,
            error: this.message,
            name: this.name,
            statusCode: this.statusCode,
            amount: `0x${this.amount.toString(16)}`,
            token: this.token.publicKeyString.get()
        });
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        // Extract required properties specific to PaymentRequired
        if (!('amount' in other) || typeof other.amount !== 'string') {
            throw (new Error('Invalid KeetaKYCAnchorCertificatePaymentRequired JSON object: missing or invalid amount'));
        }
        if (!('token' in other) || typeof other.token !== 'string') {
            throw (new Error('Invalid KeetaKYCAnchorCertificatePaymentRequired JSON object: missing or invalid token'));
        }
        const error = new this({
            amount: other.amount,
            token: other.token
        }, message);
        error.restoreFromJSON(other);
        return (error);
    }
}
export const Errors = {
    /**
     * The verification ID was not found
     */
    VerificationNotFound: KeetaKYCAnchorVerificationNotFoundError,
    /**
     * The certificate for the verification ID was not found
     * (typically this means the verification is still pending)
     */
    CertificateNotFound: KeetaKYCAnchorCertificateNotFoundError,
    /**
     * Payment is required for the certificate
     */
    PaymentRequired: KeetaKYCAnchorCertificatePaymentRequired
};
export async function generateSignedData(account) {
    return (await Signing.SignData(account, []));
}
export async function verifySignedData(request) {
    const account = KeetaNet.lib.Account.fromPublicKeyString(request.account);
    return (await Signing.VerifySignedData(account, [], request.signed));
}
//# sourceMappingURL=common.js.map