"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findSimilarStrings = exports.levenshteinDistance = exports.expandPath = exports.error = void 0;
const path = __importStar(require("path"));
const os = __importStar(require("os"));
/**
 * Simple error formatting
 */
function error(message) {
    console.error(`ERROR: ${message}`);
    console.error('Try: npm install -g @kaitranntt/ccs --force');
    process.exit(1);
}
exports.error = error;
/**
 * Path expansion (~ and env vars)
 */
function expandPath(pathStr) {
    // Normalize separators first to handle mixed paths
    pathStr = pathStr.replace(/\\/g, '/');
    // Handle tilde expansion
    if (pathStr.startsWith('~/')) {
        pathStr = path.join(os.homedir(), pathStr.slice(2));
    }
    else if (pathStr === '~') {
        pathStr = os.homedir();
    }
    // Expand environment variables (Windows and Unix)
    pathStr = pathStr.replace(/\$\{([^}]+)\}/g, (_, name) => process.env[name] || '');
    pathStr = pathStr.replace(/\$([A-Z_][A-Z0-9_]*)/gi, (_, name) => process.env[name] || '');
    // Windows %VAR% style
    if (process.platform === 'win32') {
        pathStr = pathStr.replace(/%([^%]+)%/g, (_, name) => process.env[name] || '');
    }
    return path.normalize(pathStr);
}
exports.expandPath = expandPath;
/**
 * Calculate Levenshtein distance between two strings
 */
function levenshteinDistance(a, b) {
    if (a.length === 0)
        return b.length;
    if (b.length === 0)
        return a.length;
    const matrix = [];
    // Initialize first row and column
    for (let i = 0; i <= b.length; i++) {
        matrix[i] = [i];
    }
    for (let j = 0; j <= a.length; j++) {
        matrix[0][j] = j;
    }
    // Fill in the rest of the matrix
    for (let i = 1; i <= b.length; i++) {
        for (let j = 1; j <= a.length; j++) {
            if (b.charAt(i - 1) === a.charAt(j - 1)) {
                matrix[i][j] = matrix[i - 1][j - 1];
            }
            else {
                matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
                matrix[i][j - 1] + 1, // insertion
                matrix[i - 1][j] + 1 // deletion
                );
            }
        }
    }
    return matrix[b.length][a.length];
}
exports.levenshteinDistance = levenshteinDistance;
/**
 * Find similar strings using fuzzy matching
 */
function findSimilarStrings(target, candidates, maxDistance = 2) {
    const targetLower = target.toLowerCase();
    const matches = candidates
        .map((candidate) => ({
        name: candidate,
        distance: levenshteinDistance(targetLower, candidate.toLowerCase()),
    }))
        .filter((item) => item.distance <= maxDistance && item.distance > 0)
        .sort((a, b) => a.distance - b.distance)
        .slice(0, 3) // Show at most 3 suggestions
        .map((item) => item.name);
    return matches;
}
exports.findSimilarStrings = findSimilarStrings;
//# sourceMappingURL=helpers.js.map