"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSampleEventHookPayload = buildSampleEventHookPayload;
const config_1 = require("./config");
const workflow_labels_1 = require("../platform/ticket-tracker/workflow-labels");
const types_1 = require("../platform/workflow-state/types");
const publisher_events_1 = require("../workflows/publisher-events");
function buildSampleEventHookPayload(hook) {
    const sample = buildSampleScenario(hook);
    return {
        schemaVersion: 1,
        eventId: "sample-event",
        occurredAt: new Date().toISOString(),
        hook: {
            path: hook.path,
            runner: hook.runner ?? (0, config_1.inferEventHookRunner)(hook.path),
        },
        matchedTriggers: sample.matchedTriggers,
        repository: {
            host: "github",
            projectRef: "owner/repo",
            baseBranch: "main",
        },
        issue: {
            id: "sample-issue",
            displayId: "#123",
            title: "Sample event hook test issue",
            url: "https://example.invalid/issues/123",
            kind: "issue",
            isOpen: !sample.targetClosed,
            labelNames: [sample.nextLabel],
        },
        transition: {
            previousStatus: sample.previousStatus,
            nextStatus: sample.nextStatus,
            previousLabel: sample.previousLabel,
            nextLabel: sample.nextLabel,
        },
        publish: {
            status: "applied",
            visibleEvent: sample.visibleEvent,
            targetClosed: sample.targetClosed,
            commentUrl: "https://example.invalid/issues/123#comment",
            workflowStateCommentUrl: "https://example.invalid/issues/123#state",
        },
        pullRequest: {
            url: null,
            threadId: null,
        },
        result: {
            runMode: "workflow_execute",
            workerOutcome: sample.workerOutcome,
            needsHumanReason: sample.needsHumanReason,
            summary: sample.summary,
            nextStep: sample.nextStep,
            workflowTemplateName: "sample",
        },
    };
}
function buildSampleScenario(hook) {
    const primaryTrigger = hook.on.find((trigger) => trigger !== "all") ?? "all";
    const eventTokens = new Set(["all", primaryTrigger]);
    if (primaryTrigger === "workerFailed" || primaryTrigger === "systemFailed") {
        eventTokens.add("aiNeedsHuman");
    }
    const matchedTriggers = hook.on.filter((trigger) => eventTokens.has(trigger));
    const triggerMatched = matchedTriggers.length === 0 ? [primaryTrigger] : [...matchedTriggers];
    switch (primaryTrigger) {
        case "aiNeedsHuman":
        case "workerFailed":
        case "systemFailed":
            return {
                matchedTriggers: triggerMatched,
                previousStatus: types_1.WORKFLOW_STATUSES.aiRunning,
                nextStatus: types_1.WORKFLOW_STATUSES.aiNeedsHuman,
                previousLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiRunning,
                nextLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiNeedsHuman,
                visibleEvent: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.humanAttentionRequested,
                targetClosed: false,
                workerOutcome: "wait_human",
                needsHumanReason: primaryTrigger === "aiNeedsHuman"
                    ? publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.question
                    : publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.systemError,
                summary: primaryTrigger === "aiNeedsHuman"
                    ? "Sample issue needs human attention."
                    : "Sample failure needs investigation. Details are hidden for security.",
                nextStep: primaryTrigger === "aiNeedsHuman"
                    ? "Review the issue and decide the next action."
                    : "Paste the suggested investigation prompt into chat and resolve the stopped run.",
            };
        case "issueCreated":
            return {
                matchedTriggers: triggerMatched,
                previousStatus: types_1.WORKFLOW_STATUSES.aiReady,
                nextStatus: types_1.WORKFLOW_STATUSES.aiReady,
                previousLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiReady,
                nextLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiReady,
                visibleEvent: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.issueCreated,
                targetClosed: false,
                workerOutcome: "proceed",
                needsHumanReason: null,
                summary: "Sample issue was created.",
                nextStep: "Wait for AlpacaLoop to process the issue.",
            };
        case "all":
        case "completed":
            return {
                matchedTriggers: triggerMatched,
                previousStatus: types_1.WORKFLOW_STATUSES.aiRunning,
                nextStatus: types_1.WORKFLOW_STATUSES.aiReady,
                previousLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiRunning,
                nextLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiReady,
                visibleEvent: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.workflowCompleted,
                targetClosed: true,
                workerOutcome: "proceed",
                needsHumanReason: null,
                summary: "Sample workflow completed.",
                nextStep: "No next step.",
            };
        default:
            return {
                matchedTriggers: triggerMatched,
                previousStatus: types_1.WORKFLOW_STATUSES.aiRunning,
                nextStatus: statusForLabelTrigger(primaryTrigger),
                previousLabel: workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiRunning,
                nextLabel: labelForTrigger(primaryTrigger),
                visibleEvent: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.workflowStarted,
                targetClosed: false,
                workerOutcome: "proceed",
                needsHumanReason: null,
                summary: `Sample issue transitioned to ${primaryTrigger}.`,
                nextStep: "Continue normal workflow handling.",
            };
    }
}
function statusForLabelTrigger(trigger) {
    switch (trigger) {
        case "aiReady":
            return types_1.WORKFLOW_STATUSES.aiReady;
        case "aiNeedsHuman":
        case "aiQuestion":
        case "aiRunLimit":
            return types_1.WORKFLOW_STATUSES.aiNeedsHuman;
        case "aiWorkingInPr":
            return types_1.WORKFLOW_STATUSES.aiWorkingInPr;
        case "aiRecurring":
            return types_1.WORKFLOW_STATUSES.aiRecurring;
        case "aiScheduled":
        case "aiCoolingDown":
            return types_1.WORKFLOW_STATUSES.aiScheduled;
        default:
            return types_1.WORKFLOW_STATUSES.aiRunning;
    }
}
function labelForTrigger(trigger) {
    return (workflow_labels_1.DEFAULT_WORKFLOW_LABELS[trigger] ??
        workflow_labels_1.DEFAULT_WORKFLOW_LABELS.aiRunning);
}
