"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildInvalidWorkflowCommandOptionResult = buildInvalidWorkflowCommandOptionResult;
exports.buildInvalidWorkflowCommandOptionValueResult = buildInvalidWorkflowCommandOptionValueResult;
exports.buildWorkflowCommandOptionUpdateResult = buildWorkflowCommandOptionUpdateResult;
exports.buildWorkflowCommandOptionFlagUpdateResult = buildWorkflowCommandOptionFlagUpdateResult;
exports.applyWorkflowCommandOptionParseResult = applyWorkflowCommandOptionParseResult;
exports.resolveCommandOptionValue = resolveCommandOptionValue;
exports.isNegativeNumberToken = isNegativeNumberToken;
exports.parseRequiredWorkflowCommandOptionValue = parseRequiredWorkflowCommandOptionValue;
exports.parseRequiredWorkflowCommandOptionUpdate = parseRequiredWorkflowCommandOptionUpdate;
exports.rejectExistingWorkflowCommandOptionValues = rejectExistingWorkflowCommandOptionValues;
exports.parseExclusiveWorkflowCommandOptionUpdate = parseExclusiveWorkflowCommandOptionUpdate;
exports.parseWorkflowRunAtOrAfterOptionValue = parseWorkflowRunAtOrAfterOptionValue;
const schedule_primitives_1 = require("./schedule-primitives");
function buildInvalidWorkflowCommandOptionResult(reason, inlineTrailingText) {
    return {
        kind: "invalid",
        reason,
        inlineTrailingText,
    };
}
function buildInvalidWorkflowCommandOptionValueResult(tokens, index) {
    return buildInvalidWorkflowCommandOptionResult("invalid-option-value", tokens.slice(index + 1).join(" "));
}
function buildWorkflowCommandOptionUpdateResult(fields, consumedTokens, inlineTrailingText) {
    return {
        kind: "update",
        ...fields,
        consumedTokens,
        inlineTrailingText,
    };
}
function buildWorkflowCommandOptionFlagUpdateResult(fields, inlineTrailingText) {
    return buildWorkflowCommandOptionUpdateResult(fields, 1, inlineTrailingText);
}
function applyWorkflowCommandOptionParseResult(parsed, index, apply) {
    if (parsed.kind === "invalid") {
        return {
            kind: "invalid",
            result: parsed,
        };
    }
    apply(parsed);
    return {
        kind: "applied",
        nextIndex: index + parsed.consumedTokens - 1,
    };
}
function resolveCommandOptionValue(tokens, index, inlineTrailingText, options = {}) {
    const nextValue = tokens[index + 1];
    if (nextValue === undefined) {
        return buildInvalidWorkflowCommandOptionResult("missing-option-value", inlineTrailingText);
    }
    if (nextValue.startsWith("-") &&
        options.allowValueStartingWithDash?.(nextValue) !== true) {
        return buildInvalidWorkflowCommandOptionResult("missing-option-value", tokens.slice(index + 1).join(" "));
    }
    return { kind: "value", value: nextValue };
}
function isNegativeNumberToken(value) {
    return /^-(?:\d|\.\d)/u.test(value);
}
function parseRequiredWorkflowCommandOptionValue(tokens, index, inlineTrailingText, parseValue) {
    const optionValue = resolveCommandOptionValue(tokens, index, inlineTrailingText);
    if (optionValue.kind === "invalid") {
        return optionValue;
    }
    const parsedValue = parseValue(optionValue.value);
    if (parsedValue === null) {
        return buildInvalidWorkflowCommandOptionValueResult(tokens, index);
    }
    return {
        kind: "value",
        parsedValue,
    };
}
function parseRequiredWorkflowCommandOptionUpdate(tokens, index, inlineTrailingText, parseFields) {
    const optionValue = parseRequiredWorkflowCommandOptionValue(tokens, index, inlineTrailingText, parseFields);
    if (optionValue.kind === "invalid") {
        return optionValue;
    }
    return buildWorkflowCommandOptionUpdateResult(optionValue.parsedValue, 2, inlineTrailingText);
}
function rejectExistingWorkflowCommandOptionValues(existingValues, reason, inlineTrailingText) {
    return existingValues.some((value) => value !== undefined)
        ? buildInvalidWorkflowCommandOptionResult(reason, inlineTrailingText)
        : undefined;
}
function parseExclusiveWorkflowCommandOptionUpdate(tokens, index, inlineTrailingText, existingValues, conflictReason, parseFields) {
    const conflict = rejectExistingWorkflowCommandOptionValues(existingValues, conflictReason, inlineTrailingText);
    return (conflict ??
        parseRequiredWorkflowCommandOptionUpdate(tokens, index, inlineTrailingText, parseFields));
}
function parseWorkflowRunAtOrAfterOptionValue(mode, value) {
    const isValueValid = mode === "run_at"
        ? (0, schedule_primitives_1.isWorkflowRunAtInputShape)(value)
        : (0, schedule_primitives_1.parseWorkflowDurationToMilliseconds)(value) !== null;
    return isValueValid ? { mode, value } : null;
}
