<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Account;

use Closure;
use Illuminate\Http\Request;

final class AccountTab
{
    private string $label;

    private int $order = 50;

    private ?Closure $data = null;

    private function __construct(
        private readonly string $key,
        private readonly string $component,
    ) {
        $this->label = $key;
    }

    public static function make(string $key, string $component): self
    {
        return new self($key, $component);
    }

    public function label(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function order(int $order): self
    {
        $this->order = $order;

        return $this;
    }

    public function data(Closure $data): self
    {
        $this->data = $data;

        return $this;
    }

    public function getKey(): string
    {
        return $this->key;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function getComponent(): string
    {
        return $this->component;
    }

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

    /** @return array<string, mixed> */
    public function getData(Request $request): array
    {
        if ($this->data === null) {
            return [];
        }

        return ($this->data)($request);
    }

    /** @return array<string, mixed> */
    public function toArray(Request $request): array
    {
        return [
            'key' => $this->key,
            'label' => $this->label,
            'component' => $this->component,
            'order' => $this->order,
            'data' => $this->getData($request),
        ];
    }
}
