// Pure rewrite of a premium plugin's compiled-output import path so that
// `(../)+platform/lib/` resolves to the lib's shipped location under the
// bundle root at deploy time. The total number of `../` segments is the
// sum of:
//
//   deployDepth      — depth from `dist/` to the bundle's `lib/` sibling.
//                       Standalone MCP:        `mcp/dist/`        → 2
//                       Bundle-sub MCP:        `plugins/<sub>/mcp/dist/` → 4
//
//   intraDistDepth   — depth of the importing file inside `dist/`.
//                       `dist/index.js`           → 0
//                       `dist/tools/foo.js`       → 1
//                       `dist/a/b/c.js`           → 2
//
// Without the intra-dist component, a file at `dist/tools/foo.js` emits
// `../../lib/...` which resolves to `mcp/lib/...` — one level shallower
// than the shipped `<bundle>/lib/...` location, producing
// ERR_MODULE_NOT_FOUND on first spawn after install.
//
// The bundler runs on macOS (laptop) and Linux (Pi); never Windows.
// readdirSync emits POSIX `/` separators on those platforms, so the
// split is hard-coded to `/`.
const PLATFORM_LIB_PREFIX = /(?:\.\.\/)+platform\/lib\//g;
export function rewritePlatformLibImports(entry, content, deployDepth) {
    const intraDistDepth = entry.split("/").length - 1;
    const totalUps = deployDepth + intraDistDepth;
    const replacement = `${"../".repeat(totalUps)}lib/`;
    return content.replace(PLATFORM_LIB_PREFIX, replacement);
}
