<?php

namespace Modules\Reception\Outil;

class DatatableColumn
{
    /** @var string $key */
    private string $key;

    /** @var string $label */
    private string $label;

    /** @var mixed $attr */
    private $attr;

    /** @var mixed $fn */
    private $fn;

    /**
     * @param string $key
     * @param string $label
     * @param array<mixed> $attr
     */
    public function __construct(
        string $key,
        string $label,
        array  $attr = []
    ) {
        $this->key = $key;
        $this->label = $label;
        $this->attr = $attr;
        $this->fn = null;
    }

    /**
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }

    /**
     * @return array<mixed>
     */
    public function getHtmlAttr()
    {
        return $this->attr;
    }

    public function setRowCallback(?callable $fn): self
    {
        $this->fn = $fn;
        return $this;
    }

    /**
     * @param mixed $row
     * @param mixed $data
     * @param mixed $key
     * @return mixed
     */
    public function renderRow($row, $data, $key)
    {
        if ($this->fn == null) {
            return $data;
        }

        $fn = $this->fn;
        return $fn($row, $data, $key);
    }
}
