"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.suggestNextPhaseForSlice = suggestNextPhaseForSlice;
exports.suggestNextPhaseFromSlice = suggestNextPhaseFromSlice;
exports.suggestNextMilestoneFromPhase = suggestNextMilestoneFromPhase;
exports.renderAssistiveSuggestionText = renderAssistiveSuggestionText;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const planning_document_utils_1 = require("./planning-document-utils");
function suggestNextPhaseForSlice(options) {
    return suggestFromDeclaredChildren({
        repositoryRoot: options.repositoryRoot,
        parentOverviewPath: options.sliceOverviewPath,
        sectionName: "Phases",
        artifactKind: "phase",
        currentSlug: undefined,
        commandForSlug: (slug, parentOverviewPath) => {
            return `day-shift phase new --slice-overview ${parentOverviewPath} --phase-slug ${slug}`;
        }
    });
}
function suggestNextPhaseFromSlice(options) {
    return suggestFromDeclaredChildren({
        repositoryRoot: options.repositoryRoot,
        parentOverviewPath: options.sliceOverviewPath,
        sectionName: "Phases",
        artifactKind: "phase",
        currentSlug: options.currentPhaseSlug,
        commandForSlug: (slug, parentOverviewPath) => {
            return `day-shift phase new --slice-overview ${parentOverviewPath} --phase-slug ${slug}`;
        }
    });
}
function suggestNextMilestoneFromPhase(options) {
    return suggestFromDeclaredChildren({
        repositoryRoot: options.repositoryRoot,
        parentOverviewPath: options.phaseOverviewPath,
        sectionName: "Milestones",
        artifactKind: "milestone",
        currentSlug: options.currentMilestoneSlug,
        commandForSlug: (slug, parentOverviewPath) => {
            return `day-shift milestone new --phase-overview ${parentOverviewPath} --milestone-slug ${slug}`;
        }
    });
}
function renderAssistiveSuggestionText(suggestion) {
    const lines = ["Assistive next step:"];
    if (suggestion.status === "suggested") {
        lines.push(`Recommended ${suggestion.artifactKind}: ${suggestion.artifactSlug}`);
        lines.push(`Run: ${suggestion.command}`);
        return lines;
    }
    lines.push(suggestion.message);
    return lines;
}
function suggestFromDeclaredChildren(options) {
    const parentAbsolutePath = node_path_1.default.join(options.repositoryRoot, options.parentOverviewPath);
    const declaredSlugs = safeReadDeclaredChildSlugs(parentAbsolutePath, options.sectionName);
    if (declaredSlugs.length === 0) {
        return noFurtherDeclaredArtifact(options.artifactKind, options.parentOverviewPath);
    }
    let nextSlug;
    if (options.currentSlug === undefined) {
        nextSlug = declaredSlugs[0];
    }
    else {
        const currentIndex = declaredSlugs.findIndex((slug) => slug === options.currentSlug);
        if (currentIndex >= 0 && currentIndex + 1 < declaredSlugs.length) {
            nextSlug = declaredSlugs[currentIndex + 1];
        }
    }
    if (nextSlug === undefined) {
        return noFurtherDeclaredArtifact(options.artifactKind, options.parentOverviewPath);
    }
    return {
        status: "suggested",
        artifactKind: options.artifactKind,
        parentOverviewPath: options.parentOverviewPath,
        artifactSlug: nextSlug,
        command: options.commandForSlug(nextSlug, options.parentOverviewPath),
        message: `Next declared ${options.artifactKind} is '${nextSlug}'.`
    };
}
function safeReadDeclaredChildSlugs(parentOverviewPath, sectionName) {
    try {
        if (!node_fs_1.default.existsSync(parentOverviewPath)) {
            return [];
        }
        const fileText = node_fs_1.default.readFileSync(parentOverviewPath, "utf8");
        return (0, planning_document_utils_1.readDeclaredChildSlugs)(fileText, sectionName);
    }
    catch {
        return [];
    }
}
function noFurtherDeclaredArtifact(artifactKind, parentOverviewPath) {
    return {
        status: "none",
        artifactKind,
        parentOverviewPath,
        artifactSlug: null,
        command: null,
        message: "No further declared artifact to suggest."
    };
}
