<?php
namespace nulib\schema\_assoc;

use nulib\cl;
use nulib\ref\schema\ref_schema;
use nulib\schema\Schema;
use nulib\schema\Wrapper;
use nulib\ValueException;

/**
 * Class AssocSchema
 */
class AssocSchema extends Schema {
  /**
   * indiquer si $definition est une définition de schéma de nature tableau
   * associatif que {@link normalize_definition()} 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 === "assoc";
    }
    # tableau associatif
    return !cl::have_num_keys($definition);
  }

  static function normalize_definition($definition, $definitionKey=null): array {
    if (!is_array($definition)) $definition = [$definition];
    if (!self::have_nature($definition)) {
      $definition = [
        "?array",
        "" => "assoc",
        "schema" => $definition,
      ];
    }
    $natureMetaschema = array_merge(ref_schema::NATURE_METASCHEMA, ref_schema::ASSOC_NATURE_METASCHEMA);
    self::_normalize_definition($definition, $definitionKey, $natureMetaschema);
    self::_ensure_nature($definition, "assoc", "array");
    return $definition;
  }

  function __construct($definition=null, $definitionKey=null, bool $normalize=true) {
    if ($definition === null) $definition = static::SCHEMA;
    if ($normalize) {
      $definition = self::normalize_definition($definition, $definitionKey);
      $this->_definition = $definition;
      self::_ensure_type($definition);
      self::_ensure_schema_instances($definition);
    } else {
      # ici, $definition contient un schema déjà instancié, mais c'est le mieux
      # qu'on puisse faire
      $this->_definition = $definition;
    }
    $this->definition = $definition;
    $keys = [];
    foreach ($definition["schema"] as $key => $schema) {
      if (!$schema["computed"]) $keys[] = $key;
    }
    $this->keys = $keys;
  }

  protected array $keys;

  function getKeys(): array {
    return $this->keys;
  }

  function getSchema($key=false): Schema {
    if ($key === null || $key === false) return $this;
    $schema = $this->definition["schema"][$key] ?? null;
    if ($schema === null) throw ValueException::invalid_key($key);
    return $schema;
  }

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

  function getWrapper(&$value=null, $valueKey=null, ?array $params=null, ?Wrapper &$wrapper=null): AssocWrapper {
    # si pas de valeur ni de wrapper, pas d'analyse et donc pas d'exception
    # cf le code similaire dans AssocWrapper::__construct()
    $dontAnalyze = $value === null && $wrapper === null;
    if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper();

    # la nature du schéma peut contenir des paramètres par défaut
    $nature = $this->definition[""];
    foreach (array_keys(ref_schema::ASSOC_PARAMS_SCHEMA) as $paramKey) {
      $paramValue = $nature[$paramKey] ?? null;
      if ($paramValue !== null) $params[$paramKey] = $paramValue;
    }
    if ($params !== null) $wrapper->resetParams($params);

    return $wrapper->reset($value, $valueKey, $dontAnalyze? ["analyze" => false]: null);
  }
}
