"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.createSmartInitRunContext = createSmartInitRunContext;
const path = __importStar(require("node:path"));
const apiDbHintDetection_1 = require("./apiDbHintDetection");
const projectKindDetection_1 = require("./projectKindDetection");
const stackDetection_1 = require("./stackDetection");
function normalizeRoot(workspaceRoot) {
    const resolved = path.resolve(workspaceRoot);
    return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
}
function createSmartInitRunContext() {
    const stackResults = new Map();
    const projectKindResults = new Map();
    const apiDbResults = new Map();
    const ignoreMatcherIds = new WeakMap();
    let nextIgnoreMatcherId = 1;
    const counts = {
        stacks: 0,
        projectKinds: 0,
        apiDbHints: 0,
    };
    const matcherKey = (ignoreMatcher) => {
        if (!ignoreMatcher) {
            return 'ignore:none';
        }
        const existing = ignoreMatcherIds.get(ignoreMatcher);
        if (existing) {
            return `ignore:${existing}`;
        }
        const id = nextIgnoreMatcherId;
        nextIgnoreMatcherId += 1;
        ignoreMatcherIds.set(ignoreMatcher, id);
        return `ignore:${id}`;
    };
    const cacheKey = (workspaceRoot, options) => `${normalizeRoot(workspaceRoot)}|${matcherKey(options?.ignoreMatcher)}`;
    return {
        detectStacks(workspaceRoot, options) {
            const key = cacheKey(workspaceRoot, options);
            const cached = stackResults.get(key);
            if (cached) {
                return cached;
            }
            counts.stacks += 1;
            const result = (0, stackDetection_1.detectStacks)(workspaceRoot, options);
            stackResults.set(key, result);
            return result;
        },
        detectProjectKinds(workspaceRoot, options) {
            const key = cacheKey(workspaceRoot, options);
            const cached = projectKindResults.get(key);
            if (cached) {
                return cached;
            }
            counts.projectKinds += 1;
            const result = (0, projectKindDetection_1.detectProjectKinds)(workspaceRoot, options);
            projectKindResults.set(key, result);
            return result;
        },
        detectApiDbHints(workspaceRoot, options) {
            const key = cacheKey(workspaceRoot, options);
            const cached = apiDbResults.get(key);
            if (cached) {
                return cached;
            }
            counts.apiDbHints += 1;
            const result = (0, apiDbHintDetection_1.detectApiDbHints)(workspaceRoot, {
                ...(options?.ignoreMatcher ? { ignoreMatcher: options.ignoreMatcher } : {}),
                projectKinds: this.detectProjectKinds(workspaceRoot, options),
            });
            apiDbResults.set(key, result);
            return result;
        },
        getDetectorCallCounts() {
            return { ...counts };
        },
    };
}
//# sourceMappingURL=runContext.js.map