<?php
namespace nur\mapper\base;

use nur\A;
use nur\b\ValueException;
use nur\base;
use nur\func;
use nur\mapper\base\oobd\IOobdManager;
use nur\str;
use ReflectionClass;

class mapper_utils {
  static function ensure_mapper_class($mapper) {
    if ($mapper instanceof Mapper) return $mapper;
    if (is_callable($mapper)) return $mapper;
    if (is_string($mapper) && is_subclass_of($mapper, Mapper::class)) {
      return $mapper;
    }
    if (is_array($mapper) &&
      array_key_exists(0, $mapper) &&
      is_subclass_of($mapper[0], Mapper::class)) {
      return $mapper;
    }
    throw ValueException::unexpected_type(Mapper::class, $mapper);
  }

  static function ensure_mapper($mapper, iterable $iterator): Mapper {
    if ($mapper instanceof Mapper) {
      if ($iterator !== null) $mapper->setSource($iterator);
    } elseif (is_callable($mapper)) {
      $mapper = new FuncMapper($mapper, $iterator);
    } else {
      if (!is_array($mapper)) $mapper = [$mapper];
      [$args, $params] = A::split_assoc($mapper);
      if ($args === null) throw new ValueException("mapper class is required");
      $c = new ReflectionClass($args[0]);
      $rf = $c->getConstructor();
      if ($rf !== null && !$rf->isVariadic()) {
        # NB: $maxArgs ne doit pas tenir compte du dernier argument $source,
        # mais comme le premier élément de $args est la classe, ça colle
        $maxArgs = $rf->getNumberOfParameters();
        $args = array_slice($mapper, 0, $maxArgs);
        while (count($args) < $maxArgs) {
          $args[] = null;
        }
      }
      $args[] = $iterator;
      $mapper = func::cons(...$args);
      if ($params !== null) $mapper->setParametrableParams($params);
    }
    if ($iterator instanceof IOobdManager) {
      # récupérer le gestionnaire partagé le cas échéant
      $sharedOobdManager = $iterator->getSharedOobdManager();
      if ($sharedOobdManager !== null) $mapper->setSharedOobdManager($sharedOobdManager);
    }
    return $mapper;
  }

  static function assemble_mappers(array $mappers, iterable $iterator): iterable {
    foreach ($mappers as $mapper) {
      $iterator = self::ensure_mapper($mapper, $iterator);
    }
    return $iterator;
  }

  public static function check_prefix(string $value, ?string &$params, string ...$prefixes): bool {
    foreach ($prefixes as $prefix) {
      if (preg_match('/^[A-Za-z0-9_]+$/', $prefix)) {
        # correspondance exacte pour une commande alpha-numérique
        if ($value == $prefix) {
          $params = null;
          return true;
        }
      } elseif (str::_starts_with($prefix, $value)) {
        $params = str::without_prefix($prefix, $value);
        return true;
      }
    }
    return false;
  }

  static function split_param(string $param, ?string &$name, ?string &$type, ?string &$value): bool {
    if (preg_match('/^([A-Za-z0-9_]*)(?::([^=]+))?(?:=(.*))?$/', $param, $vs)) {
      $name = base::vn(A::get($vs, 1));
      $type = base::vn(A::get($vs, 2));
      $value = base::vn(A::get($vs, 3));
      return true;
    }
    return false;
  }
}