"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.loadModelProfiles = loadModelProfiles;
exports.saveModelProfiles = saveModelProfiles;
exports.getModelProfile = getModelProfile;
exports.setModelProfileField = setModelProfileField;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const STORE_REL_PATH = path.join('.harness', 'model-profiles.json');
function storePath(projectDir) {
    return path.join(projectDir, STORE_REL_PATH);
}
async function loadModelProfiles(projectDir) {
    const fp = storePath(projectDir);
    let raw;
    try {
        raw = await fs.readFile(fp, 'utf-8');
    }
    catch {
        return { profiles: {} };
    }
    try {
        const parsed = JSON.parse(raw);
        const profiles = {};
        if (parsed.profiles && typeof parsed.profiles === 'object') {
            for (const [model, value] of Object.entries(parsed.profiles)) {
                if (!model || typeof model !== 'string')
                    continue;
                const next = {};
                if (value && typeof value === 'object') {
                    const v = value;
                    if (typeof v.contextMaxTokens === 'number' && Number.isFinite(v.contextMaxTokens) && v.contextMaxTokens >= 0) {
                        next.contextMaxTokens = v.contextMaxTokens;
                    }
                    if (typeof v.validationProfile === 'string' && v.validationProfile.trim()) {
                        next.validationProfile = v.validationProfile.trim().slice(0, 80);
                    }
                    if (typeof v.pairedVisionModel === 'string' && v.pairedVisionModel.trim()) {
                        next.pairedVisionModel = v.pairedVisionModel.trim().slice(0, 120);
                    }
                }
                profiles[model] = next;
            }
        }
        return { profiles };
    }
    catch {
        // Malformed file should never crash the daemon; treat as empty.
        return { profiles: {} };
    }
}
async function saveModelProfiles(projectDir, store) {
    const fp = storePath(projectDir);
    await fs.mkdir(path.dirname(fp), { recursive: true });
    await fs.writeFile(fp, JSON.stringify(store, null, 2), 'utf-8');
}
async function getModelProfile(projectDir, model) {
    if (!model)
        return undefined;
    const store = await loadModelProfiles(projectDir);
    return store.profiles[model];
}
async function setModelProfileField(projectDir, model, field, value) {
    if (!model)
        throw new Error('model is required');
    const store = await loadModelProfiles(projectDir);
    const current = store.profiles[model] ?? {};
    const next = { ...current };
    if (value === undefined) {
        delete next[field];
    }
    else {
        next[field] = value;
    }
    // Drop the entry entirely when no fields remain so the file stays
    // tidy and round-trips correctly.
    const hasAnyField = Object.values(next).some((v) => v !== undefined);
    if (hasAnyField) {
        store.profiles[model] = next;
    }
    else {
        delete store.profiles[model];
    }
    await saveModelProfiles(projectDir, store);
    return store;
}
//# sourceMappingURL=modelProfiles.js.map