<?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')
 * - persona_control_html: string raw HTML for custom persona control (optional)
 * - gender_control_html: string raw HTML for custom gender control (deprecated, use persona_control_html instead)
 *
 */
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 persona selection if enabled.
        if (!isset($opts['persona_control_html']) && !isset($opts['gender_control_html']) && \mh_cfg('MAILBEEZ_FILTER_ADD_GENDER_STATUS') === 'True') {
            $opts['persona_control_html'] = \Components\Ui\EmailEditor::renderPreviewControls(0, [
                'gender_var' => 'gender',
                'lng_var'    => 'languageId',
                'apply_js'   => 'changeGender();',
            ]);
        }

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