<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Form;

final class DesignerFormLayout
{
    public function __construct(
        private readonly string $layout = 'one_column',
        private readonly int $columns = 1,
        private readonly array $breakpoints = [],
        private readonly int $gap = 12
    ) {}

    public static function fromName(string $layout): self
    {
        return match ($layout) {
            'two_column', '2-column', '2' => new self('two_column', 2, ['xs' => 1, 'sm' => 1, 'md' => 2, 'lg' => 2], 12),
            'three_column', '3-column', '3' => new self('three_column', 3, ['xs' => 1, 'sm' => 1, 'md' => 2, 'lg' => 3], 12),
            'four_column', '4-column', '4' => new self('four_column', 4, ['xs' => 1, 'sm' => 2, 'md' => 2, 'lg' => 4], 12),
            default => new self('one_column', 1, ['xs' => 1, 'sm' => 1, 'md' => 1, 'lg' => 1], 12),
        };
    }

    public function columns(): int { return $this->columns; }

    public function fieldSpan(): int
    {
        return max(1, intdiv(12, max(1, $this->columns)));
    }

    public function toArray(): array
    {
        return [
            'layout' => $this->layout,
            'columns' => $this->columns,
            'fieldSpan' => $this->fieldSpan(),
            'breakpoints' => $this->breakpoints,
            'gap' => $this->gap,
        ];
    }
}
