"use strict";
/**
 * Utilities for handling tilde path expansion
 *
 * For SSH commands, tilde paths need special handling:
 * - Quoted tildes won't expand: `cd '~'` fails, but `cd "$HOME"` works
 * - Must escape special shell characters when using $HOME expansion
 *
 * For local paths, tildes should be expanded to actual file system paths.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.expandTilde = expandTilde;
exports.expandTildeForSSH = expandTildeForSSH;
exports.cdCommandForSSH = cdCommandForSSH;
const os = __importStar(require("os"));
const path = __importStar(require("path"));
/**
 * Expand tilde to actual home directory path for local file system operations.
 *
 * Converts:
 * - "~" → "/home/user" (actual home directory)
 * - "~/path" → "/home/user/path"
 * - "/abs/path" → "/abs/path" (unchanged)
 *
 * @param filePath - Path that may contain tilde prefix
 * @returns Fully expanded absolute path
 *
 * @example
 * expandTilde("~")           // => "/home/user"
 * expandTilde("~/workspace") // => "/home/user/workspace"
 * expandTilde("/abs/path")   // => "/abs/path"
 */
function expandTilde(filePath) {
    if (filePath === "~") {
        return os.homedir();
    }
    else if (filePath.startsWith("~/")) {
        return path.join(os.homedir(), filePath.slice(2));
    }
    else {
        return filePath;
    }
}
/**
 * Expand tilde path to $HOME-based path for use in SSH commands.
 *
 * Converts:
 * - "~" → "$HOME"
 * - "~/path" → "$HOME/path"
 * - "/abs/path" → quoted absolute path (no expansion)
 *
 * The result is safe to use in bash commands and will properly expand at runtime.
 * Special characters in paths are escaped for use inside double quotes.
 *
 * @param path - Path that may contain tilde prefix
 * @returns Bash-safe string ready to use in commands
 *
 * @example
 * expandTildeForSSH("~")           // => "$HOME"
 * expandTildeForSSH("~/workspace") // => "$HOME/workspace"
 * expandTildeForSSH("/abs/path")   // => '"/abs/path"'
 */
function expandTildeForSSH(path) {
    if (path === "~") {
        return '"$HOME"';
    }
    else if (path.startsWith("~/")) {
        const pathAfterTilde = path.slice(2);
        // Escape special chars for use inside double quotes
        const escaped = pathAfterTilde
            .replace(/\\/g, "\\\\")
            .replace(/"/g, '\\"')
            .replace(/\$/g, "\\$")
            .replace(/`/g, "\\`");
        return `"$HOME/${escaped}"`;
    }
    else {
        // No tilde - quote the path as-is
        // Note: We use double quotes to allow variable expansion if needed
        return `"${path.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\$/g, "\\$").replace(/`/g, "\\`")}"`;
    }
}
/**
 * Generate a cd command for use in SSH exec, handling tilde paths correctly.
 *
 * @param path - Working directory path (may contain tilde)
 * @returns Bash command string like `cd "$HOME/path"`
 *
 * @example
 * cdCommandForSSH("~")           // => 'cd "$HOME"'
 * cdCommandForSSH("~/workspace") // => 'cd "$HOME/workspace"'
 * cdCommandForSSH("/abs/path")   // => 'cd "/abs/path"'
 */
function cdCommandForSSH(path) {
    return `cd ${expandTildeForSSH(path)}`;
}
//# sourceMappingURL=tildeExpansion.js.map