"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    Settings: function() {
        return Settings;
    },
    embedModelFromSettingsOrContext: function() {
        return embedModelFromSettingsOrContext;
    },
    llmFromSettingsOrContext: function() {
        return llmFromSettingsOrContext;
    },
    nodeParserFromSettingsOrContext: function() {
        return nodeParserFromSettingsOrContext;
    },
    promptHelperFromSettingsOrContext: function() {
        return promptHelperFromSettingsOrContext;
    }
});
const _openai = require("./llm/openai.js");
const _PromptHelper = require("./PromptHelper.js");
const _SimpleNodeParser = require("./nodeParsers/SimpleNodeParser.js");
const _env = require("@llamaindex/env");
const _CallbackManager = require("./internal/settings/CallbackManager.js");
const _EmbedModel = require("./internal/settings/EmbedModel.js");
const _chunksize = require("./internal/settings/chunk-size.js");
/**
 * @internal
 */ class GlobalSettings {
    #prompt = {};
    #llm = null;
    #promptHelper = null;
    #nodeParser = null;
    #chunkOverlap;
    #llmAsyncLocalStorage = new _env.AsyncLocalStorage();
    #promptHelperAsyncLocalStorage = new _env.AsyncLocalStorage();
    #nodeParserAsyncLocalStorage = new _env.AsyncLocalStorage();
    #chunkOverlapAsyncLocalStorage = new _env.AsyncLocalStorage();
    #promptAsyncLocalStorage = new _env.AsyncLocalStorage();
    get debug() {
        const debug = (0, _env.getEnv)("DEBUG");
        return Boolean(debug) && debug?.includes("llamaindex") || debug === "*" || debug === "true";
    }
    get llm() {
        if (this.#llm === null) {
            this.#llm = new _openai.OpenAI();
        }
        return this.#llmAsyncLocalStorage.getStore() ?? this.#llm;
    }
    set llm(llm) {
        this.#llm = llm;
    }
    withLLM(llm, fn) {
        return this.#llmAsyncLocalStorage.run(llm, fn);
    }
    get promptHelper() {
        if (this.#promptHelper === null) {
            this.#promptHelper = new _PromptHelper.PromptHelper();
        }
        return this.#promptHelperAsyncLocalStorage.getStore() ?? this.#promptHelper;
    }
    set promptHelper(promptHelper) {
        this.#promptHelper = promptHelper;
    }
    withPromptHelper(promptHelper, fn) {
        return this.#promptHelperAsyncLocalStorage.run(promptHelper, fn);
    }
    get embedModel() {
        return (0, _EmbedModel.getEmbeddedModel)();
    }
    set embedModel(embedModel) {
        (0, _EmbedModel.setEmbeddedModel)(embedModel);
    }
    withEmbedModel(embedModel, fn) {
        return (0, _EmbedModel.withEmbeddedModel)(embedModel, fn);
    }
    get nodeParser() {
        if (this.#nodeParser === null) {
            this.#nodeParser = new _SimpleNodeParser.SimpleNodeParser({
                chunkSize: this.chunkSize,
                chunkOverlap: this.chunkOverlap
            });
        }
        return this.#nodeParserAsyncLocalStorage.getStore() ?? this.#nodeParser;
    }
    set nodeParser(nodeParser) {
        this.#nodeParser = nodeParser;
    }
    withNodeParser(nodeParser, fn) {
        return this.#nodeParserAsyncLocalStorage.run(nodeParser, fn);
    }
    get callbackManager() {
        return (0, _CallbackManager.getCallbackManager)();
    }
    set callbackManager(callbackManager) {
        (0, _CallbackManager.setCallbackManager)(callbackManager);
    }
    withCallbackManager(callbackManager, fn) {
        return (0, _CallbackManager.withCallbackManager)(callbackManager, fn);
    }
    set chunkSize(chunkSize) {
        (0, _chunksize.setChunkSize)(chunkSize);
    }
    get chunkSize() {
        return (0, _chunksize.getChunkSize)();
    }
    withChunkSize(chunkSize, fn) {
        return (0, _chunksize.withChunkSize)(chunkSize, fn);
    }
    get chunkOverlap() {
        return this.#chunkOverlapAsyncLocalStorage.getStore() ?? this.#chunkOverlap;
    }
    set chunkOverlap(chunkOverlap) {
        this.#chunkOverlap = chunkOverlap;
    }
    withChunkOverlap(chunkOverlap, fn) {
        return this.#chunkOverlapAsyncLocalStorage.run(chunkOverlap, fn);
    }
    get prompt() {
        return this.#promptAsyncLocalStorage.getStore() ?? this.#prompt;
    }
    set prompt(prompt) {
        this.#prompt = prompt;
    }
    withPrompt(prompt, fn) {
        return this.#promptAsyncLocalStorage.run(prompt, fn);
    }
}
const llmFromSettingsOrContext = (serviceContext)=>{
    if (serviceContext?.llm) {
        return serviceContext.llm;
    }
    return Settings.llm;
};
const nodeParserFromSettingsOrContext = (serviceContext)=>{
    if (serviceContext?.nodeParser) {
        return serviceContext.nodeParser;
    }
    return Settings.nodeParser;
};
const embedModelFromSettingsOrContext = (serviceContext)=>{
    if (serviceContext?.embedModel) {
        return serviceContext.embedModel;
    }
    return Settings.embedModel;
};
const promptHelperFromSettingsOrContext = (serviceContext)=>{
    if (serviceContext?.promptHelper) {
        return serviceContext.promptHelper;
    }
    return Settings.promptHelper;
};
const Settings = new GlobalSettings();
