<?php
namespace nur\v\bs3\fo;

use nur\A;
use nur\b\params\IParametrable;
use nur\b\params\Tparametrable1;
use nur\base;
use nur\v\base\ComponentPrintable;
use nur\v\vo;

abstract class Control extends ComponentPrintable implements IParametrable {
  use Tparametrable1;

  /** @var bool faut-il automatiquement créer l'id à partir de name? */
  const AUTOID = true;

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "id" => ["?string", null, "identifiant"],
    "name" => ["?string", null, "nom de l'élément"],
    "value" => ["?string", null, "valeur de l'élément"],
    "attrs" => ["?array", null, "attributs HTML génériques"],
  ];

  function __construct(Form $form, ?array $params=null) {
    $this->form = $form;
    [$params, $attrs] = $this->splitParametrableParams($params);
    if ($attrs) A::merge($params["attrs"], $attrs);
    $this->initParametrableParams($params);
    if (static::AUTOID) {
      base::update_n($this->ppId, $this->ppName);
    }
  }

  /** @var Form */
  protected $form;

  /** @var ?string */
  protected $ppId;

  /** @var ?string */
  protected $ppName;

  /** @var array|string */
  protected $ppValue;

  /** @var ?array */
  protected $ppAttrs;

  abstract function getControl(): array;

  function print(): void {
    vo::write($this->getControl());
  }
}
