export const jwtMiddleware = (getJwt, getAttributes) => {
    return {
        transformParams: async ({ params }) => {
            const jwt = getJwt?.();
            const aiAttributes = getAttributes?.();
            // Extract retry metadata from provider options safely
            const billing = params.providerOptions?.["billing"];
            const retryMetadata = billing && typeof billing === "object" && billing !== null
                ? billing
                : undefined;
            // Filter out undefined values from existing headers
            const existingHeaders = params.headers || {};
            const headers = {};
            Object.entries(existingHeaders)
                .filter(([_, value]) => typeof value === "string")
                .forEach(([key, value]) => {
                headers[key] = value;
            });
            if (jwt) {
                headers.Authorization = `Bearer ${jwt}`;
                headers["x-superblocks-authorization"] = `Bearer ${jwt}`;
            }
            // Merge workflow-level attributes with session-level retry data
            const mergedAttributes = {
                ...aiAttributes,
                ...retryMetadata,
            };
            return {
                ...params,
                headers,
                attributes: mergedAttributes,
            };
        },
    };
};
// named so as to be distinct from the ai SDK's defaultSettingsMiddleware
// which has confusing behavior
export const prioritySettingsMiddleware = ({ settings, }) => {
    return {
        transformParams: async ({ params }) => ({
            ...params,
            ...settings,
            providerMetadata: {
                ...params.providerOptions,
                ...settings.providerOptions,
            },
        }),
    };
};
//# sourceMappingURL=utils.js.map