"use strict";
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.validateProjectPath = validateProjectPath;
const fs = __importStar(require("fs/promises"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
/**
 * Expand tilde (~) in paths to the user's home directory
 *
 * @param inputPath - Path that may contain tilde
 * @returns Path with tilde expanded to home directory
 *
 * @example
 * expandTilde("~/Documents") // => "/home/user/Documents"
 * expandTilde("~") // => "/home/user"
 * expandTilde("/absolute/path") // => "/absolute/path"
 */
function expandTilde(inputPath) {
    if (!inputPath) {
        return inputPath;
    }
    if (inputPath === "~") {
        return os.homedir();
    }
    if (inputPath.startsWith("~/") || inputPath.startsWith("~\\")) {
        return path.join(os.homedir(), inputPath.slice(2));
    }
    return inputPath;
}
/**
 * Validate that a project path exists, is a directory, and is a git repository
 * Automatically expands tilde and normalizes the path
 *
 * @param inputPath - Path to validate (may contain tilde)
 * @returns Validation result with expanded path or error
 *
 * @example
 * await validateProjectPath("~/my-project")
 * // => { valid: true, expandedPath: "/home/user/my-project" }
 *
 * await validateProjectPath("~/nonexistent")
 * // => { valid: false, error: "Path does not exist: /home/user/nonexistent" }
 *
 * await validateProjectPath("~/not-a-git-repo")
 * // => { valid: false, error: "Not a git repository: /home/user/not-a-git-repo" }
 */
async function validateProjectPath(inputPath) {
    // Expand tilde if present
    const expandedPath = expandTilde(inputPath);
    // Normalize to resolve any .. or . in the path
    const normalizedPath = path.normalize(expandedPath);
    // Check if path exists
    try {
        const stats = await fs.stat(normalizedPath);
        // Check if it's a directory
        if (!stats.isDirectory()) {
            return {
                valid: false,
                error: `Path is not a directory: ${normalizedPath}`,
            };
        }
    }
    catch (err) {
        if (err.code === "ENOENT") {
            return {
                valid: false,
                error: `Path does not exist: ${normalizedPath}`,
            };
        }
        throw err;
    }
    // Check if it's a git repository
    const gitPath = path.join(normalizedPath, ".git");
    try {
        await fs.stat(gitPath);
    }
    catch (err) {
        if (err.code === "ENOENT") {
            return {
                valid: false,
                error: `Not a git repository: ${normalizedPath}`,
            };
        }
        throw err;
    }
    return {
        valid: true,
        expandedPath: normalizedPath,
    };
}
//# sourceMappingURL=pathUtils.js.map