text: import {DropdownParams} from "./DropdownParams";
  
  export class Dropdowns {
  
      static selectize(selector) {
  
          if($(selector).is('select')) {
              Dropdowns.initSelectizeHandler($(selector));
          }
  
          $(selector).find('select:not(.selectize-template)').each(function () {
              Dropdowns.initSelectizeHandler($(this));
          })
      }
  
      static destroy(selector) {
          $(selector).find('select').each(function () {
              if ($(this)[0].hasOwnProperty('selectize')) {
                  $(this)[0].selectize.clear();
                  $(this)[0].selectize.clearOptions();
                  $(this)[0].selectize.destroy();
              }
          })
      }
  
      static initSelectizeHandler($element) {
          if($.fn.selectize === undefined)
              require('selectize');
  
  
          let params = {},
              selectize = undefined,
              src = "",
              selected = "";
  
          if((src = $element.data('src')) !== undefined) {
  
              params = Object.assign(params, {
                  preload: true,
                  valueField: $element.data('src-value') ?? 'id',
                  labelField: $element.data('src-label') ?? 'name',
                  searchField: $element.data('src-label') ?? 'name',
                  load: function(query, callback) {
                      Dropdowns.selectizeAxiosLoad($element, src, selectize, callback, query);
                  },
              });
          }
          else {
              /** If src not set it will allow a selected option */
              if ((selected = $element.attr('data-selected')) !== undefined){
                  $element.val(selected);
              }
          }
  
          //Loading the specific parameters for this dropdown
          if($element.data('init') !== undefined)
              params = Object.assign(params, DropdownParams[$element.data('init')](params, $element));
  
  
          selectize = $element.selectize(params)[0].selectize;
      }
  
      static selectizeAxiosLoad($element, src, selectize, callback, query) {
          if(query === undefined)
              query = {};
  
          axios.get(src, {
              params: {
                  query: query,
                  selectize: true
              },
              headers: {
                  "Authorization": 'Bearer ' + $('meta[name="api-token"]').attr('content')
              }
          }).then(response => {
              callback(response.data);
  
              if($element.data('selected') !== undefined) {
                  if(!isNaN($element.attr('data-selected'))) {
                      selectize.addItem($element.attr('data-selected'));
                  } else if(Array.isArray($element.attr('data-selected'))) {
                      console.log(1);
                      $element.data('selected').forEach(item => {
                          selectize.addItem(item);
                      })
                  } else if ($element.attr('data-selected').indexOf(',') > 0) {
                      $element.attr('data-selected').split(',').forEach((value) => {
                          selectize.addItem(value);
                      })
                  }
                  else {
                      selectize.addItem($element.attr('data-selected'));
                  }
              }
          })
      }
  }
  