/*
 * Copyright (c) 2025, Salesforce, Inc.
 * SPDX-License-Identifier: Apache-2
 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * Shared "label / value" detail block printer for `*Get` style commands.
 *
 * Many commands render the same two-column "Title / dashes / label-value
 * pairs" block via cliui. This helper centralizes the layout so individual
 * commands only need to provide the title and field list.
 *
 * @module cli/details
 */
import { ux } from '@oclif/core';
import cliui from 'cliui';
function normalizeField(field) {
    return Array.isArray(field) ? { label: field[0], value: field[1] } : field;
}
function renderFields(ui, fields, labelWidth) {
    for (const raw of fields) {
        const { label, value } = normalizeField(raw);
        if (value === undefined || value === null)
            continue;
        ui.div({ text: `${label}:`, width: labelWidth, padding: [0, 2, 0, 0] }, { text: String(value), padding: [0, 0, 0, 0] });
    }
}
/**
 * Renders a "details" block to stdout: a title, a separator, then a column
 * of `label: value` rows. Optional named sections can be rendered after the
 * primary block.
 *
 * @param title - Heading rendered above the fields.
 * @param fields - Primary list of label / value rows. Rows whose value is
 *   `undefined` are skipped.
 * @param options - Rendering options including optional sub-sections.
 *
 * @example
 * ```typescript
 * printFieldsBlock('User Details', [
 *   ['Login', user.login],
 *   ['Email', user.email],
 *   ['Disabled', user.disabled?.toString()],
 * ], {
 *   sections: user.roles?.length
 *     ? [{title: 'Roles', fields: user.roles.map((r) => [r, ''] as DetailField)}]
 *     : [],
 * });
 * ```
 */
export function printFieldsBlock(title, fields, options = {}) {
    const labelWidth = options.labelWidth ?? 25;
    const separatorWidth = options.separatorWidth ?? 50;
    const termWidth = options.termWidth ?? process.stdout.columns ?? 80;
    const ui = cliui({ width: termWidth });
    ui.div({ text: title, padding: [1, 0, 0, 0] });
    ui.div({ text: '─'.repeat(separatorWidth), padding: [0, 0, 0, 0] });
    renderFields(ui, fields, labelWidth);
    for (const section of options.sections ?? []) {
        ui.div({ text: '', padding: [1, 0, 0, 0] });
        ui.div({ text: section.title, padding: [0, 0, 0, 0] });
        ui.div({ text: '─'.repeat(separatorWidth), padding: [0, 0, 0, 0] });
        if (section.fields) {
            renderFields(ui, section.fields, labelWidth);
        }
        for (const line of section.lines ?? []) {
            ui.div({ text: line, padding: [0, 0, 0, 0] });
        }
    }
    ux.stdout(ui.toString());
}
//# sourceMappingURL=details.js.map