<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Ui\Component\DetailsCard;
use Plainwork\Ui\Component\Collection;
use Plainwork\Ui\Component\HtmlElement;

final class DetailsCardRenderer
{
    public function __invoke(DetailsCard $el)
    {
        $summary = $el->getSummary();
        $content = $el->getContent();

        $elSummary = (new HtmlElement('summary', $summary))
            ->withAttr('class', 'pw-details-card_summary')
            ;

        $elContent = (new HtmlElement('div', $content))
            ->withAttr('class', 'pw-details-card_content')
            ;

        $ret = (new HtmlElement('details', $elSummary, $elContent))
            ->withAttr('class', 'pw-details-card')
            ;

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

        $css = '
<style>
#pw2 .pw-details-card {list-style: none; display: block;}
#pw2 .pw-details-card_summary {display: flex; gap: .5em; justify-content: start; align-items: center; }
#pw2 .pw-details-card_summary::after {
  content: "";
  border: solid;
  border-width: 0 .125rem .125rem 0;
  display: inline-block;
  padding: .2rem;
  transition: 0.3s;
  transform: rotate(45deg);
}
#pw2 .pw-details-card_content {margin-top: .5em;}
#pw2 details[open] .pw-details-card_summary::after {transform: rotate(-135deg);}
</style>
';

        $ret = new Collection($ret, $css);

        return $ret;






        $retSummary = new HtmlElement('summary');
        if (is_object($summary) && method_exists($summary, 'getAttr')) {
            $attr = $summary->getAttr();
            foreach ($attr as $k => $v) {
                $retSummary->withAttr($k, $v);
                $summary->removeAttr($k);
            }
        }
        $retSummary->append($summary);

        $ret = (new HtmlElement('details'))
            ->append($retSummary)
            ->append(new HtmlElement('div', $el->getContent()))
            ;

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

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

        return $ret;
    }
}
