function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { isOptgroupSymbol } from './SuperSelect.constants';
export var getGroupNameIsValid = function getGroupNameIsValid(groupName) {
  var isGroupDefined = ![undefined, null, ''].includes(groupName);
  return isGroupDefined && typeof groupName === 'string' && groupName.length > 0;
};
export var getOptionsSortingAlgorithm = function getOptionsSortingAlgorithm(_ref) {
  var getOptionIsBatch = _ref.getOptionIsBatch,
    getOptionLabel = _ref.getOptionLabel;
  return function (a, b) {
    var isOptionBatch1 = getOptionIsBatch(a);
    var isOptionBatch2 = getOptionIsBatch(b);

    // move batched options up
    if (isOptionBatch1 && isOptionBatch2) {
      return compareStrings(getOptionLabel(a), getOptionLabel(b));
    } else if (isOptionBatch1) {
      return -1;
    } else if (isOptionBatch2) {
      return 1;
    }
    return compareStrings(getOptionLabel(a), getOptionLabel(b));
  };
};
export var getBatchOptionFormatter = function getBatchOptionFormatter(_ref2) {
  var value = _ref2.value,
    multiple = _ref2.multiple,
    getOptionIsBatch = _ref2.getOptionIsBatch,
    getOptionValue = _ref2.getOptionValue;
  return function (option) {
    if (isMultiple(multiple, value) && getOptionIsBatch(option)) {
      var val = getOptionValue(option);
      var disabled = Array.isArray(val) && val.every(function (v) {
        return value.includes(v);
      });
      return _objectSpread(_objectSpread({}, option), {}, {
        disabled: disabled
      });
    }
    return option;
  };
};
export var RESERVED_UNGROUPED_OPTGROUP_NAME = 'RESERVED_UNGROUPED_OPTGROUP_NAME';
export var collectGroupsInOrderOfOccurrence = function collectGroupsInOrderOfOccurrence(opts, getOptionGroup) {
  var output = opts.reduce(function (_ref3, option) {
    var _groupedOptions$adjus;
    var groups = _ref3.groups,
      groupedOptions = _ref3.groupedOptions;
    var groupName = getOptionGroup(option);

    // if group name is invalid, consider the option ungrouped
    var isGroupNameValid = getGroupNameIsValid(groupName);
    var adjustedGroupName = isGroupNameValid ? groupName : RESERVED_UNGROUPED_OPTGROUP_NAME;
    var groupOptions = (_groupedOptions$adjus = groupedOptions[adjustedGroupName]) !== null && _groupedOptions$adjus !== void 0 ? _groupedOptions$adjus : [];

    // push/assign for performance considerations
    groupOptions.push(option);
    groupedOptions[adjustedGroupName] = groupOptions;
    return {
      groups: groups.includes(adjustedGroupName) ? groups : [].concat(_toConsumableArray(groups), [adjustedGroupName]),
      groupedOptions: groupedOptions
    };
  }, {
    groups: [],
    groupedOptions: {}
  });

  // if ungrouped options were found, lift them to the top
  if (output.groups.includes(RESERVED_UNGROUPED_OPTGROUP_NAME)) {
    return {
      groupedOptions: output.groupedOptions,
      groups: [RESERVED_UNGROUPED_OPTGROUP_NAME].concat(_toConsumableArray(output.groups.filter(function (group) {
        return group !== RESERVED_UNGROUPED_OPTGROUP_NAME;
      })))
    };
  }
  return output;
};
function findParentOptgroup(list, index) {
  for (var i = index; i >= 0; i--) {
    var option = list[i];
    var isOptgroup = getOptionIsOptgroup(option);
    if (isOptgroup) {
      return option.id;
    }
  }
  // no parent optgroup was found, so the option is considered ungrouped
  return RESERVED_UNGROUPED_OPTGROUP_NAME;
}
export function reorder(options, startIndex, endIndex) {
  var nextOptions = _toConsumableArray(options);
  var _nextOptions$splice = nextOptions.splice(startIndex, 1),
    _nextOptions$splice2 = _slicedToArray(_nextOptions$splice, 1),
    removed = _nextOptions$splice2[0];
  nextOptions.splice(endIndex, 0, removed);
  var prevGroup = findParentOptgroup(options, startIndex);
  var nextGroup = findParentOptgroup(nextOptions, endIndex);
  return {
    nextOptions: nextOptions,
    prevGroup: prevGroup,
    nextGroup: nextGroup
  };
}
function compareStrings(a, b) {
  return a.localeCompare(b, undefined, {
    numeric: true,
    sensitivity: 'base'
  });
}
export function isMultiple(multiple, value) {
  return multiple && Array.isArray(value);
}
export var getOptionIsOptgroup = function getOptionIsOptgroup(item) {
  return Boolean(item[isOptgroupSymbol]);
};
export var sortOptgroups = function sortOptgroups(groupA, groupB) {
  // always put ungrouped options on top
  if (groupA === RESERVED_UNGROUPED_OPTGROUP_NAME) {
    return -1;
  }
  if (groupB === RESERVED_UNGROUPED_OPTGROUP_NAME) {
    return 1;
  }
  return compareStrings(groupA, groupB);
};
export function getIsAllOptionsUngrouped(groups) {
  return groups.length === 1 && groups[0] === RESERVED_UNGROUPED_OPTGROUP_NAME;
}
export function createOptgroup(groupName, groupOptions) {
  var _optgroupHeader;
  // ungrouped options should not have a group header
  if (groupName === RESERVED_UNGROUPED_OPTGROUP_NAME) {
    return groupOptions;
  }
  var optgroupHeader = (_optgroupHeader = {}, _defineProperty(_optgroupHeader, isOptgroupSymbol, true), _defineProperty(_optgroupHeader, "id", groupName), _defineProperty(_optgroupHeader, "label", groupName), _optgroupHeader);
  return [optgroupHeader].concat(_toConsumableArray(groupOptions));
}
export function removeEmptyOptGroups(options) {
  return options.filter(function (opt, index, arr) {
    var currentElementIsLast = index === arr.length - 1;
    var currentElementIsOptgroup = getOptionIsOptgroup(opt);
    var nextElementIsOptgroup = !currentElementIsLast && getOptionIsOptgroup(arr[index + 1]);
    return !(currentElementIsOptgroup && (nextElementIsOptgroup || currentElementIsLast));
  });
}
//# sourceMappingURL=SuperSelect.utils.js.map