"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWorkflowCommandComment = parseWorkflowCommandComment;
exports.parseWorkflowCommandFirstLine = parseWorkflowCommandFirstLine;
exports.hasIssueCommentCommandOnFirstNonEmptyLine = hasIssueCommentCommandOnFirstNonEmptyLine;
exports.parseApproveCommandComment = parseApproveCommandComment;
exports.parseStartCommandComment = parseStartCommandComment;
exports.isOperationalCommandComment = isOperationalCommandComment;
const branding_1 = require("../core/branding");
const workflow_option_normalizer_1 = require("./workflow-option-normalizer");
const WORKFLOW_COMMAND_PREFIX = branding_1.ISSUE_COMMENT_COMMAND_PREFIX;
const APPROVE_COMMAND = branding_1.ISSUE_COMMENT_APPROVE_COMMAND;
const START_COMMAND = branding_1.ISSUE_COMMENT_START_COMMAND;
function parseWorkflowCommandComment(commentBody) {
    const lines = commentBody.split(/\r?\n/);
    const firstNonEmptyLineIndex = findFirstNonEmptyLineIndex(lines);
    if (firstNonEmptyLineIndex === -1) {
        return { kind: "none", bodyAfterCommand: "" };
    }
    const firstLine = lines[firstNonEmptyLineIndex]?.trim();
    if (firstLine === undefined) {
        return { kind: "none", bodyAfterCommand: "" };
    }
    const firstLineCommand = parseWorkflowCommandFirstLine(firstLine);
    if (firstLineCommand.kind !== "workflow") {
        return { kind: "none", bodyAfterCommand: "" };
    }
    return (0, workflow_option_normalizer_1.parseWorkflowOptionTail)(firstLineCommand.rawCommandTail, collectRemainingBody(lines, firstNonEmptyLineIndex));
}
function parseWorkflowCommandFirstLine(line) {
    const firstLine = line.trim();
    if (!isWorkflowCommandPrefix(firstLine)) {
        return { kind: "none" };
    }
    if (firstLine === APPROVE_COMMAND) {
        return { kind: "approve" };
    }
    if (firstLine === START_COMMAND) {
        return { kind: "start" };
    }
    return {
        kind: "workflow",
        rawCommandTail: firstLine.slice(WORKFLOW_COMMAND_PREFIX.length).trim(),
    };
}
function hasIssueCommentCommandOnFirstNonEmptyLine(text) {
    const lines = text.split(/\r?\n/);
    const firstNonEmptyLineIndex = findFirstNonEmptyLineIndex(lines);
    if (firstNonEmptyLineIndex === -1) {
        return false;
    }
    const firstLine = lines[firstNonEmptyLineIndex];
    return (firstLine !== undefined &&
        parseWorkflowCommandFirstLine(firstLine).kind !== "none");
}
function parseApproveCommandComment(commentBody) {
    return parseExactOperationalCommandComment(commentBody, APPROVE_COMMAND);
}
function parseStartCommandComment(commentBody) {
    return parseExactOperationalCommandComment(commentBody, START_COMMAND);
}
function isOperationalCommandComment(commentBody) {
    return (parseApproveCommandComment(commentBody) ||
        parseStartCommandComment(commentBody) ||
        parseWorkflowCommandComment(commentBody).kind !== "none");
}
function parseExactOperationalCommandComment(commentBody, command) {
    const lines = commentBody.split(/\r?\n/);
    const firstNonEmptyLineIndex = findFirstNonEmptyLineIndex(lines);
    if (firstNonEmptyLineIndex === -1) {
        return false;
    }
    return lines[firstNonEmptyLineIndex]?.trim() === command;
}
function findFirstNonEmptyLineIndex(lines) {
    for (const [index, line] of lines.entries()) {
        if (line.trim().length > 0) {
            return index;
        }
    }
    return -1;
}
function collectRemainingBody(lines, firstLineIndex) {
    return trimSurroundingBlankLines(lines.slice(firstLineIndex + 1)).join("\n");
}
function trimSurroundingBlankLines(lines) {
    let start = 0;
    let end = lines.length;
    while (start < end && lines[start]?.trim().length === 0) {
        start += 1;
    }
    while (end > start && lines[end - 1]?.trim().length === 0) {
        end -= 1;
    }
    return lines.slice(start, end);
}
function isWorkflowCommandPrefix(line) {
    return (line === WORKFLOW_COMMAND_PREFIX ||
        line.startsWith(`${WORKFLOW_COMMAND_PREFIX} `));
}
