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

use nur\sery\cl;
use nur\sery\ref\schema\ref_schema;
use nur\sery\wip\schema\Schema;
use nur\sery\wip\schema\Value;

/**
 * Class AssocSchema
 */
class AssocSchema extends Schema {
  /** @var array meta-schema d'un schéma de nature tableau associatif */
  const METASCHEMA = ref_schema::ASSOC_METASCHEMA;

  /**
   * indiquer si $definition est une définition de schéma de nature tableau
   * associatif que {@link normalize()} pourrait normaliser
   */
  static function isa_definition($definition): bool {
    if (!is_array($definition)) return false;
    # nature explicitement spécifiée
    if (array_key_exists("", $definition)) {
      $nature = $definition[""];
      if ($nature === "assoc") return true;
      if (is_array($nature)
        && array_key_exists(0, $nature)
        && $nature[0] === "assoc") {
        return true;
      }
      return false;
    }
    # un tableau associatif
    return !cl::have_num_keys($definition);
  }

  static function normalize($definition, $definitionKey=null): array {
  }

  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;
  }

  function isAssoc(?AssocSchema &$assoc=null): bool {
    $assoc = $this;
    return true;
  }

  function newValue(?Value &$destv=null, &$dest=null, $destKey=null): AssocValue {
    if ($destv instanceof AssocValue) return $destv->reset($dest, $destKey);
    else return ($destv = new AssocValue($this, $dest, $destKey));
  }
}
