"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createScriptBasedProviderFactory = createScriptBasedProviderFactory;
const file_1 = require("../util/file");
const assertCustomAIExecutionPolicy_1 = require("../util/customAIApplication/assertCustomAIExecutionPolicy");
/**
 * Creates a factory for script-based providers (exec, golang, python)
 * @param prefix The provider prefix to match (e.g. 'exec:')
 * @param fileExtension Optional file extension to match when using file:// format
 * @param providerConstructor The provider class constructor
 * @returns A ProviderFactory for the specified script-based provider
 */
function createScriptBasedProviderFactory(prefix, fileExtension, providerConstructor) {
    return {
        test: (providerPath) => {
            // Test if path matches the prefix or file extension pattern
            if (providerPath.startsWith(`${prefix}:`)) {
                return true;
            }
            // Check file extension only if fileExtension is provided
            if (fileExtension && providerPath.startsWith('file://')) {
                return (providerPath.endsWith(`.${fileExtension}`) || providerPath.includes(`.${fileExtension}:`));
            }
            return false;
        },
        create: async (providerPath, providerOptions, context) => {
            // Extract the script path from the provider path
            let scriptPath;
            if (providerPath.startsWith('file://')) {
                scriptPath = providerPath.slice('file://'.length);
            }
            else {
                // For prefixed paths like 'exec:', 'python:', 'golang:'
                scriptPath = providerPath.split(':').slice(1).join(':');
            }
            (0, assertCustomAIExecutionPolicy_1.assertServerAllowsLocalCustomProviderPath)(scriptPath);
            // If using a cloud config, always use process.cwd() instead of context.basePath
            const isCloudConfig = providerOptions.config?.isCloudConfig === true;
            const resolvedPath = (0, file_1.getResolvedRelativePath)(scriptPath, isCloudConfig);
            return new providerConstructor(resolvedPath, providerOptions);
        },
    };
}
//# sourceMappingURL=scriptBasedProvider.js.map