
!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]="64945933-ee90-51da-819d-af59d06b55a5")}catch(e){}}();
import { ACTOR_README_MAX_LENGTH } from '../const.js';
/**
 * Get Actor input schema by Actor name.
 * First, fetch the Actor details to get the default build tag and buildId.
 * Then, fetch the build details and return actorName, description, and input schema.
 * @param {string} actorIdOrName - Actor ID or Actor full name.
 * @param {ApifyClient} apifyClient - The Apify client instance.
 * @param {number} limit - Truncate the README to this limit.
 * @returns {Promise<ActorDefinitionWithInfo | null>} - The Actor definition with info or null if not found.
 */
export async function getActorDefinition(actorIdOrName, apifyClient, limit = ACTOR_README_MAX_LENGTH) {
    const actorClient = apifyClient.actor(actorIdOrName);
    try {
        // Fetch Actor details
        const actor = await actorClient.get();
        if (!actor) {
            return null;
        }
        const defaultBuildClient = await actorClient.defaultBuild();
        const buildDetails = await defaultBuildClient.get();
        if (buildDetails === null || buildDetails === void 0 ? void 0 : buildDetails.actorDefinition) {
            const actorDefinitions = buildDetails === null || buildDetails === void 0 ? void 0 : buildDetails.actorDefinition;
            // We set actorDefinition ID to Actor ID
            actorDefinitions.id = actor.id;
            actorDefinitions.readme = truncateActorReadme(actorDefinitions.readme || '', limit);
            actorDefinitions.readmeSummary = actor.readmeSummary;
            actorDefinitions.description = actor.description || '';
            actorDefinitions.actorFullName = `${actor.username}/${actor.name}`;
            actorDefinitions.defaultRunOptions = actor.defaultRunOptions;
            // Pass pictureUrl from actor object (untyped property but present in API response)
            actorDefinitions.pictureUrl = actor.pictureUrl;
            return {
                definition: pruneActorDefinition(actorDefinitions),
                info: actor,
            };
        }
        return null;
    }
    catch (error) {
        // Check if it's a "not found" error (404 or 400 status codes)
        const isNotFound = typeof error === 'object'
            && error !== null
            && 'statusCode' in error
            && (error.statusCode === 404 || error.statusCode === 400);
        if (isNotFound) {
            // Return null for not found - caller will log appropriately
            return null;
        }
        // For server errors, throw the original error (preserve error type)
        // Caller should catch and log
        throw error;
    }
}
function pruneActorDefinition(response) {
    return {
        id: response.id,
        actorFullName: response.actorFullName || '',
        buildTag: (response === null || response === void 0 ? void 0 : response.buildTag) || '',
        readme: (response === null || response === void 0 ? void 0 : response.readme) || '',
        readmeSummary: response.readmeSummary,
        input: (response === null || response === void 0 ? void 0 : response.input) && 'type' in response.input && 'properties' in response.input
            ? {
                ...response.input,
                type: response.input.type,
                properties: response.input.properties,
            }
            : undefined,
        description: response.description,
        defaultRunOptions: response.defaultRunOptions,
        webServerMcpPath: 'webServerMcpPath' in response ? response.webServerMcpPath : undefined,
        pictureUrl: 'pictureUrl' in response ? response.pictureUrl : undefined,
    };
}
/** Prune Actor README if it is too long
 * If the README is too long
 * - We keep the README as it is up to the limit.
 * - After the limit, we keep heading only
 * - We add a note that the README was truncated because it was too long.
 */
function truncateActorReadme(readme, limit = ACTOR_README_MAX_LENGTH) {
    if (readme.length <= limit) {
        return readme;
    }
    const readmeFirst = readme.slice(0, limit);
    const readmeRest = readme.slice(limit);
    const lines = readmeRest.split('\n');
    const prunedReadme = lines.filter((line) => line.startsWith('#'));
    return `${readmeFirst}\n\nREADME was truncated because it was too long. Remaining headers:\n${prunedReadme.join(', ')}`;
}
//# sourceMappingURL=build.js.map
//# debugId=64945933-ee90-51da-819d-af59d06b55a5
