"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEnabledOptions = void 0;
var _react = require("react");
/*

  NOTE

  If provided options is a simple object, remember that keys will be always treated as strings.
  This Note should be on the Select component docs!

*/

var useEnabledOptions = (options, allowedValues, disallowedValues) => {
  var [enabledOptions, setEnabledOptions] = (0, _react.useState)([]);
  (0, _react.useEffect)(() => {
    var getFromArrayOfArray = a => {
      var enabledOptions = [];
      a.map(opt => {
        var optValue = opt[0];
        var optLabel = opt[1] || optValue;
        var optDisabled = opt.length >= 3 ? opt[2] : false;
        enabledOptions.push({
          value: optValue,
          label: optLabel,
          disabled: optDisabled
        });
      });
      return enabledOptions;
    };
    var getFromArrayOfObject = a => {
      var enabledOptions = [];
      a.map(opt => {
        var optValue = opt.value;
        var optLabel = (opt === null || opt === void 0 ? void 0 : opt.label) || optValue;
        var optDisabled = (opt === null || opt === void 0 ? void 0 : opt.disabled) || false;
        enabledOptions.push({
          value: optValue,
          label: optLabel,
          disabled: optDisabled
        });
      });
      return enabledOptions;
    };
    var getFromObject = a => {
      var enabledOptions = [];
      Object.entries(a).map(opt => {
        var optValue = opt[0];
        var optLabel = opt[1];
        enabledOptions.push({
          value: optValue,
          label: optLabel,
          disabled: false
        });
      });
      return enabledOptions;
    };
    var isAnArray = v => Object.prototype.toString.call(v) === '[object Array]';
    var isDisallowed = v => {
      if (disallowedValues) {
        if (disallowedValues.indexOf(v) >= 0) {
          return true;
        }
      }
      return false;
    };
    var isAllowed = v => {
      if (allowedValues) {
        if (allowedValues.indexOf(v) >= 0) {
          return true;
        }
        return false;
      }
      return true;
    };
    var optionsList = [];
    if (!isAnArray(options)) {
      if (Object.keys(options).length > 0) {
        optionsList = getFromObject(options);
      }
    } else {
      if (options.length > 0) {
        var first = options[0];
        if (isAnArray(first)) {
          optionsList = getFromArrayOfArray(options);
        } else {
          optionsList = getFromArrayOfObject(options);
        }
      }
    }
    var nEnabledOptions = optionsList.map(opt => {
      return {
        value: opt.value,
        label: opt.label,
        disabled: opt.disabled || isDisallowed(opt.value) || !isAllowed(opt.value)
      };
    });
    setEnabledOptions(nEnabledOptions);
  }, [options, allowedValues, disallowedValues]);
  return enabledOptions;
};
exports.useEnabledOptions = useEnabledOptions;