import { KeetaAnchorUserError, KeetaAnchorUserValidationError, KeetaAnchorCertificateRequiredError } from '../../lib/error.js';
import { Buffer, arrayBufferLikeToBuffer } from '../../lib/utils/buffer.js';
// #region Shared Constants
/** Content type for JSON payloads */
export const CONTENT_TYPE_JSON = 'application/json';
/** Content type for binary/octet-stream payloads */
export const CONTENT_TYPE_OCTET_STREAM = 'application/octet-stream';
/** Default TTL for signed URLs in seconds (1 hour) */
export const DEFAULT_SIGNED_URL_TTL_SECONDS = 3600;
export function getKeetaStorageAnchorPutRequestSigningData(input) {
    const visibility = input.visibility ?? 'private';
    const tags = input.tags ?? [];
    const sortedTags = [...tags].sort();
    return (['put', input.path, visibility, ...sortedTags]);
}
/**
 * Build signing data for an update-metadata request.
 * Tags are sorted with localeCompare for deterministic signatures.
 */
export function getKeetaStorageAnchorUpdateMetadataRequestSigningData(input) {
    const sortedTags = [...input.tags].sort(function (a, b) { return (a.localeCompare(b)); });
    return (['updateMetadata', input.path, input.visibility, ...sortedTags]);
}
export function getKeetaStorageAnchorGetRequestSigningData(input) {
    return (['get', input.path]);
}
export function getKeetaStorageAnchorDeleteRequestSigningData(input) {
    return (['delete', input.path]);
}
export function getKeetaStorageAnchorSearchRequestSigningData(input) {
    const limit = input.pagination?.limit ?? 0;
    const cursor = input.pagination?.cursor ?? '';
    return (['search', JSON.stringify(input.criteria), limit, cursor]);
}
/**
 * Get signing data for quota requests.
 * The input is unused because quota requests don't need request-specific signing -
 * authentication alone is sufficient.
 *
 * The parameter was kept for API consistency with other signing functions.
 */
