<?php

namespace Components\Ui;

use Components\Base;
use Components\Support\Devices;

/**
 * EmailPreviewer page component.
 *
 * Renders the email preview UI (URL field, device buttons, mockups, iframes)
 * as a reusable component. All inline HTML is captured with ob_start.
 *
 * Options:
 * - url: string initial URL to load (optional)
 * - device: 'desktop'|'tablet'|'mobile' (default 'desktop')
 * - id_prefix: string unique prefix for element ids to avoid collisions (default 'ep')
 * - gender_control_html: string raw HTML for custom gender control (optional)
 *
 */
class EmailPreviewer
{
    public static function render(array $opts = []): string
    {
        // Backwards compatible wrapper: keep `EmailPreviewer` as the public API
        // while delegating the implementation to `DevicePreviewer`.
        $opts['mode'] = 'email';

        // Refactor: use `EmailEditor::renderPreviewControls` for gender selection if enabled.
        if (!isset($opts['gender_control_html'])) {
            $opts['gender_control_html'] = \Components\Ui\EmailEditor::renderPreviewControls(0, [
                'gender_var' => 'gender',
                'lng_var'    => 'languageId',
                'apply_js'   => 'changeGender();',
                'show_label' => true,
            ]);
        }

        return DevicePreviewer::render($opts);
    }
}
