/**
 * NOTE: Added reference types here to avoid a `pnpm` bug during build.
 * https://github.com/enboxorg/enbox/pull/507
 */
/// <reference types="@enbox/dwn-sdk-js" />
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());
    });
};
import { ReadOnlyRecord } from './read-only-record.js';
// ---------------------------------------------------------------------------
// DwnReaderApi
// ---------------------------------------------------------------------------
/**
 * A read-only API for querying public data on remote DWNs without any identity or signing keys.
 *
 * This class mirrors the shape of {@link DwnApi}'s `records` and `protocols`
 * namespaces, but restricts to read-path operations and requires a `from` DID
 * on every call (remote-only). All messages are unsigned, so only published
 * records and protocols are accessible.
 *
 * Obtain an instance via {@link Enbox.anonymous | `Enbox.anonymous()`}.
 *
 * @example
 * ```ts
 * const { dwn } = Enbox.anonymous();
 *
 * const { records } = await dwn.records.query({
 *   from: 'did:dht:alice...',
 *   filter: { protocol: 'https://social.example/posts', protocolPath: 'post' },
 * });
 *
 * for (const record of records) {
 *   console.log(record.id, await record.data.text());
 * }
 * ```
 *
 * @beta
 */
export class DwnReaderApi {
    constructor(anonymousDwn) {
        this._anonymousDwn = anonymousDwn;
    }
    /**
     * API to interact with public DWN records (query, read, count).
     */
    get records() {
        return {
            /**
             * Query public records from a remote DWN.
             * Only published records are returned.
             */
            query: (request) => __awaiter(this, void 0, void 0, function* () {
                const reply = yield this._anonymousDwn.recordsQuery(request.from, {
                    filter: request.filter,
                    dateSort: request.dateSort,
                    pagination: request.pagination,
                });
                const { entries = [], status, cursor } = reply;
                const records = entries.map((entry) => new ReadOnlyRecord({
                    rawMessage: entry,
                    initialWrite: entry.initialWrite,
                    encodedData: entry.encodedData,
                    remoteOrigin: request.from,
                    anonymousDwn: this._anonymousDwn,
                }));
                return { records, status, cursor };
            }),
            /**
             * Read a specific public record from a remote DWN.
             * Succeeds for published records and protocol records with `{ who: 'anyone', can: ['read'] }`.
             */
            read: (request) => __awaiter(this, void 0, void 0, function* () {
                const reply = yield this._anonymousDwn.recordsRead(request.from, {
                    filter: request.filter,
                });
                const { entry, status } = reply;
                let record;
                if (200 <= status.code && status.code <= 299 && (entry === null || entry === void 0 ? void 0 : entry.recordsWrite)) {
                    record = new ReadOnlyRecord({
                        rawMessage: entry.recordsWrite,
                        initialWrite: entry.initialWrite,
                        data: entry.data,
                        remoteOrigin: request.from,
                        anonymousDwn: this._anonymousDwn,
                    });
                }
                return { record, status };
            }),
            /**
             * Count public records on a remote DWN.
             * Only published records are counted.
             */
            count: (request) => __awaiter(this, void 0, void 0, function* () {
                const reply = yield this._anonymousDwn.recordsCount(request.from, {
                    filter: request.filter,
                });
                const { count, status } = reply;
                return { count, status };
            }),
        };
    }
    /**
     * API to query published protocol definitions from remote DWNs.
     */
    get protocols() {
        return {
            /**
             * Query published protocols from a remote DWN.
             * Only protocol definitions with `published: true` are returned.
             */
            query: (request) => __awaiter(this, void 0, void 0, function* () {
                const reply = yield this._anonymousDwn.protocolsQuery(request.from, {
                    filter: request.filter,
                });
                const { entries = [], status } = reply;
                const protocols = entries.map((entry) => entry.descriptor.definition);
                return { protocols, status };
            }),
        };
    }
}
//# sourceMappingURL=dwn-reader-api.js.map