<?php

namespace PHPNative\Ui\Widget;

use PHPNative\Framework\TextRenderer;
use PHPNative\Ui\Component;

class Separator extends Component
{
    private bool $horizontal;

    public function __construct(
        bool $horizontal = true,
        string $style = '',
    ) {
        $this->horizontal = $horizontal;

        // Default styles based on orientation
        $defaultStyle = $horizontal
            ? 'bg-gray-300 h-0.5 w-full my-1'
            : 'bg-gray-300 w-0.5 h-full mx-1';

        parent::__construct($defaultStyle . ' ' . $style);
    }

    public function layout(null|TextRenderer $textRenderer = null): void
    {
        parent::layout($textRenderer);

        // Apply explicit height/width from styles
        $height = $this->computedStyles[\PHPNative\Tailwind\Style\Height::class] ?? null;
        $width = $this->computedStyles[\PHPNative\Tailwind\Style\Width::class] ?? null;

        if ($this->horizontal) {
            // For horizontal separator, enforce the height from style (default h-0.5 = 2px)
            if ($height) {
                $this->viewport->height = (int) $height->value;
            } elseif ($this->viewport->height < 1) {
                $this->viewport->height = 2; // Default 2px
            }
            $this->contentViewport->height = $this->viewport->height;
        } else {
            // For vertical separator, enforce the width from style (default w-0.5 = 2px)
            if ($width) {
                $this->viewport->width = (int) $width->value;
            } elseif ($this->viewport->width < 1) {
                $this->viewport->width = 2; // Default 2px
            }
            $this->contentViewport->width = $this->viewport->width;
        }
    }
}
