var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
    if (typeof path === "string" && /^\.\.?\//.test(path)) {
        return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
            return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
        });
    }
    return path;
};
import { existsSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
export const INTERNAL_INTERCOM_BROKER_ARG = "--atomic-internal-intercom-broker";
export function getBundledIntercomBrokerPath(executablePath = process.execPath) {
    return join(dirname(executablePath), "builtin", "intercom", "broker", "broker.ts");
}
function comparablePath(path, platform) {
    const resolved = resolve(path);
    return platform === "win32" ? resolved.toLowerCase() : resolved;
}
export function isBundledIntercomBrokerPath(modulePath, executablePath = process.execPath, platform = process.platform) {
    return comparablePath(modulePath, platform) === comparablePath(getBundledIntercomBrokerPath(executablePath), platform);
}
export function validateInternalIntercomBrokerPath(modulePath, executablePath = process.execPath, platform = process.platform) {
    if (!modulePath) {
        throw new Error("Atomic internal intercom broker module path is required");
    }
    if (!isBundledIntercomBrokerPath(modulePath, executablePath, platform)) {
        throw new Error("Atomic internal intercom broker path must resolve to the bundled intercom broker module");
    }
    return resolve(modulePath);
}
export async function importInternalIntercomBroker(modulePath, executablePath = process.execPath, platform = process.platform) {
    const brokerPath = validateInternalIntercomBrokerPath(modulePath, executablePath, platform);
    if (!existsSync(brokerPath)) {
        throw new Error(`Atomic internal intercom broker module not found at ${brokerPath}`);
    }
    await import(__rewriteRelativeImportExtension(pathToFileURL(brokerPath).href));
}
//# sourceMappingURL=internal-intercom-broker.js.map