<?php
namespace nur\mapper\csv;

use nur\b\ValueException;

class csv_defaults {
  const OO_NAME = "ooffice";
  const OO_SEPARATOR = ",";
  const OO_ENCLOSURE = "\"";
  const OO_ESCAPE = "\\";
  const OO_FLAVOUR = [self::OO_SEPARATOR, self::OO_ENCLOSURE, self::OO_ESCAPE];
  const OO_ENCODING = "utf-8";

  const XL_NAME = "excel";
  const XL_SEPARATOR = ";";
  const XL_ENCLOSURE = "\"";
  const XL_ESCAPE = "\\";
  const XL_FLAVOUR = [self::XL_SEPARATOR, self::XL_ENCLOSURE, self::XL_ESCAPE];
  const XL_ENCODING = "cp1252";

  const NAME_MAP = [
    "oo" => self::OO_NAME,
    "xl" => self::XL_NAME,
  ];

  static final function verifix_name(string $name): string {
    if (array_key_exists(strtolower($name), self::NAME_MAP)) {
      $name = self::NAME_MAP[strtolower($name)];
    }
    return $name;
  }

  private static function invalid_name(string $name): ValueException {
    return new ValueException("$name: format CSV invalide");
  }

  static final function get_flavour(string $name): array {
    switch ($name) {
    case self::OO_NAME: return self::OO_FLAVOUR;
    case self::XL_NAME: return self::XL_FLAVOUR;
    default: throw self::invalid_name($name);
    }
  }

  static final function get_encoding(string $name): string {
    switch ($name) {
    case self::OO_NAME: return self::OO_ENCODING;
    case self::XL_NAME: return self::XL_ENCODING;
    default: throw self::invalid_name($name);
    }
  }
}
