import { ANY } from '../../constants';

function buildAnyOption(anyOption) {
  if (anyOption && anyOption.visible) {
    return {
      id: ANY,
      displayName: anyOption.label || ANY,
    };
  }

  return null;
}

export function buildCustomOption({ value, label, direction }) {
  return {
    id: value,
    displayName: label,
    direction,
  };
}

export function buildCustomOptions(optionValues) {
  const customOptions = optionValues.customOptions.map(buildCustomOption);
  const anyOption = buildAnyOption(optionValues.anyOption);

  return anyOption ? [anyOption, ...customOptions] : customOptions;
}

export function buildChickletValue(customOption, model) {
  return {
    value: customOption.id,
    chickletLabel: customOption.displayName,
    option: customOption.id,
    direction: customOption.direction,
    kdFilterOptions: {
      model,
      itemHandler: ({ value }) => (
        {
          PropertyName: value,
          Direction: 'desc',
        }
      ),
    },
  };
}
