
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="384a4893-f924-54c0-97e4-ff8e4c01311d")}catch(e){}}();
import { APIFY_STORE_URL, MAX_INPUT_FIELDS_IN_TEXT_CARD } from '../const.js';
import { getCurrentPricingInfo, pricingInfoToSimplifiedString, pricingInfoToSimplifiedStructured, pricingInfoToString, pricingInfoToStructured, } from './pricing_info.js';
function getInputSchema(actor) {
    return 'inputSchema' in actor ? actor.inputSchema : undefined;
}
function inputFieldsToString(inputSchema) {
    var _a;
    const entries = Object.entries(inputSchema.properties);
    if (entries.length === 0)
        return null;
    const requiredSet = new Set((_a = inputSchema.required) !== null && _a !== void 0 ? _a : []);
    const shown = entries.slice(0, MAX_INPUT_FIELDS_IN_TEXT_CARD);
    const fields = shown
        .map(([name, prop]) => `${name}${requiredSet.has(name) ? '' : '?'}: ${Array.isArray(prop.type) ? prop.type.join('|') : prop.type}`)
        .join(', ');
    const overflow = entries.length - shown.length;
    const suffix = overflow > 0 ? ` ... (+${overflow} more)` : '';
    return `- **Input fields:** ${fields}${suffix}`;
}
// Helper function to format categories from uppercase with underscores to a proper case
function formatCategories(categories) {
    if (!categories)
        return [];
    return categories.map((category) => {
        const formatted = category
            .toLowerCase()
            .split('_')
            .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
            .join(' ');
        // Special case for MCP server, AI, and SEO tools
        return formatted.replace('Mcp Server', 'MCP Server').replace('Ai', 'AI').replace('Seo', 'SEO');
    });
}
/**
 * Resolves pricing info from either ActorStoreList (has currentPricingInfo)
 * or Actor (has pricingInfos array).
 */
function getActorPricingInfo(actor) {
    if ('currentPricingInfo' in actor) {
        return actor.currentPricingInfo;
    }
    return getCurrentPricingInfo(actor.pricingInfos || [], new Date());
}
export const DEFAULT_CARD_OPTIONS = {
    includeDescription: true,
    includeStats: true,
    includePricing: true,
    includeRating: true,
    includeMetadata: true,
};
/**
 * Extracts all actor data into a normalized intermediate form.
 * Both formatActorToActorCard and formatActorToStructuredCard consume this.
 */
function extractActorData(actor, options) {
    var _a, _b;
    const actorFullName = `${actor.username}/${actor.name}`;
    const actorUrl = `${APIFY_STORE_URL}/${actorFullName}`;
    const data = {
        actorFullName,
        actorUrl,
        title: actor.title,
        pictureUrl: actor.pictureUrl || undefined,
        description: options.includeDescription
            ? (actor.description || 'No description provided.')
            : '',
        pricingInfo: options.includePricing ? getActorPricingInfo(actor) : null,
        developer: { username: '', isOfficialApify: false, url: '' },
        categories: [],
        isDeprecated: false,
    };
    // Extract stats — each field checked independently to match original markdown behavior
    if (options.includeStats && 'stats' in actor) {
        const { stats } = actor;
        if ('totalUsers' in stats && 'totalUsers30Days' in stats) {
            data.stats = {
                totalUsers: stats.totalUsers,
                monthlyUsers: stats.totalUsers30Days,
            };
        }
        if ('publicActorRunStats30Days' in stats && stats.publicActorRunStats30Days) {
            const runStats = stats.publicActorRunStats30Days;
            if (runStats.TOTAL > 0) {
                (_a = data.stats) !== null && _a !== void 0 ? _a : (data.stats = { totalUsers: 0, monthlyUsers: 0 });
                data.stats.successRate = Number(((runStats.SUCCEEDED / runStats.TOTAL) * 100).toFixed(1));
            }
        }
        const bookmarkCount = ('bookmarkCount' in actor && actor.bookmarkCount)
            || ('bookmarkCount' in stats && stats.bookmarkCount);
        if (bookmarkCount) {
            (_b = data.stats) !== null && _b !== void 0 ? _b : (data.stats = { totalUsers: 0, monthlyUsers: 0 });
            data.stats.bookmarks = Number(bookmarkCount);
        }
    }
    // Extract rating — only actorReviewRating is required (count is optional)
    if (options.includeRating) {
        const actorReviewRating = ('actorReviewRating' in actor && actor.actorReviewRating)
            || ('stats' in actor && actor.stats && 'actorReviewRating' in actor.stats && actor.stats.actorReviewRating);
        if (actorReviewRating) {
            const actorReviewCount = ('actorReviewCount' in actor && actor.actorReviewCount)
                || ('stats' in actor && actor.stats && 'actorReviewCount' in actor.stats && actor.stats.actorReviewCount);
            data.rating = {
                average: Number(Number(actorReviewRating).toFixed(2)),
                count: actorReviewCount ? Number(actorReviewCount) : 0,
            };
        }
    }
    // Extract metadata
    if (options.includeMetadata) {
        data.developer = {
            username: actor.username,
            isOfficialApify: actor.username === 'apify',
            url: `${APIFY_STORE_URL}/${actor.username}`,
        };
        data.categories = formatCategories('categories' in actor ? actor.categories : undefined);
        if ('modifiedAt' in actor && actor.modifiedAt) {
            data.modifiedAt = actor.modifiedAt.toISOString();
        }
        data.isDeprecated = ('isDeprecated' in actor && actor.isDeprecated) || false;
    }
    return data;
}
/**
 * Formats Actor details into a markdown Actor card.
 * Used in both default (text-only) and OpenAI (widget) modes as the LLM-facing text content.
 */
