"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.sortOptions = sortOptions;
var _adaptiveListConstants = require("./adaptive-list-constants");
function sortOptions({
  options,
  maxWidth,
  selected,
  optionsWidth,
  startWidth,
  state
}) {
  const totalItemsWidth = Object.values(options).reduce((acc, current) => acc + optionsWidth[current.key], 0);
  const isLimitNeeded = totalItemsWidth > maxWidth;
  if (!isLimitNeeded || state === _adaptiveListConstants.DRAW_STATE.CALCULATING) {
    return {
      active: options,
      inActive: []
    };
  }
  const sortedObject = options.reduce((acc, current, index) => ({
    ...acc,
    [current.key]: index
  }), {});
  const selectedOptions = options.filter(({
    key
  }) => selected.includes(key));
  const unSelectedOptions = options.filter(({
    key
  }) => !selected.includes(key));
  const active = [];
  const inActive = [];
  let currentWidth = startWidth;
  [...selectedOptions, ...unSelectedOptions].forEach(option => {
    currentWidth = currentWidth + (optionsWidth[option.key] || 0);
    if (currentWidth > maxWidth) {
      inActive.push(option);
    } else {
      active.push(option);
    }
  });
  const sortFn = (a, b) => sortedObject[a.key] - sortedObject[b.key];
  return {
    active: active.sort(sortFn),
    inActive: inActive.sort(sortFn)
  };
}