"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperatingSystem = void 0;
const ExecutionBoard_1 = require("./ExecutionBoard");
const RoleHandoff_1 = require("./RoleHandoff");
const CadenceEngine_1 = require("./CadenceEngine");
class OperatingSystem {
    board;
    handoffs;
    cadence;
    constructor() {
        this.board = new ExecutionBoard_1.ExecutionBoard();
        this.handoffs = new RoleHandoff_1.RoleHandoff();
        this.cadence = new CadenceEngine_1.CadenceEngine();
    }
    decide(missionId) {
        const item = this.board.getAll().find((entry) => entry.id === missionId);
        const handoff = this.handoffs.getAll().find((entry) => entry.missionId === missionId && entry.status !== 'completed');
        const blockers = this.collectBlockers(item);
        const effectiveOwnerRole = handoff?.toRole || item?.ownerRole || 'coo';
        const effectivePriority = this.computePriority(item, blockers);
        const cadence = this.cadence.getAll().filter((event) => event.linkedMissionId === missionId);
        const hasReview = cadence.some((event) => event.kind === 'review');
        const shouldRelease = blockers.length === 0 && hasReview && item?.status === 'done';
        return {
            missionId,
            effectiveOwnerRole,
            effectivePriority,
            blockers,
            shouldRelease,
            rationale: `owner=${effectiveOwnerRole}; priority=${effectivePriority}; blockers=${blockers.join('|') || 'none'}; handoff=${handoff ? `${handoff.fromRole}->${handoff.toRole}` : 'none'}`
        };
    }
    scheduleWeeklyCadence(anchor = 'weekly-operating-cycle') {
        const planning = this.cadence.schedule('planning', `${anchor}: planning`, 'coo');
        const review = this.cadence.schedule('review', `${anchor}: review`, 'ceo');
        const release = this.cadence.schedule('release', `${anchor}: release`, 'cto');
        return [planning, review, release];
    }
    collectBlockers(item) {
        const blockers = [];
        if (!item)
            return ['missing-board-item'];
        if (item.blockerReason)
            blockers.push(item.blockerReason);
        if (item.dependencies?.length) {
            const all = this.board.getAll();
            for (const dep of item.dependencies) {
                const depItem = all.find((candidate) => candidate.id === dep);
                if (!depItem || depItem.status !== 'done')
                    blockers.push(`dependency:${dep}`);
            }
        }
        if (typeof item.slaHours === 'number' && typeof item.ageHours === 'number' && item.ageHours > item.slaHours) {
            blockers.push('sla-breached');
        }
        return blockers;
    }
    computePriority(item, blockers) {
        if (!item)
            return 'critical';
        if (blockers.includes('sla-breached'))
            return 'critical';
        if (item.priority === 'critical')
            return 'critical';
        if (blockers.length > 0)
            return 'high';
        return item.priority;
    }
}
exports.OperatingSystem = OperatingSystem;