export function formatActorToActorCard(actor, options = DEFAULT_CARD_OPTIONS) {
    var _a;
    const data = extractActorData(actor, options);
    const userTier = (_a = options.userTier) !== null && _a !== void 0 ? _a : 'FREE';
    const markdownLines = [
        `## [${data.title}](${data.actorUrl}) (\`${data.actorFullName}\`)`,
        `- **URL:** ${data.actorUrl}`,
    ];
    if (options.includeDescription) {
        markdownLines.push(`- **Description:** ${data.description}`);
    }
    if (options.includePricing) {
        const pricingString = options.simplifyPricingForUserTier
            ? pricingInfoToSimplifiedString(data.pricingInfo, userTier)
            : pricingInfoToString(data.pricingInfo);
        markdownLines.push(`- **[Pricing](${data.actorUrl}/pricing):** ${pricingString}`);
    }
    if (data.stats) {
        const statsParts = [
            `${data.stats.totalUsers.toLocaleString()} total users, ${data.stats.monthlyUsers.toLocaleString()} monthly users`,
        ];
        if (data.stats.successRate !== undefined) {
            statsParts.push(`Runs succeeded: ${data.stats.successRate}%`);
        }
        if (data.stats.bookmarks) {
            statsParts.push(`${data.stats.bookmarks} bookmarks`);
        }
        markdownLines.push(`- **Stats:** ${statsParts.join(', ')}`);
    }
    if (data.rating) {
        markdownLines.push(`- **Rating:** ${data.rating.average.toFixed(2)} out of 5`);
    }
    if (options.includeMetadata) {
        markdownLines.push(`- **Developed by:** [${data.developer.username}](${data.developer.url}) ${data.developer.isOfficialApify ? '(Apify)' : '(community)'}`);
        markdownLines.push(`- **Categories:** ${data.categories.length ? data.categories.join(', ') : 'Uncategorized'}`);
        if (data.modifiedAt) {
            markdownLines.push(`- **Last modified:** ${data.modifiedAt}`);
        }
        if (data.isDeprecated) {
            markdownLines.push('\n>This Actor is deprecated and may not be maintained anymore.');
        }
    }
    const inputSchema = getInputSchema(actor);
    if (inputSchema) {
        const line = inputFieldsToString(inputSchema);
        if (line)
            markdownLines.push(line);
    }
    return markdownLines.join('\n');
}
/**
 * Extracts structured Actor data for programmatic use.
 * Used in both default (text-only) and OpenAI (widget) modes as the structured content in MCP responses.
 */
export function formatActorToStructuredCard(actor, options = DEFAULT_CARD_OPTIONS) {
    var _a;
    const data = extractActorData(actor, options);
    const userTier = (_a = options.userTier) !== null && _a !== void 0 ? _a : 'FREE';
    const pricing = options.simplifyPricingForUserTier
        ? pricingInfoToSimplifiedStructured(data.pricingInfo, userTier)
        : pricingInfoToStructured(data.pricingInfo, userTier);
    return {
        title: data.title,
        url: data.actorUrl,
        id: actor.id,
        fullName: data.actorFullName,
        pictureUrl: data.pictureUrl,
        developer: data.developer,
        description: data.description,
        categories: data.categories,
        pricing,
        stats: data.stats,
        rating: data.rating,
        modifiedAt: data.modifiedAt,
        isDeprecated: data.isDeprecated,
        inputFields: getInputSchema(actor),
    };
}
/**
 * Formats Actor for widget UI components.
 * Used only in OpenAI (widget) mode — search results and Actor details widgets.
 *
 * Always uses simplified tier-aware pricing so the widget's top-level
 * `pricePerUnit` / `events[0].priceUsd` (which is what the widget UI renders)
 * matches the tier-filtered prices shown in the LLM text and structured output.
 */
export function formatActorForWidget(actor, userTier) {
    var _a, _b, _c;
    const fullName = `${actor.username}/${actor.name}`;
    return {
        id: actor.id,
        name: actor.name,
        username: actor.username,
        fullName,
        title: actor.title || actor.name,
        description: actor.description || 'No description available',
        pictureUrl: actor.pictureUrl || '',
        stats: {
            actorReviewRating: ('actorReviewRating' in actor && actor.actorReviewRating)
                || ((_a = actor.stats) === null || _a === void 0 ? void 0 : _a.actorReviewRating) || 0,
            actorReviewCount: ('actorReviewCount' in actor && actor.actorReviewCount)
                || ((_b = actor.stats) === null || _b === void 0 ? void 0 : _b.actorReviewCount) || 0,
            totalUsers: ((_c = actor.stats) === null || _c === void 0 ? void 0 : _c.totalUsers) || 0,
        },
        url: `${APIFY_STORE_URL}/${fullName}`,
        currentPricingInfo: pricingInfoToSimplifiedStructured(getActorPricingInfo(actor), userTier),
    };
}
//# sourceMappingURL=actor_card.js.map
//# debugId=384a4893-f924-54c0-97e4-ff8e4c01311d
