<?php
namespace nulib\schema\types;

use nulib\cl;
use nulib\schema\_scalar\ScalarSchema;
use nulib\schema\Result;
use nulib\schema\Schema;

class tarray extends _tstring {
  const NAME = "array";

  const SPLIT_PATTERN = '/\s+/';
  const FORMAT = " ";

  public static function get_params_from_definition(?array $definition): ?array {
    $params = parent::get_params_from_definition($definition);
    $splitPattern = $definition["split_pattern"] ?? null;
    if ($splitPattern !== null) $params["split_pattern"] = $splitPattern;
    $format = $definition["format"] ?? null;
    if ($format !== null) $params["format"] = $format;
    return $params;
  }

  static function ensure_array(&$array): void {
    if (!is_array($array)) $array = cl::with($array);
  }

  static function ensure_narray(&$array): void {
    if ($array !== null) self::ensure_array($array);
  }

  function getClass(): string {
    return "array";
  }

  function getNullValue() {
    return $this->nullable? null: [];
  }

  function isValid($value, ?bool &$normalized=null): bool {
    $normalized = is_array($value);
    return $normalized || is_scalar($value);
  }

  function parse(string $value) {
    $pattern = $this->params["split_pattern"] ?? static::SPLIT_PATTERN;
    return preg_split($pattern, $value);
  }

  /**
   * @var ScalarSchema $schema
   */
  function normalize(&$value, Result $result, Schema $schema): bool {
    if ($result->normalized) {
    } elseif (is_array($value)) {
      $result->setNormalized();
    } elseif (is_scalar($value)) {
      $value = cl::with($value);
      return true;
    }
    return false;
  }

  function format($value, $format=null): string {
    if ($value === null) return "";
    $format ??= $this->params["format"] ?? static::FORMAT;
    return implode($format, $value);
  }
}
