<?php
namespace nur\v\bs3\plugins;

use nur\b\params\IParametrable;
use nur\b\params\Tparametrable1;
use nur\v\BasePlugin;
use nur\v\js;

class easymdePlugin extends BasePlugin implements IParametrable {
  use Tparametrable1;

  const CSS = [
    "nur-v-bs3/highlightjs/styles/default.min.css",
    "nur-v-bs3/easymde/easymde.min.css",
  ];

  const JS = [
    "nur-v-bs3/highlightjs/highlight.min.js",
    "nur-v-bs3/easymde/easymde.min.js",
  ];

  const SELECTOR = null;
  const CONFIG = [
    "spellChecker" => false,
    "hideIcons" => ["image"],
  ];

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "selector" => ["string", "textarea:first", "sélecteur pour les controles"],
    "config" => ["?array", null, "configuration de l'objet"],
  ];

  function __construct(?array $params=null) {
    self::set_parametrable_params_defaults($params, [
      "selector" => static::SELECTOR,
      "config" => static::CONFIG,
    ]);
    $this->initParametrableParams($params);
  }

  protected $ppSelector;

  protected $ppConfig;

  const HAVE_JQUERY = true;

  function printJquery(): void {
    ?>
<script type="text/javascript">
jQuery.noConflict()(function($) {
  let config = <?=js::qo($this->ppConfig)?>;
  let selector = <?=js::qv($this->ppSelector)?>;
  if (selector !== "") {
    let $elements = $(selector);
    if ($elements.length > 0) {
      $elements.each(function () {
        config.element = this;
        new EasyMDE(config);
      });
    }
  }
});
</script>
<?php
  }
}
