<?php
namespace PrestaShop\Module\PolarisPrestaTheme\Config;

use Category;
use Context;
use Link;

class ConfigMenuParent
{
    public $menu;  // int ou array enrichi
    public $isVisible = true;
  
    
 

    /**
     * enrichit le menu avec les données de la catégorie
     */
    public function enrich(): void
    {
        // Préserve les colonnes existantes
        $existingColumns = [];
        if (is_array($this->menu) && isset($this->menu['columns'])) {
            $existingColumns = $this->menu['columns'];
        }

        if ($this->menu_type === 'custom') {
            $customId =  hexdec(uniqid());

            $this->menu = [
                'id'      => $customId,
                'name'    => $this->custom_label,
                'image'   => "",
                'url'     => $this->custom_url ,
                'type' => $this->menu_type ,
                'columns' => $existingColumns,
            ];
            
        }else{

            // Mode category (comportement original)
            $link     = new Link();
            $id_lang  = Context::getContext()->language->id;
            $menuId   = is_array($this->menu) ? $this->menu['id'] : (int) $this->menu;
            $category = new Category($menuId, $id_lang);

            $this->menu = [
                'id'      => $category->id,
                'name'    => $category->name,
                'url'     => $link->getCategoryLink($category),
                'type'    => 'category',
                'columns' => $existingColumns,
            ];
            unset($this->custom_label, $this->custom_url , $this->image, $this->menu_type);

        }

        
        
    }

    public function addColumn(array $column): void
    {
        if (!isset($this->menu['columns'])) {
            $this->menu['columns'] = [];
        }
        $this->menu['columns'][] = $column;
    }
}
