export function getFlattenedSubsectionOptions(collection) {
  return collection.flatMap(option => option.subOptions && option.isShown !== false ? option.subOptions : []);
}
export function getFooterOptionWithPrivateKey(option) {
  if (!option) {
    return option;
  }
  return {
    privateKey: 'footer',
    ...option
  };
}

/**
 * privateKey is used for internal logic only, for efficient search and comparison of options.
 * it is not exposed to consumers (omitted in onSelect function)
 */
export function getOptionsWithPrivateKey(options, ancestors = []) {
  if (!options || !options.length) {
    return options;
  }
  return options.map(({
    subItems,
    subOptions,
    radioOptions,
    ...rest
  }, i) => {
    const option = {
      privateKey: [...ancestors, i].join('.'),
      ...rest
    };
    if (subItems) {
      option.subItems = getOptionsWithPrivateKey(subItems, [...ancestors, i]);
    }
    if (subOptions) {
      option.subOptions = getOptionsWithPrivateKey(subOptions, [...ancestors, i]);
    }
    if (radioOptions) {
      option.radioOptions = radioOptions.map((o, ii) => ({
        ...o,
        privateKey: [...ancestors, ii].join('.')
      }));
    }
    return option;
  });
}