import { Args } from '@oclif/core';
import { nonNegativeNumberDecoder } from '../shared/decoders.js';
export const newAppArg = Args.custom({
    async parse(input, _, opts) {
        const commandSplit = input.split("@");
        if (commandSplit.length > 2) {
            throw new Error("Invalid input. Multiple version separators (@) are not allowed.");
        }
        const appCommand = commandSplit[0];
        const version = commandSplit[1];
        if (!opts.supportedApps?.includes(appCommand)) {
            throw new Error(`Invalid input. Supported options are: ${opts.supportedApps?.join(", ")}.`);
        }
        if (!version || version === "latest") {
            return appCommand;
        }
        const versionSplit = version.split(".");
        if (versionSplit.length > 2) {
            throw new Error("Invalid input. Multiple version separators (.) are not allowed.");
        }
        const major = Number(versionSplit[0]);
        const minor = Number(versionSplit[1]);
        if (!major) {
            throw new Error("Invalid input. Major version must be a number.");
        }
        if (!minor && minor !== 0) {
            throw new Error("Invalid input. Minor version must be a number.");
        }
        nonNegativeNumberDecoder.runWithException(Number(versionSplit[0]));
        nonNegativeNumberDecoder.runWithException(Number(versionSplit[1]));
        return input;
    },
});
