"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOWS_CACHE_DIR = void 0;
exports.zipExists = zipExists;
exports.needsExtraction = needsExtraction;
exports.extractFromZip = extractFromZip;
exports.ensureExtracted = ensureExtracted;
const adm_zip_1 = __importDefault(require("adm-zip"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const EXAMPLES_DIR = path.resolve(__dirname, '..', 'examples');
const ZIP_PATH = path.join(EXAMPLES_DIR, 'templates.zip');
const MANIFEST_PATH = path.join(EXAMPLES_DIR, 'manifest.json');
function sdkVersion() {
    try {
        const pkgPath = path.resolve(__dirname, '..', 'package.json');
        return (JSON.parse(fs.readFileSync(pkgPath, 'utf-8')).version ??
            'unversioned');
    }
    catch {
        return 'unversioned';
    }
}
exports.WORKFLOWS_CACHE_DIR = path.join(os.tmpdir(), 'n8n-workflow-sdk', sdkVersion(), 'workflows');
function zipExists() {
    return fs.existsSync(ZIP_PATH);
}
function needsExtraction() {
    if (!fs.existsSync(ZIP_PATH))
        return false;
    if (!fs.existsSync(MANIFEST_PATH))
        return false;
    const manifest = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf-8'));
    for (const entry of manifest.workflows ?? []) {
        if (!entry.success || entry.skip)
            continue;
        const filePath = path.join(exports.WORKFLOWS_CACHE_DIR, `${entry.slug}.json`);
        if (!fs.existsSync(filePath))
            return true;
    }
    return false;
}
function extractFromZip() {
    if (!fs.existsSync(ZIP_PATH)) {
        throw new Error(`Examples zip not found: ${ZIP_PATH}`);
    }
    if (!fs.existsSync(exports.WORKFLOWS_CACHE_DIR)) {
        fs.mkdirSync(exports.WORKFLOWS_CACHE_DIR, { recursive: true });
    }
    const zip = new adm_zip_1.default(ZIP_PATH);
    for (const entry of zip.getEntries()) {
        if (entry.isDirectory)
            continue;
        zip.extractEntryTo(entry, exports.WORKFLOWS_CACHE_DIR, false, true);
    }
}
function ensureExtracted() {
    if (needsExtraction()) {
        extractFromZip();
    }
}
//# sourceMappingURL=examples-zip.js.map