<?php

declare(strict_types=1);

namespace ShieldX\WPCLI;

defined('ABSPATH') || exit;

final class CommandOutput
{
    /**
     * @param array<string,mixed> $assocArgs
     */
    public static function isJson(array $assocArgs): bool
    {
        return strtolower((string) ($assocArgs['format'] ?? '')) === 'json';
    }

    /**
     * @param array<string,mixed> $payload
     */
    public static function writeJson(array $payload): void
    {
        $json = wp_json_encode($payload, JSON_UNESCAPED_SLASHES);
        if (!is_string($json)) {
            \WP_CLI::error('Could not encode SHIELD-X WP-CLI JSON response.');
        }

        \WP_CLI::line($json);
    }
}
