import { resolveAttachmentFileName, } from "@superblocksteam/library-shared/types";
import { detectAttachmentArchiveType, } from "@superblocksteam/shared";
const ATTACHMENT_URL_BASE = "https://localhost/";
export const isUploadedAttachment = (attachment) => {
    const candidate = attachment;
    return (candidate.type === "uploaded" &&
        typeof candidate.url === "string" &&
        typeof candidate.mediaType === "string");
};
/**
 * Canonicalize stable attachment URLs before they enter lease resolution.
 *
 * Server-uploaded attachments commonly use relative `/api/v1/files/content/:id`
 * URLs. We intentionally normalize those against a fixed localhost origin so the
 * same canonical string is used for:
 * - attachment metadata sent over the lease RPC
 * - provider-side cache keys
 * - URL-backed file/image content parts
 *
 * The origin is not semantically important; the server resolves by attachment ID
 * encoded in the pathname.
 */
export function toAttachmentUrl(url) {
    try {
        return new URL(url);
    }
    catch {
        return new URL(url, ATTACHMENT_URL_BASE);
    }
}
export function getCanonicalAttachmentUrl(url) {
    if (url instanceof URL) {
        return url.href;
    }
    return toAttachmentUrl(url).href;
}
export function getResolvableAttachmentUrl(value) {
    if (value instanceof URL) {
        return getCanonicalAttachmentUrl(value);
    }
    const trimmed = value.trim();
    if (trimmed.length === 0 || trimmed.startsWith("data:")) {
        return undefined;
    }
    try {
        const parsed = new URL(trimmed);
        if (parsed.protocol === "http:" || parsed.protocol === "https:") {
            return parsed.href;
        }
    }
    catch {
        if (trimmed.startsWith("/")) {
            return getCanonicalAttachmentUrl(trimmed);
        }
        return undefined;
    }
    return undefined;
}
export function getAttachmentLeaseMetadata(attachment) {
    return {
        url: getCanonicalAttachmentUrl(attachment.url),
        mediaType: attachment.mediaType,
        fileName: resolveAttachmentFileName(attachment),
        ...(attachment.storageKey ? { storageKey: attachment.storageKey } : {}),
        ...(attachment.scopeType ? { scopeType: attachment.scopeType } : {}),
        ...(attachment.applicationId
            ? { applicationId: attachment.applicationId }
            : {}),
    };
}
export function toUploadedAttachmentContentPart(attachment) {
    const url = toAttachmentUrl(attachment.url);
    const superblocksAttachmentMetadata = getAttachmentLeaseMetadata(attachment);
    if (attachment.mediaType.startsWith("image/")) {
        return {
            type: "image",
            image: url,
            superblocksAttachmentMetadata,
        };
    }
    return {
        type: "file",
        data: url,
        mediaType: attachment.mediaType,
        filename: resolveAttachmentFileName(attachment),
        superblocksAttachmentMetadata,
    };
}
export function toUploadedAttachmentReferenceTextPart(attachment, options = { downloadEnabled: true }) {
    const fileName = resolveAttachmentFileName(attachment);
    const isArchive = detectAttachmentArchiveType({
        fileName,
        mimeType: attachment.mediaType,
    }) !== null;
    if (!isArchive) {
        return {
            type: "text",
            text: `<attachment_reference>
fileName: ${fileName}
mediaType: ${attachment.mediaType}
note: The user attached an unsupported file. This cannot be read.
</attachment_reference>`,
        };
    }
    const { downloadEnabled } = options;
    const note = downloadEnabled
        ? "This attachment is reference-only. Follow the attachment_reference workflow defined in your system instructions."
        : "Attachment downloads are disabled in this workspace; treat this as a pointer only and do not attempt to fetch it.";
    return {
        type: "text",
        text: `<attachment_reference>
fileName: ${fileName}
mediaType: ${attachment.mediaType}
url: ${getCanonicalAttachmentUrl(attachment.url)}
storageKey: ${attachment.storageKey ?? "unknown"}
note: ${note}
</attachment_reference>`,
    };
}
//# sourceMappingURL=uploaded-content-part.js.map