<?php
namespace nur\sery\wip\schema\_list;

use nulib\ref\schema\ref_schema;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\Wrapper;

class ListSchema extends Schema {
  /** @var array meta-schema d'un schéma de nature liste */
  const METASCHEMA = ref_schema::LIST_METASCHEMA;

  /**
   * indiquer si $definition est une définition de schéma de nature liste que
   * {@link normalize()} pourrait normaliser
   */
  static function isa_definition($definition): bool {
    if (!is_array($definition)) return false;
    # nature explicitement spécifiée
    if (self::have_nature($definition, $nature)) {
      return $nature === "list";
    }
    # un unique élément tableau à l'index 0
    $count = count($definition);
    $haveIndex0 = array_key_exists(0, $definition);
    return $count == 1 && $haveIndex0 && is_array($definition[0]);
  }

  static function normalize($definition, $definitionKey=null): array {
    if (!is_array($definition)) $definition = [$definition];
    if (!self::have_nature($definition)) {
      $definition = [
        "?array",
        "" => "list",
        "schema" => $definition[0],
      ];
    }
    self::_normalize($definition, $definitionKey);
    self::_ensure_nature($definition, "list", "array");
    return $definition;
  }

  function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
    if ($definition === null) $definition = static::SCHEMA;
    if ($normalize) {
      $definition = self::normalize($definition, $definitionKey);
      $this->_definition = $definition;
      self::_ensure_type($definition);
      self::_ensure_schema_instances($definition);
    }
    $this->definition = $definition;
  }

  function isList(?ListSchema &$schema=null): bool {
    $schema = $this;
    return true;
  }

  protected function newWrapper(): ListWrapper {
    return new ListWrapper($this);
  }

  function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): ListWrapper {
    if (!($wrapper instanceof ListWrapper)) $wrapper = $this->newWrapper();
    return $wrapper->reset($value, $valueKey);
  }
}
