'use strict';

var node_fs = require('node:fs');
var node_module = require('node:module');
var path = require('node:path');
var node_url = require('node:url');
var errors = require('./errors.cjs');

var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('runtime/embedded-cli.cjs', document.baseURI).href)));
const TARGET_TO_PACKAGE = {
    'darwin-arm64': '@superdoc-dev/sdk-darwin-arm64',
    'darwin-x64': '@superdoc-dev/sdk-darwin-x64',
    'linux-x64': '@superdoc-dev/sdk-linux-x64',
    'linux-arm64': '@superdoc-dev/sdk-linux-arm64',
    'windows-x64': '@superdoc-dev/sdk-windows-x64',
};
const TARGET_TO_DIR = {
    'darwin-arm64': 'sdk-darwin-arm64',
    'darwin-x64': 'sdk-darwin-x64',
    'linux-x64': 'sdk-linux-x64',
    'linux-arm64': 'sdk-linux-arm64',
    'windows-x64': 'sdk-windows-x64',
};
function resolveTarget() {
    const platform = process.platform;
    const arch = process.arch;
    if (platform === 'darwin' && arch === 'arm64')
        return 'darwin-arm64';
    if (platform === 'darwin' && arch === 'x64')
        return 'darwin-x64';
    if (platform === 'linux' && arch === 'x64')
        return 'linux-x64';
    if (platform === 'linux' && arch === 'arm64')
        return 'linux-arm64';
    if (platform === 'win32' && arch === 'x64')
        return 'windows-x64';
    return null;
}
function binaryNameForTarget(target) {
    return target === 'windows-x64' ? 'superdoc.exe' : 'superdoc';
}
function ensureExecutable(binaryPath) {
    if (process.platform === 'win32')
        return;
    try {
        node_fs.chmodSync(binaryPath, 0o755);
    }
    catch {
        // Non-fatal: if chmod fails, spawn() will surface the real execution error.
    }
}
function resolveFromPlatformPackage(target) {
    const pkg = TARGET_TO_PACKAGE[target];
    const binaryName = binaryNameForTarget(target);
    try {
        return require$1.resolve(`${pkg}/bin/${binaryName}`);
    }
    catch {
        return null;
    }
}
function resolveFromWorkspaceFallback(target) {
    const binaryName = binaryNameForTarget(target);
    const dirName = TARGET_TO_DIR[target];
    const filePath = path.resolve(node_url.fileURLToPath(new URL('../../platforms', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('runtime/embedded-cli.cjs', document.baseURI).href)))), dirName, 'bin', binaryName);
    if (!node_fs.existsSync(filePath))
        return null;
    return filePath;
}
/**
 * Resolve the path to the embedded SuperDoc CLI binary for the current platform.
 */
function resolveEmbeddedCliBinary() {
    const target = resolveTarget();
    if (!target) {
        throw new errors.SuperDocCliError('No embedded SuperDoc CLI binary is available for this platform.', {
            code: 'UNSUPPORTED_PLATFORM',
            details: {
                platform: process.platform,
                arch: process.arch,
            },
        });
    }
    const platformPackagePath = resolveFromPlatformPackage(target);
    const resolvedPath = platformPackagePath ?? resolveFromWorkspaceFallback(target);
    if (!resolvedPath) {
        throw new errors.SuperDocCliError('Embedded SuperDoc CLI binary is missing for this platform.', {
            code: 'CLI_BINARY_MISSING',
            details: {
                target,
                packageName: TARGET_TO_PACKAGE[target],
            },
        });
    }
    ensureExecutable(resolvedPath);
    return resolvedPath;
}

exports.resolveEmbeddedCliBinary = resolveEmbeddedCliBinary;
