<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Ui\Component\DropdownButton;
use Plainwork\Ui\Component\HtmlElement;

final class DropdownButtonRenderer
{
    public function __invoke(DropdownButton $el)
    {
        $summary = $el->getSummary();

        $js = "
const trg=this.closest('div').querySelector('[data-pw-dropdown-content]');
trg.style.display=('none'==trg.style.display) ? '' : 'none';";
        $retSummary = (new HtmlElement('button', $summary))
            ->withAttr('onclick', $js)
        ;
        $retContent = (new HtmlElement('div', $el->getContent()))
            ->withAttr('data-pw-dropdown-content', true)
            ->withAttr('style', ['display' => 'none', 'margin-top' => '.5em'])
            ;

        $ret = (new HtmlElement('div'))
            ->append($retSummary)
            ->append($retContent)
            ;

        // $ret->withAttr('class', 'pw-details-card');

        $attr = $el->getAttr();
        foreach ($attr as $k => $v) {
            $ret->withAttr($k, $v);
        }

        return $ret;
    }
}
