
!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]="bac114ad-5ba3-58b1-993f-e3923124db23")}catch(e){}}();
import log from '@apify/log';
import { SKYFIRE_README_CONTENT } from '../const.js';
import { RESOURCE_MIME_TYPE } from './widgets.js';
export function createResourceService(options) {
    const { skyfireMode, mode = 'default', getAvailableWidgets } = options;
    const listResources = async () => {
        const resources = [];
        if (skyfireMode) {
            resources.push({
                uri: 'file://readme.md',
                name: 'readme',
                description: 'Apify MCP Server usage guide. Read this to understand how to use the server, '
                    + 'especially in Skyfire mode before interacting with it.',
                mimeType: 'text/markdown',
            });
        }
        if (mode === 'openai') {
            for (const widget of getAvailableWidgets().values()) {
                if (!widget.exists) {
                    continue;
                }
                resources.push({
                    uri: widget.uri,
                    name: widget.name,
                    description: widget.description,
                    mimeType: RESOURCE_MIME_TYPE,
                    _meta: widget.meta,
                });
            }
        }
        return { resources };
    };
    const readResource = async (uri) => {
        if (skyfireMode && uri === 'file://readme.md') {
            return {
                contents: [{
                        uri: 'file://readme.md',
                        mimeType: 'text/markdown',
                        text: SKYFIRE_README_CONTENT,
                    }],
            };
        }
        if (mode === 'openai' && uri.startsWith('ui://widget/')) {
            const widget = getAvailableWidgets().get(uri);
            if (!widget || !widget.exists) {
                return {
                    contents: [{
                            uri,
                            mimeType: 'text/plain',
                            text: `Widget ${uri} is not available. ${!widget ? 'Not found in registry.' : `File not found at ${widget.jsPath}`}`,
                        }],
                };
            }
            try {
                log.debug('Reading widget file', { uri, jsPath: widget.jsPath });
                const fs = await import('node:fs');
                const widgetJs = fs.readFileSync(widget.jsPath, 'utf-8');
                const widgetHtml = `<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>${widget.title}</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module">${widgetJs}</script>
  </body>
</html>`;
                const widgetContent = {
                    uri,
                    mimeType: RESOURCE_MIME_TYPE,
                    text: widgetHtml,
                    html: widgetHtml,
                    _meta: widget.meta,
                };
                return {
                    contents: [widgetContent],
                };
            }
            catch (error) {
                const errorMessage = error instanceof Error ? error.message : String(error);
                return {
                    contents: [{
                            uri,
                            mimeType: 'text/plain',
                            text: `Failed to load widget: ${errorMessage}`,
                        }],
                };
            }
        }
        return {
            contents: [{
                    uri,
                    mimeType: 'text/plain',
                    text: `Resource ${uri} not found`,
                }],
        };
    };
    const listResourceTemplates = async () => ({
        resourceTemplates: [],
    });
    return {
        listResources,
        readResource,
        listResourceTemplates,
    };
}
//# sourceMappingURL=resource_service.js.map
//# debugId=bac114ad-5ba3-58b1-993f-e3923124db23
