/**
 * Data-access helpers for the {@link Record} class.
 *
 * `RecordData` is the object returned by `Record.data`. It wraps a lazily
 * evaluated `stream()` function with convenience accessors that mirror the
 * Fetch `Response` API (`blob()`, `bytes()`, `json()`, `text()`).
 *
 * Extracted from `record.ts` so the convenience-method boilerplate lives in
 * its own module while the stream-resolution logic (which is tightly coupled
 * to `Record` internals) stays inside the `Record` class.
 *
 * @module
 */
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 { Stream } from '@enbox/common';
/**
 * Create a {@link RecordData} wrapper around a `stream` provider function.
 *
 * @param streamFn   - A function that returns a `Promise<ReadableStream>` for the record data.
 * @param dataFormat - The MIME type used when constructing Blobs.
 * @returns A {@link RecordData} object with convenience accessors.
 *
 * @beta
 */
export function createRecordData(streamFn, dataFormat) {
    const dataObj = {
        /**
         * Returns the data of the current record as a `Blob`.
         *
         * @returns A promise that resolves to a Blob containing the record's data.
         * @throws If the record data is not available or cannot be converted to a `Blob`.
         *
         * @beta
         */
        blob() {
            return __awaiter(this, void 0, void 0, function* () {
                return new Blob([yield Stream.consumeToBytes({ readableStream: yield this.stream() })], { type: dataFormat });
            });
        },
        /**
         * Returns the data of the current record as a `Uint8Array`.
         *
         * @returns A Promise that resolves to a `Uint8Array` containing the record's data bytes.
         * @throws If the record data is not available or cannot be converted to a byte array.
         *
         * @beta
         */
        bytes() {
            return __awaiter(this, void 0, void 0, function* () {
                return yield Stream.consumeToBytes({ readableStream: yield this.stream() });
            });
        },
        /**
         * Parses the data of the current record as JSON and returns it as a JavaScript object.
         *
         * @returns A Promise that resolves to a JavaScript object parsed from the record's JSON data.
         * @throws If the record data is not available, not in JSON format, or cannot be parsed.
         *
         * @beta
         */
        json() {
            return __awaiter(this, void 0, void 0, function* () {
                return yield Stream.consumeToJson({ readableStream: yield this.stream() });
            });
        },
        /**
         * Returns the data of the current record as a `string`.
         *
         * @returns A promise that resolves to a `string` containing the record's text data.
         * @throws If the record data is not available or cannot be converted to text.
         *
         * @beta
         */
        text() {
            return __awaiter(this, void 0, void 0, function* () {
                return yield Stream.consumeToText({ readableStream: yield this.stream() });
            });
        },
        /**
         * Provides a Web `ReadableStream` containing the record's data.
         *
         * Uses the standard Web Streams API for cross-platform compatibility across
         * browsers, Node.js, Bun, and Deno.
         *
         * @returns A promise that resolves to a Web `ReadableStream` of the record's data.
         * @throws If the record data is not available in-memory and cannot be fetched.
         *
         * @beta
         */
        stream: streamFn,
        /**
         * Attaches callbacks for the resolution and/or rejection of the `Promise` returned by
         * `stream()`.
         *
         * This method is a proxy to the `then` method of the `Promise` returned by `stream()`,
         * allowing for a seamless integration with promise-based workflows.
         * @param onFulfilled - A function to asynchronously execute when the `stream()` promise
         *                      becomes fulfilled.
         * @param onRejected - A function to asynchronously execute when the `stream()` promise
         *                     becomes rejected.
         * @returns A `Promise` for the completion of which ever callback is executed.
         */
        then(onFulfilled, onRejected) {
            return this.stream().then(onFulfilled, onRejected);
        },
        /**
         * Attaches a rejection handler callback to the `Promise` returned by the `stream()` method.
         * This method is a shorthand for `.then(undefined, onRejected)`, specifically designed for handling
         * rejection cases in the promise chain initiated by accessing the record's data. It ensures that
         * errors during data retrieval or processing can be caught and handled appropriately.
         *
         * @param onRejected - A function to asynchronously execute when the `stream()` promise
         *                     becomes rejected.
         * @returns A `Promise` that resolves to the value of the callback if it is called, or to its
         *          original fulfillment value if the promise is instead fulfilled.
         */
        catch(onRejected) {
            return this.stream().catch(onRejected);
        }
    };
    return dataObj;
}
//# sourceMappingURL=record-data.js.map