<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class WrapTextComponent extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct(
        public string $text,
        public int $width = 50,
        public int $max = 50,
        public string $dotTriple = '...'
    ) {
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        $text = $this->text;
        $width = $this->width;
        $max = $this->max;
        $dotTriple = $this->dotTriple;

        $wrappedString = wordwrap($text, $width, "\n");
        $wrappedString = str_replace("\n", " ", $wrappedString);
        $truncatedString = substr($wrappedString, 0, $max);

        $tripleKill =  strlen($text) > $max ? $dotTriple : '';
        $isToolTip = '';

        if ($tripleKill) {
            // handle quadra kill
            $isToolTip = "data-tooltip=\"$text\"";
        }

        return "<span $isToolTip>" . $truncatedString . $tripleKill . '</span>';
    }
}
