"use strict";
// Squad session persistence.
//
// Tracks which squad a chat session is associated with so callers do not
// need to re-pass `squadId` on every turn. Stored as a JSON map under
// `.harness/squads/sessions.json`. Cheap append-style writes.
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.getSquadForSession = getSquadForSession;
exports.setSquadForSession = setSquadForSession;
exports.clearSquadForSession = clearSquadForSession;
exports.resolveSessionSquad = resolveSessionSquad;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
function sessionsFilePath(projectDir) {
    return path.join(projectDir, '.harness', 'squads', 'sessions.json');
}
async function readStore(projectDir) {
    try {
        const raw = await fs.readFile(sessionsFilePath(projectDir), 'utf-8');
        const parsed = JSON.parse(raw);
        return {
            version: 1,
            associations: parsed.associations && typeof parsed.associations === 'object' && !Array.isArray(parsed.associations)
                ? Object.fromEntries(Object.entries(parsed.associations).filter(([key, value]) => typeof key === 'string' && typeof value === 'string'))
                : {},
        };
    }
    catch {
        return { version: 1, associations: {} };
    }
}
async function writeStore(projectDir, store) {
    const fp = sessionsFilePath(projectDir);
    await fs.mkdir(path.dirname(fp), { recursive: true });
    await fs.writeFile(fp, JSON.stringify(store, null, 2), 'utf-8');
}
async function getSquadForSession(projectDir, sessionId) {
    if (!sessionId)
        return undefined;
    const store = await readStore(projectDir);
    return store.associations[sessionId];
}
async function setSquadForSession(projectDir, sessionId, squadId) {
    if (!sessionId || !squadId)
        return;
    const store = await readStore(projectDir);
    if (store.associations[sessionId] === squadId)
        return;
    store.associations[sessionId] = squadId;
    await writeStore(projectDir, store);
}
async function clearSquadForSession(projectDir, sessionId) {
    const store = await readStore(projectDir);
    if (!(sessionId in store.associations))
        return false;
    delete store.associations[sessionId];
    await writeStore(projectDir, store);
    return true;
}
/**
 * Resolve the effective squadId for a chat turn. Caller-supplied id takes
 * precedence (and is persisted as the new association); otherwise the
 * stored association is returned, if any.
 */
async function resolveSessionSquad(projectDir, sessionId, explicitSquadId) {
    if (explicitSquadId && sessionId) {
        await setSquadForSession(projectDir, sessionId, explicitSquadId).catch(() => { });
        return explicitSquadId;
    }
    return getSquadForSession(projectDir, sessionId);
}
//# sourceMappingURL=squadSessions.js.map