"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.checkOpenaiPackage = void 0;
const checkOpenaiPackage = async () => {
    try {
        const packageName = 'openai';
        const versionRange = '>=6.21.0 || <7.0.0'; // Required version from package.json
        // Try to require the openai package
        let openaiPkg;
        try {
            openaiPkg = await Promise.resolve(`${packageName}`).then(s => __importStar(require(s)));
        }
        catch (_a) {
            throw new Error(`The package ${packageName} is not installed. Please install it using: npm install ${packageName}@${versionRange}`);
        }
        // Try to get the package version
        let pkgVersion;
        try {
            pkgVersion = (await Promise.resolve(`${`${packageName}/version`}`).then(s => __importStar(require(s)))).VERSION;
        }
        catch (_b) {
            // If we can't get the version from package.json, try to get it from the package itself
            pkgVersion = openaiPkg.version || 'unknown';
        }
        // Check if the version satisfies the requirement
        const semver = await Promise.resolve().then(() => __importStar(require('semver')));
        if (!semver.satisfies(pkgVersion, versionRange)) {
            throw new Error(`The package ${packageName}@${pkgVersion} is installed, but version ${versionRange} is required.`);
        }
    }
    catch (err) {
        throw new Error(`The required dependency "openai" was not found or is not supported. Please install a compatible version: ^6.21.0\n${err}`);
    }
};
exports.checkOpenaiPackage = checkOpenaiPackage;
