import PatchError from "../errors/PatchError.js";
import ContentAttachmentMime from "../graph/ContentAttachmentMime.js";
import ContentAttachmentOid from "../graph/ContentAttachmentOid.js";
import ContentAttachmentPayload from "../graph/ContentAttachmentPayload.js";
import ContentAttachmentSize from "../graph/ContentAttachmentSize.js";
import { isPropValue } from "../types/PropValue.js";
import { isStreamingInput, normalizeToAsyncIterable } from "../utils/streamUtils.js";
import { normalizeContentMetadata } from "./PatchBuilderValidation.js";
/** Validates public patch property values before intent construction. */
export function requirePatchPropertyValue(value) {
    if (isPropValue(value)) {
        return value;
    }
    throw new PatchError('Property value must be property-compatible data', {
        code: 'E_PATCH_INVALID_PROPERTY_VALUE',
    });
}
export async function storeContentAttachmentPayload(options) {
    if (isBufferedContent(options.content)) {
        return await storeBufferedContentAttachmentPayload({
            blobStorage: options.blobStorage,
            content: options.content,
            metadata: options.metadata,
            slug: options.slug,
        });
    }
    return await storeStreamingContentAttachmentPayload(options);
}
async function storeBufferedContentAttachmentPayload(options) {
    const normalizedMeta = normalizeContentMetadata(options.content, options.metadata);
    const storageOptions = contentStorageOptions(options.slug, normalizedMeta.mime, normalizedMeta.size);
    const oid = await options.blobStorage.store(options.content, storageOptions);
    return contentAttachmentPayload(oid, normalizedMeta.mime, normalizedMeta.size);
}
async function storeStreamingContentAttachmentPayload(options) {
    const metadata = streamingContentMetadata(options.metadata);
    const storageOptions = contentStorageOptions(options.slug, contentAttachmentMimeString(metadata.mime), contentAttachmentSizeNumber(metadata.size));
    const oid = await options.blobStorage.storeStream(normalizeToAsyncIterable(options.content), storageOptions);
    return new ContentAttachmentPayload({
        oid: new ContentAttachmentOid(oid),
        mime: metadata.mime,
        size: metadata.size,
    });
}
function streamingContentMetadata(metadata) {
    return {
        mime: contentAttachmentMime(metadata?.mime ?? null),
        size: contentAttachmentSize(metadata?.size ?? null),
    };
}
function contentAttachmentMimeString(mime) {
    return mime === null ? null : mime.toString();
}
function contentAttachmentSizeNumber(size) {
    return size === null ? null : size.toNumber();
}
function isBufferedContent(content) {
    return !isStreamingInput(content);
}
function contentAttachmentPayload(oid, mime, size) {
    return new ContentAttachmentPayload({
        oid: new ContentAttachmentOid(oid),
        mime: contentAttachmentMime(mime),
        size: contentAttachmentSize(size),
    });
}
function contentAttachmentMime(mime) {
    return mime === null ? null : new ContentAttachmentMime(mime);
}
function contentAttachmentSize(size) {
    return size === null ? null : new ContentAttachmentSize(size);
}
function contentStorageOptions(slug, mime, size) {
    return { slug, mime, size };
}
