<?php
declare(strict_types=1);

namespace WP_AUX\Entities\AuxFramework\Partials;

use WP_AUX\Entities\AuxFramework\Partials\Traits\SectionTraits;

/**
 * AUX section entity
 * @since 1.0.0
 */
class SectionEntity
{
  use SectionTraits;

  protected ?SectionIntroEntity $sectionIntro = null;

  /**
   * Set the section intro for the section
   * @since 1.0.0
   */
  public function setSectionIntro(SectionIntroEntity $sectionIntro): static
  {
    $this->sectionIntro = $sectionIntro;
    return $this;
  }

  /**
   * Get if the section has an intro that will render
   *
   * Section intros that do not having a heading will not render. Useful for determining
   * what level of heading needed for content inside the section.
   *
   * @since 1.0.1
   */
  public function hasIntro(): bool
  {
    return !empty($this->sectionIntro) && $this->sectionIntro->willRender();
  }

  public function __toString(): string
  {
    /**
     * If the section is empty, then don't render anything.
     */
    if (empty($this->content)) {
      return '';
    }

    $divider = $this->sectionClasses->hasDivider() ? '<span class="divider-dots gray-300"></span>' : '';

    /**
     * Prepare the section's attributes and add the base section classes and ID (if provided)
     */
    $sectionAttributes = clone $this->sectionAttributes;
    $sectionAttributes->addClass((string) $this->sectionClasses);

    if ($this->id) {
      $sectionAttributes->setAttribute('id', esc_attr($this->id));
    }

    /**
     * Prepare the component's attributes and add the base component classes
     */
    $componentAttributes = clone $this->componentAttributes;
    $componentAttributes->addClass((string) $this->componentClasses);

    $output = <<<HTML
      <section {$sectionAttributes}>
        $divider
        <div {$componentAttributes}>
          {$this->sectionIntro}
          {$this->content}
        </div>
      </section>
    HTML;

    return $output;
  }
}
