"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCadesExtensionPath = getCadesExtensionPath;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const logger_1 = require("./logger");
function getCadesExtensionPath(options) {
    const extensionFolderName = options?.extensionFolderName ?? 'Extension for CAdES Browser Plug-in 1.3.17';
    const manifestFileName = options?.manifestFileName ?? 'manifest.json';
    const candidateDirectories = options?.extensionsDir
        ? [options.extensionsDir]
        : [
            path_1.default.resolve(__dirname, '..', 'extensions'),
            path_1.default.resolve(__dirname, '..', '..', 'src', 'extensions'),
        ];
    for (const extensionsDir of candidateDirectories) {
        const extensionPath = path_1.default.join(extensionsDir, extensionFolderName);
        const manifestPath = path_1.default.join(extensionPath, manifestFileName);
        if (fs_1.default.existsSync(manifestPath)) {
            logger_1.logger.info(`CAdES extension path resolved: ${extensionPath}`, 'cades-extension-utils');
            return extensionPath;
        }
    }
    const searchedPaths = candidateDirectories.map(dir => path_1.default.join(dir, extensionFolderName)).join('\n');
    const errorMessage = `CAdES extension not found. Searched paths:\n${searchedPaths}\nPlease ensure the extension is properly installed.`;
    logger_1.logger.error(errorMessage, 'cades-extension-utils');
    throw new Error(errorMessage);
}
