<?php
namespace nur\mapper\fsv;

use nur\b\io\Tfilter;
use nur\b\params\Tparametrable;
use nur\mapper\base\Consumer;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Ttmpwriter;

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

  function __construct($output=null) {
    parent::__construct();
    $this->assoc2fsv = new Assoc2FsvMapper();
    $this->pp_setOutput($output);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "output" => [null, null, "fichier en sortie"],
  ];

  static function _get_parametrable_params_schema(): array {
    return array_merge(self::PARAMETRABLE_PARAMS_SCHEMA
      , Assoc2FsvMapper::PARAMETRABLE_PARAMS_SCHEMA
      , encoding_utils::PARAMETRABLE_PARAMS_SCHEMA);
  }

  /** @var Assoc2FsvMapper */
  protected $assoc2fsv;
  function pp_setFsvSchema($fsvSchema): void {
    $this->assoc2fsv->setFsvSchema($fsvSchema);
  }
  function pp_setInputEncoding(?string $inputEncoding): void {
    $this->assoc2fsv->setInputEncoding($inputEncoding);
  }
  function pp_setOutputEncoding(?string $outputEncoding): void {
    $this->assoc2fsv->setOutputEncoding($outputEncoding);
  }

  protected function setup(): void {
    parent::setup();
    $this->setupWriter(false);
    $this->assoc2fsv->ensureSetup();
  }

  protected function teardown(): void {
    $this->assoc2fsv->close();
    $this->teardownWriter(false);
  }

  function _consume(iterable $items): void {
    $mapper = $this->assoc2fsv;
    $mapper->setSharedOobdManager($this->sharedOobdManager);
    $writer = $this->getWriter();
    foreach ($items as $value) {
      $writer->wnl($mapper->_formatLine($value));
    }
  }

  #############################################################################
  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',
    'getFsvSchema' => 'fsv_schema',
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setOutput' => 'output',
    'setFsvSchema' => 'fsv_schema',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
  ];
  #--autogen-dynamic--
}