/**
 * Get list of shell command completion options.
 * Each option is in the form of 'name:description',
 * where 'name' is the name of command that may be
 * selected from the options by presing tab, and
 * 'description' that is only used in selection list.
 *
 * The list of commands is fitered, in a way that only
 * commands, which name starts with value that user
 * started to type directly after CLI executable, are
 * returned.
 *
 * @param {Object} ctx - context with list of positional arguments
 * @param {Array.<NodeJS.Module>} commands - list of command modules
 * @returns {Array.<string>}
 */
export const getCommandCompletion = (ctx, commands) => {
    return commands
        .reduce((acc, cur) => {
            const meta = cur?.getMetadata?.();
            if (meta?.name?.startsWith?.(ctx.args['_'][1])) {
                acc.push(meta.name + ':' + meta.desc);
            }
            return acc;
        }, []);
};

export const getArgumentsCompletion = async (ctx, command) => {
    return command?.getCompletion?.(ctx);
};
