<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DataGrid;

final class DesignerDataGridToolbar
{
    public function __construct(
        private readonly bool $showSearch = true,
        private readonly bool $showAddNew = true,
        private readonly bool $showImport = true,
        private readonly bool $showExport = true,
        private readonly bool $showRefresh = true,
        private readonly bool $showSettings = true,
        private readonly array $items = []
    ) {}

    public static function defaults(): self
    {
        return new self(items: [
            DesignerDataGridAction::create('add_new', 'Add New', 'toolbar')->toArray(),
            DesignerDataGridAction::create('import', 'Import', 'toolbar')->toArray(),
            DesignerDataGridAction::create('export', 'Export', 'toolbar')->toArray(),
            DesignerDataGridAction::create('refresh', 'Refresh', 'toolbar')->toArray(),
            DesignerDataGridAction::create('settings', 'Settings', 'toolbar')->toArray(),
        ]);
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (bool) ($data['showSearch'] ?? true),
            (bool) ($data['showAddNew'] ?? true),
            (bool) ($data['showImport'] ?? true),
            (bool) ($data['showExport'] ?? true),
            (bool) ($data['showRefresh'] ?? true),
            (bool) ($data['showSettings'] ?? true),
            (array) ($data['items'] ?? self::defaults()->toArray()['items'])
        );
    }

    public function toArray(): array
    {
        return [
            'showSearch' => $this->showSearch,
            'showAddNew' => $this->showAddNew,
            'showImport' => $this->showImport,
            'showExport' => $this->showExport,
            'showRefresh' => $this->showRefresh,
            'showSettings' => $this->showSettings,
            'items' => $this->items,
        ];
    }
}
