<?php
namespace nur\mapper\base;

use nur\b\ValueException;
use nur\func;
use Traversable;

class producer_utils {
  static function ensure_producer($producer, ?array $args=null): iterable {
    if (is_array($producer) &&
      array_key_exists(0, $producer) &&
      is_subclass_of($producer[0], Producer::class)) {
      $producer = array_merge($producer, $args);
      return func::cons(...$producer);
    }
    if (is_iterable($producer)) return $producer;
    if (is_string($producer) && is_subclass_of($producer, Traversable::class)) {
      if ($args === null) $args = [];
      return func::cons($producer, ...$args);
    }
    throw ValueException::unexpected_type(Producer::class, $producer);
  }
}