"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildRequiredSkillAvailabilityReportFromInventory = buildRequiredSkillAvailabilityReportFromInventory;
const required_skills_1 = require("../workflows/required-skills");
const skill_inventory_1 = require("../workflows/skill-inventory");
function buildRequiredSkillAvailabilityReportFromInventory(input) {
    const skillRefs = (0, required_skills_1.dedupeRequiredSkillRefs)(input.skillRefs).sort();
    const workers = [
        ...new Set(input.skillInventory.map((skill) => skill.worker)),
    ].sort();
    if (skillRefs.length === 0) {
        return { ok: true, skillRefs, missing: [], runnableWorkers: workers };
    }
    const usableRefsByWorker = new Set(input.skillInventory
        .filter((skill) => skill.usableForRequiredSkills)
        .map((skill) => `${skill.worker}\0${skill.ref}`));
    const missing = skillRefs.flatMap((skillRef) => workers
        .filter((worker) => (0, skill_inventory_1.requiredSkillRefCanRunOnWorker)(skillRef, worker) &&
        !usableRefsByWorker.has(`${worker}\0${skillRef}`))
        .map((worker) => ({
        skillRef,
        worker,
        skillPath: `${worker}:${skillRef}`,
    })));
    const runnableWorkers = workers.filter((worker) => skillRefs.every((skillRef) => (0, skill_inventory_1.requiredSkillRefCanRunOnWorker)(skillRef, worker) &&
        usableRefsByWorker.has(`${worker}\0${skillRef}`)));
    return {
        ok: missing.length === 0 && runnableWorkers.length > 0,
        skillRefs,
        missing,
        runnableWorkers,
    };
}