export function getKeetaStorageAnchorQuotaRequestSigningData(_ignoreInput) {
    return (['quota']);
}
// #endregion
// #region Error Classes
class KeetaStorageAnchorDocumentNotFoundError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorDocumentNotFoundError';
    KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID;
    static KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID = 'ac137e18-2827-4542-a852-c650610899b5';
    constructor(message) {
        super(message ?? 'Document not found');
        this.statusCode = 404;
        Object.defineProperty(this, 'KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID', {
            value: KeetaStorageAnchorDocumentNotFoundError.KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID', KeetaStorageAnchorDocumentNotFoundError.KeetaStorageAnchorDocumentNotFoundErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorAccessDeniedError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorAccessDeniedError';
    KeetaStorageAnchorAccessDeniedErrorObjectTypeID;
    static KeetaStorageAnchorAccessDeniedErrorObjectTypeID = 'fb75fa8c-6ef0-47cb-b767-3c2cfbb73617';
    constructor(message) {
        super(message ?? 'Access denied');
        this.statusCode = 403;
        Object.defineProperty(this, 'KeetaStorageAnchorAccessDeniedErrorObjectTypeID', {
            value: KeetaStorageAnchorAccessDeniedError.KeetaStorageAnchorAccessDeniedErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorAccessDeniedErrorObjectTypeID', KeetaStorageAnchorAccessDeniedError.KeetaStorageAnchorAccessDeniedErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvalidPathError extends KeetaAnchorUserValidationError {
    static name = 'KeetaStorageAnchorInvalidPathError';
    KeetaStorageAnchorInvalidPathErrorObjectTypeID;
    static KeetaStorageAnchorInvalidPathErrorObjectTypeID = 'eb0e1c0d-2281-4b93-9f78-87bf166a4829';
    constructor(message) {
        super({ fields: [{ path: 'path', message: message ?? 'Invalid path format' }] }, message ?? 'Invalid path format');
        Object.defineProperty(this, 'KeetaStorageAnchorInvalidPathErrorObjectTypeID', {
            value: KeetaStorageAnchorInvalidPathError.KeetaStorageAnchorInvalidPathErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvalidPathErrorObjectTypeID', KeetaStorageAnchorInvalidPathError.KeetaStorageAnchorInvalidPathErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorQuotaExceededError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorQuotaExceededError';
    KeetaStorageAnchorQuotaExceededErrorObjectTypeID;
    static KeetaStorageAnchorQuotaExceededErrorObjectTypeID = 'c0b75028-644a-472b-8df4-b0a856814f99';
    /** Which quota limit was exceeded */
    quotaType;
    /** The configured maximum for the exceeded quota */
    limit;
    /** The current or attempted value that exceeded the limit */
    current;
    constructor(options) {
        super(options.message ?? `Quota exceeded: ${options.quotaType} (${options.current} exceeds limit of ${options.limit})`);
        this.statusCode = 413;
        this.quotaType = options.quotaType;
        this.limit = options.limit;
        this.current = options.current;
        Object.defineProperty(this, 'KeetaStorageAnchorQuotaExceededErrorObjectTypeID', {
            value: KeetaStorageAnchorQuotaExceededError.KeetaStorageAnchorQuotaExceededErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorQuotaExceededErrorObjectTypeID', KeetaStorageAnchorQuotaExceededError.KeetaStorageAnchorQuotaExceededErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this({ quotaType: 'maxObjectSize', limit: 0, current: 0, message });
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorPrincipalRequiredError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorPrincipalRequiredError';
    KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID;
    static KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID = '12e42092-d4db-435e-8a01-798e26f653b4';
    constructor(message) {
        super(message ?? 'Validated path requires anchor as principal');
        this.statusCode = 400;
        Object.defineProperty(this, 'KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID', {
            value: KeetaStorageAnchorPrincipalRequiredError.KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID', KeetaStorageAnchorPrincipalRequiredError.KeetaStorageAnchorPrincipalRequiredErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorValidationFailedError extends KeetaAnchorUserValidationError {
    static name = 'KeetaStorageAnchorValidationFailedError';
    KeetaStorageAnchorValidationFailedErrorObjectTypeID;
    static KeetaStorageAnchorValidationFailedErrorObjectTypeID = '73cadd95-cf39-466b-b9b6-484e1ae1ca9c';
    constructor(message) {
        const msg = message ?? 'Content validation failed';
        super({ fields: [{ path: 'content', message: msg }] }, msg);
        Object.defineProperty(this, 'KeetaStorageAnchorValidationFailedErrorObjectTypeID', {
            value: KeetaStorageAnchorValidationFailedError.KeetaStorageAnchorValidationFailedErrorObjectTypeID,
            enumerable: false
        });
    }
    get statusCode() {
        return (422);
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorValidationFailedErrorObjectTypeID', KeetaStorageAnchorValidationFailedError.KeetaStorageAnchorValidationFailedErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorSignatureExpiredError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorSignatureExpiredError';
    KeetaStorageAnchorSignatureExpiredErrorObjectTypeID;
    static KeetaStorageAnchorSignatureExpiredErrorObjectTypeID = '3a676e44-882b-4925-bf5f-bc5123cc0b20';
    constructor(message) {
        super(message ?? 'Pre-signed URL has expired');
        this.statusCode = 401;
        Object.defineProperty(this, 'KeetaStorageAnchorSignatureExpiredErrorObjectTypeID', {
            value: KeetaStorageAnchorSignatureExpiredError.KeetaStorageAnchorSignatureExpiredErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorSignatureExpiredErrorObjectTypeID', KeetaStorageAnchorSignatureExpiredError.KeetaStorageAnchorSignatureExpiredErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorSignatureInvalidError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorSignatureInvalidError';
    KeetaStorageAnchorSignatureInvalidErrorObjectTypeID;
    static KeetaStorageAnchorSignatureInvalidErrorObjectTypeID = '91831c73-31e2-4f27-a9d1-4ab9a5ed5663';
    constructor(message) {
        super(message ?? 'Pre-signed URL signature verification failed');
        this.statusCode = 401;
        Object.defineProperty(this, 'KeetaStorageAnchorSignatureInvalidErrorObjectTypeID', {
            value: KeetaStorageAnchorSignatureInvalidError.KeetaStorageAnchorSignatureInvalidErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorSignatureInvalidErrorObjectTypeID', KeetaStorageAnchorSignatureInvalidError.KeetaStorageAnchorSignatureInvalidErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorPrivateKeyRequiredError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorPrivateKeyRequiredError';
    KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID;
    static KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID = '36eaae98-1b1e-412b-ba5b-b9293cc37156';
    constructor(message) {
        super(message ?? 'Account with private key required for this operation');
        this.statusCode = 401;
        Object.defineProperty(this, 'KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID', {
            value: KeetaStorageAnchorPrivateKeyRequiredError.KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID', KeetaStorageAnchorPrivateKeyRequiredError.KeetaStorageAnchorPrivateKeyRequiredErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorServiceUnavailableError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorServiceUnavailableError';
    KeetaStorageAnchorServiceUnavailableErrorObjectTypeID;
    static KeetaStorageAnchorServiceUnavailableErrorObjectTypeID = 'b2671cd1-6abb-4bd4-9be2-d8d111c17bcf';
    constructor(message) {
        super(message ?? 'Storage service not available');
        this.statusCode = 503;
        Object.defineProperty(this, 'KeetaStorageAnchorServiceUnavailableErrorObjectTypeID', {
            value: KeetaStorageAnchorServiceUnavailableError.KeetaStorageAnchorServiceUnavailableErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorServiceUnavailableErrorObjectTypeID', KeetaStorageAnchorServiceUnavailableError.KeetaStorageAnchorServiceUnavailableErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorSignerRequiredError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorSignerRequiredError';
    KeetaStorageAnchorSignerRequiredErrorObjectTypeID;
    static KeetaStorageAnchorSignerRequiredErrorObjectTypeID = 'ce4a5581-1869-4656-88c6-63f0a29b46ca';
    constructor(message) {
        super(message ?? 'A Signer or UserClient with an associated Signer is required');
        this.statusCode = 401;
        Object.defineProperty(this, 'KeetaStorageAnchorSignerRequiredErrorObjectTypeID', {
            value: KeetaStorageAnchorSignerRequiredError.KeetaStorageAnchorSignerRequiredErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorSignerRequiredErrorObjectTypeID', KeetaStorageAnchorSignerRequiredError.KeetaStorageAnchorSignerRequiredErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorAccountRequiredError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorAccountRequiredError';
    KeetaStorageAnchorAccountRequiredErrorObjectTypeID;
    static KeetaStorageAnchorAccountRequiredErrorObjectTypeID = '496e5dbe-535f-4f24-acf4-a44d9d93fb75';
    constructor(message) {
        super(message ?? 'An Account or UserClient with an associated Account is required');
        this.statusCode = 401;
        Object.defineProperty(this, 'KeetaStorageAnchorAccountRequiredErrorObjectTypeID', {
            value: KeetaStorageAnchorAccountRequiredError.KeetaStorageAnchorAccountRequiredErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorAccountRequiredErrorObjectTypeID', KeetaStorageAnchorAccountRequiredError.KeetaStorageAnchorAccountRequiredErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorOperationNotSupportedError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorOperationNotSupportedError';
    KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID;
    static KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID = 'ac491ea6-f656-4eab-a430-051a0f201fff';
    constructor(operation) {
        super(operation ? `Storage service does not support '${operation}' operation` : 'Operation not supported');
        this.statusCode = 501;
        Object.defineProperty(this, 'KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID', {
            value: KeetaStorageAnchorOperationNotSupportedError.KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID', KeetaStorageAnchorOperationNotSupportedError.KeetaStorageAnchorOperationNotSupportedErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorUnsupportedAuthMethodError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorUnsupportedAuthMethodError';
    KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID;
    static KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID = '46cfbab9-934f-44b0-9216-03d397fdd6b6';
    constructor(method) {
        super(method ? `Unsupported authentication method: ${method}` : 'Unsupported authentication method');
        this.statusCode = 501;
        Object.defineProperty(this, 'KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID', {
            value: KeetaStorageAnchorUnsupportedAuthMethodError.KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID', KeetaStorageAnchorUnsupportedAuthMethodError.KeetaStorageAnchorUnsupportedAuthMethodErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvalidResponseError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorInvalidResponseError';
    KeetaStorageAnchorInvalidResponseErrorObjectTypeID;
    static KeetaStorageAnchorInvalidResponseErrorObjectTypeID = '02480186-7bc3-4a80-b6a9-23c3a9f606c9';
    constructor(details) {
        super(details ? `Invalid response from storage service: ${details}` : 'Invalid response from storage service');
        this.statusCode = 502;
        Object.defineProperty(this, 'KeetaStorageAnchorInvalidResponseErrorObjectTypeID', {
            value: KeetaStorageAnchorInvalidResponseError.KeetaStorageAnchorInvalidResponseErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvalidResponseErrorObjectTypeID', KeetaStorageAnchorInvalidResponseError.KeetaStorageAnchorInvalidResponseErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvalidAnchorAccountError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorInvalidAnchorAccountError';
    KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID;
    static KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID = '82bb0a94-0a5e-4e0a-b5c1-7532bbe09cd6';
    constructor(publicKey) {
        super(publicKey ? `Invalid anchor account public key: ${publicKey}` : 'Invalid anchor account public key');
        this.statusCode = 502;
        Object.defineProperty(this, 'KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID', {
            value: KeetaStorageAnchorInvalidAnchorAccountError.KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID', KeetaStorageAnchorInvalidAnchorAccountError.KeetaStorageAnchorInvalidAnchorAccountErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvariantViolationError extends KeetaAnchorUserError {
    static name = 'KeetaStorageAnchorInvariantViolationError';
    KeetaStorageAnchorInvariantViolationErrorObjectTypeID;
    static KeetaStorageAnchorInvariantViolationErrorObjectTypeID = 'a7c5d3e1-8b9f-4c2a-b3d4-e5f6a7b8c9d0';
    constructor(message) {
        super(message ?? 'Internal invariant violation');
        this.statusCode = 500;
        Object.defineProperty(this, 'KeetaStorageAnchorInvariantViolationErrorObjectTypeID', {
            value: KeetaStorageAnchorInvariantViolationError.KeetaStorageAnchorInvariantViolationErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvariantViolationErrorObjectTypeID', KeetaStorageAnchorInvariantViolationError.KeetaStorageAnchorInvariantViolationErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvalidTagError extends KeetaAnchorUserValidationError {
    static name = 'KeetaStorageAnchorInvalidTagError';
    KeetaStorageAnchorInvalidTagErrorObjectTypeID;
    static KeetaStorageAnchorInvalidTagErrorObjectTypeID = 'b8d6e4f2-9c0a-5d3b-c4e5-f6a7b8c9d0e1';
    constructor(message) {
        super({ fields: [{ path: 'tags', message: message ?? 'Invalid tag' }] }, message ?? 'Invalid tag');
        Object.defineProperty(this, 'KeetaStorageAnchorInvalidTagErrorObjectTypeID', {
            value: KeetaStorageAnchorInvalidTagError.KeetaStorageAnchorInvalidTagErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvalidTagErrorObjectTypeID', KeetaStorageAnchorInvalidTagError.KeetaStorageAnchorInvalidTagErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
class KeetaStorageAnchorInvalidMetadataError extends KeetaAnchorUserValidationError {
    static name = 'KeetaStorageAnchorInvalidMetadataError';
    KeetaStorageAnchorInvalidMetadataErrorObjectTypeID;
    static KeetaStorageAnchorInvalidMetadataErrorObjectTypeID = 'c9e7f5a3-0d1b-6e4c-d5f6-a7b8c9d0e1f2';
    constructor(reason) {
        const message = reason ? `Invalid metadata: ${reason}` : 'Invalid metadata';
        super({ fields: [{ path: 'metadata', message }] }, message);
        Object.defineProperty(this, 'KeetaStorageAnchorInvalidMetadataErrorObjectTypeID', {
            value: KeetaStorageAnchorInvalidMetadataError.KeetaStorageAnchorInvalidMetadataErrorObjectTypeID,
            enumerable: false
        });
    }
    static isInstance(input) {
        return (this.hasPropWithValue(input, 'KeetaStorageAnchorInvalidMetadataErrorObjectTypeID', KeetaStorageAnchorInvalidMetadataError.KeetaStorageAnchorInvalidMetadataErrorObjectTypeID));
    }
    static async fromJSON(input) {
        const { message, other } = this.extractErrorProperties(input, this);
        const error = new this(message);
        error.restoreFromJSON(other);
        return (error);
    }
}
export const Errors = {
    /**
     * The requested document/object was not found
     */
    DocumentNotFound: KeetaStorageAnchorDocumentNotFoundError,
    /**
     * Access to the document/object was denied
     */
    AccessDenied: KeetaStorageAnchorAccessDeniedError,
    /**
     * Path doesn't match /user/<pubkey>/... format
     */
    InvalidPath: KeetaStorageAnchorInvalidPathError,
    /**
     * Object size, count, or total storage limit exceeded
     */
    QuotaExceeded: KeetaStorageAnchorQuotaExceededError,
    /**
     * Validated path requires anchor as principal
     */
    AnchorPrincipalRequired: KeetaStorageAnchorPrincipalRequiredError,
    /**
     * Namespace validator rejected content
     */
    ValidationFailed: KeetaStorageAnchorValidationFailedError,
    /**
     * Pre-signed URL has expired
     */
    SignatureExpired: KeetaStorageAnchorSignatureExpiredError,
    /**
     * Pre-signed URL signature verification failed
     */
    SignatureInvalid: KeetaStorageAnchorSignatureInvalidError,
    /**
     * Account with private key required for this operation
     */
    PrivateKeyRequired: KeetaStorageAnchorPrivateKeyRequiredError,
    /**
     * Storage service is not available
     */
    ServiceUnavailable: KeetaStorageAnchorServiceUnavailableError,
    /**
     * A Signer or UserClient with an associated Signer is required
     */
    SignerRequired: KeetaStorageAnchorSignerRequiredError,
    /**
     * An Account or UserClient with an associated Account is required
     */
    AccountRequired: KeetaStorageAnchorAccountRequiredError,
    /**
     * The requested operation is not supported by this storage service
     */
    OperationNotSupported: KeetaStorageAnchorOperationNotSupportedError,
    /**
     * The authentication method is not supported
     */
    UnsupportedAuthMethod: KeetaStorageAnchorUnsupportedAuthMethodError,
    /**
     * Invalid response received from storage service
     */
    InvalidResponse: KeetaStorageAnchorInvalidResponseError,
    /**
     * Anchor account public key from service metadata is invalid
     */
    InvalidAnchorAccount: KeetaStorageAnchorInvalidAnchorAccountError,
    /**
     * Internal invariant violation - indicates a bug in the code
     */
    InvariantViolation: KeetaStorageAnchorInvariantViolationError,
    /**
     * Tag validation failed (invalid format, too long, or too many tags)
     */
    InvalidTag: KeetaStorageAnchorInvalidTagError,
    /**
     * Metadata validation failed against path policy constraints
     */
    InvalidMetadata: KeetaStorageAnchorInvalidMetadataError,
    /**
     * Caller account does not have a certificate chaining to a configured
     * trusted issuer.
     */
    CertificateRequired: KeetaAnchorCertificateRequiredError
};
// #endregion
// #region Container Payload Utilities
/**
 * Parse the decrypted plaintext from an EncryptedContainer.
 * The payload is expected to be JSON with mimeType and base64-encoded data,
 * keeping the mimeType encrypted along with the content.
 *
 * @param plaintext - The decrypted plaintext from an EncryptedContainer
 *
 * @returns Object containing mimeType and content Buffer
 */
export function parseContainerPayload(plaintext) {
    const payloadStr = Buffer.from(plaintext).toString('utf-8');
    try {
        const payload = JSON.parse(payloadStr);
        let mimeType = CONTENT_TYPE_OCTET_STREAM;
        let content;
        if (typeof payload === 'object' && payload !== null && !Array.isArray(payload)) {
            const payloadMime = 'mimeType' in payload ? payload.mimeType : undefined;
            const payloadData = 'data' in payload ? payload.data : undefined;
            if (typeof payloadMime === 'string') {
                mimeType = payloadMime;
            }
            if (typeof payloadData === 'string') {
                content = arrayBufferLikeToBuffer(Buffer.from(payloadData, 'base64'));
            }
            else {
                content = arrayBufferLikeToBuffer(plaintext);
            }
        }
        else {
            content = arrayBufferLikeToBuffer(plaintext);
        }
        return ({ mimeType, content });
    }
    catch {
        return ({
            mimeType: CONTENT_TYPE_OCTET_STREAM,
            content: arrayBufferLikeToBuffer(plaintext)
        });
    }
}
//# sourceMappingURL=common.js.map