/**
 * A type-safe wrapper around {@link Record} that carries the data type `T`
 * through its entire lifecycle — from write to read, query, update, and
 * subscribe.
 *
 * `TypedRecord<T>` uses composition (not inheritance) to wrap the underlying
 * untyped `Record` class. All read-only getters and lifecycle methods are
 * forwarded, while data-access and mutation methods are enhanced with the
 * generic `T`:
 *
 * - `.data.json()` returns `Promise<T>` instead of `Promise<unknown>`.
 * - `.update({ data })` accepts `Partial<T>` for the data payload.
 *
 * @example
 * ```ts
 * const { record } = await typed.records.write('friend', {
 *   data: { did: 'did:example:alice', alias: 'Alice' },
 * });
 *
 * // record is TypedRecord<FriendData>
 * const data = await record.data.json(); // FriendData — no manual cast
 * ```
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
// ---------------------------------------------------------------------------
// TypedRecord class
// ---------------------------------------------------------------------------
/**
 * A type-safe wrapper around {@link Record} that preserves the data type `T`.
 *
 * Obtain instances through `TypedWeb5.records.write()`, `.query()`, `.read()`,
 * or `.subscribe()` — never construct directly.
 */
export class TypedRecord {
    constructor(record) {
        this._record = record;
    }
    // -------------------------------------------------------------------------
    // Escape hatch
    // -------------------------------------------------------------------------
    /** Access the underlying untyped {@link Record} for advanced use cases. */
    get rawRecord() {
        return this._record;
    }
    // -------------------------------------------------------------------------
    // Typed data accessor
    // -------------------------------------------------------------------------
    /**
     * Returns the data of the current record with type-safe accessors.
     *
     * The `json()` method returns `Promise<T>` — no manual generic needed.
     *
     * @throws `Error` if the record has been deleted.
     */
    get data() {
        const underlying = this._record.data;
        return {
            blob: () => underlying.blob(),
            bytes: () => underlying.bytes(),
            json: () => underlying.json(),
            text: () => underlying.text(),
            stream: () => underlying.stream(),
            then: underlying.then.bind(underlying),
            catch: underlying.catch.bind(underlying),
        };
    }
    // -------------------------------------------------------------------------
    // Typed mutation methods
    // -------------------------------------------------------------------------
    /**
     * Update the current record on the DWN.
     *
     * @param params - Parameters including the typed `data` payload.
     * @returns The status and an updated {@link TypedRecord}.
     * @throws `Error` if the record has been deleted.
     */
    update(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const { status, record } = yield this._record.update(params);
            return { status, record: new TypedRecord(record) };
        });
    }
    /**
     * Delete the current record on the DWN.
     *
     * @param params - Delete parameters.
     * @returns The status and a {@link TypedRecord} reflecting the deleted state.
     */
    delete(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const { status, record } = yield this._record.delete(params);
            return { status, record: new TypedRecord(record) };
        });
    }
    // -------------------------------------------------------------------------
    // Forwarded lifecycle methods
    // -------------------------------------------------------------------------
    /**
     * Stores the current record state to the owner's DWN.
     *
     * @param importRecord - If true, sign as owner before storing. Defaults to false.
     */
    store() {
        return __awaiter(this, arguments, void 0, function* (importRecord = false) {
            return this._record.store(importRecord);
        });
    }
    /**
     * Signs and optionally stores the record to the owner's DWN.
     * Useful when importing a record signed by someone else.
     *
     * @param store - If true, store after signing. Defaults to true.
     */
    import() {
        return __awaiter(this, arguments, void 0, function* (store = true) {
            return this._record.import(store);
        });
    }
    /**
     * Send the current record to a remote DWN.
     *
     * @param target - Optional DID of the target DWN. Defaults to the connected DID.
     */
    send(target) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._record.send(target);
        });
    }
    /**
     * Returns a JSON representation of the Record instance.
     */
    toJSON() {
        return this._record.toJSON();
    }
    /**
     * Returns a string representation of the Record instance.
     */
    toString() {
        return this._record.toString();
    }
    /**
     * Returns a pagination cursor for the current record given a sort order.
     */
    paginationCursor(sort) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._record.paginationCursor(sort);
        });
    }
    // -------------------------------------------------------------------------
    // Forwarded immutable property getters
    // -------------------------------------------------------------------------
    /** Record's ID. */
    get id() { return this._record.id; }
    /** Record's context ID. */
    get contextId() { return this._record.contextId; }
    /** Record's creation date. */
    get dateCreated() { return this._record.dateCreated; }
    /** Record's parent ID. */
    get parentId() { return this._record.parentId; }
    /** Record's protocol. */
    get protocol() { return this._record.protocol; }
    /** Record's protocol path. */
    get protocolPath() { return this._record.protocolPath; }
    /** Record's recipient. */
    get recipient() { return this._record.recipient; }
    /** Record's schema. */
    get schema() { return this._record.schema; }
    // -------------------------------------------------------------------------
    // Forwarded mutable property getters
    // -------------------------------------------------------------------------
    /** Record's data format. */
    get dataFormat() { return this._record.dataFormat; }
    /** Record's data CID. */
    get dataCid() { return this._record.dataCid; }
    /** Record's data size. */
    get dataSize() { return this._record.dataSize; }
    /** Record's published date. */
    get datePublished() { return this._record.datePublished; }
    /** Record's published status. */
    get published() { return this._record.published; }
    /** Tags of the record. */
    get tags() {
        return this._record.tags;
    }
    // -------------------------------------------------------------------------
    // Forwarded state-dependent property getters
    // -------------------------------------------------------------------------
    /** DID that is the logical author of the Record. */
    get author() { return this._record.author; }
    /** DID that is the original creator of the Record. */
    get creator() { return this._record.creator; }
    /** Record's message timestamp. */
    get timestamp() { return this._record.timestamp; }
    /** Record's encryption details. */
    get encryption() {
        return this._record.encryption;
    }
    /** Record's authorization. */
    get authorization() {
        return this._record.authorization;
    }
    /** Record's attestation. */
    get attestation() {
        return this._record.attestation;
    }
    /** Role under which the author is writing the record. */
    get protocolRole() { return this._record.protocolRole; }
    /** Record's deleted state. */
    get deleted() { return this._record.deleted; }
    /** Record's initial write if the record has been updated. */
    get initialWrite() {
        return this._record.initialWrite;
    }
    /** The raw DWN message backing this record. */
    get rawMessage() {
        return this._record.rawMessage;
    }
}
//# sourceMappingURL=typed-record.js.map