"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExampleFiles = getExampleFiles;
exports.resetExampleFilesCache = resetExampleFilesCache;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const emit_instance_ai_1 = require("./codegen/emit-instance-ai");
const examples_zip_1 = require("./examples-zip");
const EXAMPLES_DIR = path.resolve(__dirname, '..', 'examples');
const MANIFEST_PATH = path.join(EXAMPLES_DIR, 'manifest.json');
const NODES_INLINE_LIMIT = 5;
const INDEX_NODE_SEPARATOR = ',';
let cached = null;
function getExampleFiles() {
    if (cached !== null)
        return cached;
    cached = loadFromDisk();
    return cached;
}
function resetExampleFilesCache() {
    cached = null;
}
function loadFromDisk() {
    if (!fs.existsSync(MANIFEST_PATH))
        return { files: [], indexTxt: '' };
    (0, examples_zip_1.ensureExtracted)();
    const manifest = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf-8'));
    const entries = (manifest.workflows ?? [])
        .filter((e) => e.success && !e.skip)
        .sort((a, b) => b.score - a.score);
    const files = [];
    const indexLines = [];
    for (const entry of entries) {
        const wfPath = path.join(examples_zip_1.WORKFLOWS_CACHE_DIR, `${entry.slug}.json`);
        if (!fs.existsSync(wfPath))
            continue;
        const wf = JSON.parse(fs.readFileSync(wfPath, 'utf-8'));
        const header = buildJsdocHeader(entry);
        const code = (0, emit_instance_ai_1.emitInstanceAi)(wf, { jsdocHeader: header });
        files.push({ filename: `${entry.slug}.ts`, content: code });
        indexLines.push(buildIndexLine(entry));
    }
    const indexTxt = indexLines.join('\n') + (indexLines.length > 0 ? '\n' : '');
    return { files, indexTxt };
}
function buildJsdocHeader(entry) {
    return [
        '/**',
        ' * @template',
        ` * @name ${entry.name}`,
        ` * @nodes ${entry.nodes.join(', ')}`,
        ` * @tags ${entry.tags.join(', ')}`,
        ` * @source ${entry.source}`,
        ` * @author ${entry.author}`,
        ' */',
    ].join('\n');
}
function buildIndexLine(entry) {
    const truncatedNodes = truncateNodes(entry.nodes);
    return [
        `${entry.slug}.ts`,
        entry.name,
        truncatedNodes,
        entry.tags.join(','),
        `n8n:${entry.id}`,
    ].join(' | ');
}
function truncateNodes(nodes) {
    if (nodes.length <= NODES_INLINE_LIMIT)
        return nodes.join(INDEX_NODE_SEPARATOR);
    const head = nodes.slice(0, NODES_INLINE_LIMIT).join(INDEX_NODE_SEPARATOR);
    const remaining = nodes.length - NODES_INLINE_LIMIT;
    return `${head} +${remaining} more`;
}
//# sourceMappingURL=examples-loader.js.map