<?php
namespace nur\io\fsv;

use nur\A;
use nur\b\io\Tfilter;
use nur\b\params\Parametrable;
use nur\b\params\Tparametrable;
use nur\b\ValueException;
use nur\io\Ttmpwriter;

/**
 * Class FsvWriter: écrire un flux de données au format FSV dans un fichier
 * destination
 *
 * --autogen-properties-and-methods--
 * @method setOutput($value)
 * @method array setSchema(array $value)
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 */
class FsvWriter extends Parametrable {
  use Tparametrable, Ttmpwriter, Tfilter;

  function __construct($output=null, ?array $params=null) {
    A::set_nn($params, "output", $output);
    $this->schema = new FsvSchema();
    parent::__construct($params);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "output" => [null, null, "fichier en sortie"],
    "schema" => ["array", null, "schéma des données"],
    "input_encoding" => ["?string", null, "encoding en entrée"],
    "output_encoding" => ["?string", null, "encoding en sortie"],
  ];

  /** @var FsvSchema */
  protected $schema;

  function pp_setSchema($schema): void {
    if ($schema instanceof FsvSchema) $this->schema = $schema;
    elseif (is_array($schema)) $this->schema->setSchema($schema);
    elseif ($schema !== null) throw ValueException::invalid_value($schema, "schema");
  }
  function pp_setInputEncoding($inputEncoding): void {
    $this->schema->setDataEncoding($inputEncoding);
  }
  function pp_setOutputEncoding($outputEncoding): void {
    $this->schema->setOutputEncoding($outputEncoding);
  }

  protected function _formatLine(array $row): string {
    return $this->schema->formatRow($row);
  }

  function writeAll(iterable $items): self {
    $this->schema->checkSchema();
    $writer = $this->ensureWriter(true);
    $resource = $writer->getResource();
    if ($resource !== null) {
      foreach ($items as $value) {
        $writer->wnl($this->_formatLine($value));
      }
      fflush($resource);
    } else {
      foreach ($items as $value) {
        $writer->wnl($this->_formatLine($value));
      }
    }
    return $this;
  }

  function close(): void {
    $this->teardownWriter(true);
  }

  #############################################################################
  const _AUTOGEN_CONSTS = [
    "" => [self::class, "_autogen_consts", true],
  ];
  const _AUTOGEN_LITERALS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '\\nur\\b\\params\\parametrable_utils::class',
    ],
    [self::class, 'self::class'],
    [
      self::PARAMETRABLE_PARAMS_SCHEMA,
      'self::PARAMETRABLE_PARAMS_SCHEMA',
    ],
  ];
  const _AUTOGEN_METHODS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_getters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      self::class,
    ],
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_setters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      self::class,
    ],
  ];
  const _AUTO_GETTERS = /*autogen*/[
    'getOutput' => 'output',
    'getSchema' => 'schema',
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setOutput' => 'output',
    'setSchema' => 'schema',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
  ];
  #--autogen-dynamic--
}
