function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { isOptgroupSymbol, isOptSelectAllSymbol } 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 getOptionIsOptSelectAll = function getOptionIsOptSelectAll(item) {
  return Boolean(item[isOptSelectAllSymbol]);
};
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) {
  // ungrouped options should not have a group header
  if (groupName === RESERVED_UNGROUPED_OPTGROUP_NAME) {
    return groupOptions;
  }
  var optgroupHeader = _defineProperty(_defineProperty(_defineProperty({}, isOptgroupSymbol, true), "id", groupName), "label", groupName);
  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