<?php

namespace App\View\Components\Common;

use Illuminate\View\Component;

class Table extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */

    public $headers;
    public $tableClass;
    public $headerClass;
    public function __construct( $headers = [], $tableClass = '', $headerClass = '' )
    {

        $this->headers = $this->formatHeaders($headers);
        $this->tableClass = $tableClass;
        $this->headerClass = $headerClass;
    }

    private function formatHeaders($headers = []){

        return array_map( function ($headers){

            $name = is_array($headers) ? $headers['name'] : $headers;

            return [
                "name"      => $name,
                "align"     => $this->formatAlignClasses($headers['align'] ?? 'left')
            ];

        }, $headers);
    }

    private function formatAlignClasses($align){
        
        //lookup table
        return [
            'left'     => 'text-left',
            'right'    => 'text-right',
            'center'   => 'text-center',

        ][$align] ?? 'text-left';
    }
    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.common.table');
    }
}
