<?php

declare(strict_types=1);

/**
 * @param array<string, mixed> $attrs
 */
function myronAdminFormKitAttrs(array $attrs): string
{
    $html = '';
    foreach ($attrs as $name => $value) {
        $name = trim((string) $name);
        if ($name === '' || $value === false || $value === null) {
            continue;
        }
        if ($value === true) {
            $html .= ' ' . myronEscape($name);
            continue;
        }
        $html .= ' ' . myronEscape($name) . '="' . myronEscape((string) $value) . '"';
    }
    return $html;
}

/**
 * @param array<string, mixed> $options
 */
function myronAdminFormField(string $label, string $controlHtml, array $options = []): string
{
    $classes = ['sg-field'];
    if (!empty($options['full'])) {
        $classes[] = 'sg-field--full';
    }
    if (!empty($options['checkbox'])) {
        $classes[] = 'sg-field--checkbox';
    }
    foreach (array_filter(array_map('trim', explode(' ', (string) ($options['class'] ?? '')))) as $class) {
        $classes[] = $class;
    }

    $attrs = is_array($options['attrs'] ?? null) ? $options['attrs'] : [];
    $hint = trim((string) ($options['hint'] ?? ''));

    $html = [];
    $html[] = '<label class="' . myronEscape(implode(' ', array_unique($classes))) . '"' . myronAdminFormKitAttrs($attrs) . '>';
    $html[] = '<span class="sg-field__label">' . myronEscape($label) . '</span>';
    $html[] = $controlHtml;
    if ($hint !== '') {
        $html[] = '<span class="sg-field__hint">' . myronEscape($hint) . '</span>';
    }
    $html[] = '</label>';
    return implode("\n", $html);
}

/**
 * @param array<string, mixed> $attrs
 * @param array<string, mixed> $options
 */
function myronAdminFormInput(string $label, array $attrs, array $options = []): string
{
    $attrs['class'] = trim('sg-field__control ' . (string) ($attrs['class'] ?? ''));
    return myronAdminFormField($label, '<input' . myronAdminFormKitAttrs($attrs) . '>', $options);
}

/**
 * @param array<string, string> $choices
 * @param array<string, mixed> $attrs
 * @param array<string, mixed> $options
 */
function myronAdminFormSelect(string $label, array $choices, string $selected, array $attrs, array $options = []): string
{
    $attrs['class'] = trim('sg-field__control ' . (string) ($attrs['class'] ?? ''));
    $html = ['<select' . myronAdminFormKitAttrs($attrs) . '>'];
    foreach ($choices as $value => $choiceLabel) {
        $optionAttrs = ['value' => (string) $value];
        if ((string) $value === $selected) {
            $optionAttrs['selected'] = true;
        }
        $html[] = '<option' . myronAdminFormKitAttrs($optionAttrs) . '>' . myronEscape((string) $choiceLabel) . '</option>';
    }
    $html[] = '</select>';
    return myronAdminFormField($label, implode("\n", $html), $options);
}

/**
 * @param array<string, mixed> $attrs
 * @param array<string, mixed> $options
 */
function myronAdminFormTextarea(string $label, string $value, array $attrs, array $options = []): string
{
    $attrs['class'] = trim('sg-field__control sg-field__control--textarea ' . (string) ($attrs['class'] ?? ''));
    return myronAdminFormField($label, '<textarea' . myronAdminFormKitAttrs($attrs) . '>' . myronEscape($value) . '</textarea>', ['full' => $options['full'] ?? true] + $options);
}

/**
 * @param array<string, mixed> $attrs
 * @param array<string, mixed> $options
 */
function myronAdminFormCheckbox(string $label, string $copy, array $attrs, array $options = []): string
{
    $attrs['type'] = 'checkbox';
    $control = '<span class="sg-field__checkbox"><input' . myronAdminFormKitAttrs($attrs) . '><span>' . myronEscape($copy) . '</span></span>';
    return myronAdminFormField($label, $control, ['checkbox' => true] + $options);
}

/**
 * @param array<string, mixed> $options
 */
function myronAdminFormSectionStart(string $title, string $description = '', array $options = []): string
{
    $attrs = is_array($options['attrs'] ?? null) ? $options['attrs'] : [];
    $gridClass = 'mc-admin-form__grid';
    if (($options['columns'] ?? '') === 3 || ($options['columns'] ?? '') === 'three') {
        $gridClass .= ' mc-admin-form__grid--three';
    }
    if (!empty($options['compact'])) {
        $gridClass .= ' mc-admin-form__grid--compact';
    }

    $html = [];
    $html[] = '<section class="mc-admin-form__section"' . myronAdminFormKitAttrs($attrs) . '>';
    $html[] = '<div class="mc-admin-form__section-header">';
    $html[] = '<h3 class="mc-admin-form__section-title">' . myronEscape($title) . '</h3>';
    if ($description !== '') {
        $html[] = '<p class="mc-admin-form__section-description">' . myronEscape($description) . '</p>';
    }
    $html[] = '</div>';
    $html[] = '<div class="' . myronEscape($gridClass) . '">';
    return implode("\n", $html);
}

function myronAdminFormSectionEnd(): string
{
    return "</div>\n</section>";
}

/**
 * @param list<array{key:string,label:string,attrs?:array<string,mixed>,active?:bool}> $tabs
 */
function myronAdminFormStepTabs(array $tabs, string $label): string
{
    $html = ['<nav class="mc-workspace__tabs" aria-label="' . myronEscape($label) . '">'];
    foreach ($tabs as $tab) {
        $attrs = is_array($tab['attrs'] ?? null) ? $tab['attrs'] : [];
        $attrs['type'] = 'button';
        $attrs['class'] = 'mc-workspace__tab' . (!empty($tab['active']) ? ' mc-workspace__tab--active is-active' : '');
        $html[] = '<button' . myronAdminFormKitAttrs($attrs) . '>' . myronEscape((string) $tab['label']) . '</button>';
    }
    $html[] = '</nav>';
    return implode("\n", $html);
}
