"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.getLocations = getLocations;
exports.resolveStringToLocation = resolveStringToLocation;
const fs = __importStar(require("fs"));
async function getLocations(params) {
    try {
        if (!params.filepath) {
            return {
                success: false,
                message: "filepath is required",
            };
        }
        if (!params.searchString) {
            return {
                success: false,
                message: "searchString is required",
            };
        }
        let contents = params.fileContents;
        if (!contents) {
            try {
                contents = await fs.promises.readFile(params.filepath, "utf8");
            }
            catch (error) {
                return {
                    success: false,
                    message: `Failed to read file: ${error.message}`,
                };
            }
        }
        const matchType = params.matchType || "exact";
        const maxResults = params.maxResults || 50;
        const locations = [];
        const lines = contents.split('\n');
        for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
            const line = lines[lineIndex];
            const lineNumber = lineIndex + 1;
            let searchIndex = 0;
            while (searchIndex < line.length) {
                let matchIndex = -1;
                switch (matchType) {
                    case "exact":
                        matchIndex = line.indexOf(params.searchString, searchIndex);
                        break;
                    case "prefix":
                        for (let i = searchIndex; i <= line.length - params.searchString.length; i++) {
                            if (line.substring(i, i + params.searchString.length) === params.searchString) {
                                if (i === 0 || /\W/.test(line[i - 1])) {
                                    matchIndex = i;
                                    break;
                                }
                            }
                        }
                        break;
                    case "contains":
                        matchIndex = line.toLowerCase().indexOf(params.searchString.toLowerCase(), searchIndex);
                        break;
                }
                if (matchIndex === -1) {
                    break;
                }
                const contextStart = Math.max(0, matchIndex - 20);
                const contextEnd = Math.min(line.length, matchIndex + params.searchString.length + 20);
                const context = line.substring(contextStart, contextEnd);
                locations.push({
                    line: lineNumber,
                    column: matchIndex + 1,
                    context: context.trim(),
                    matchIndex: locations.length
                });
                if (locations.length >= maxResults) {
                    break;
                }
                searchIndex = matchIndex + 1;
            }
            if (locations.length >= maxResults) {
                break;
            }
        }
        if (locations.length === 0) {
            return {
                success: true,
                locations: [],
                message: `No matches found for "${params.searchString}" in ${params.filepath}`,
            };
        }
        return {
            success: true,
            locations,
            message: `Found ${locations.length} location${locations.length === 1 ? '' : 's'} for "${params.searchString}"`,
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to get locations: ${error.message}`,
        };
    }
}
async function resolveStringToLocation(filepath, searchString, fileContents, matchType = "exact") {
    const result = await getLocations({
        filepath,
        searchString,
        fileContents,
        matchType,
        maxResults: 1
    });
    if (result.success && result.locations && result.locations.length > 0) {
        const location = result.locations[0];
        return {
            line: location.line,
            column: location.column
        };
    }
    return null;
}
//# sourceMappingURL=getLocations.js.map