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

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


function buildSelectOption(selectOption) {
  if (selectOption && selectOption.visible) {
    return {
      id: SELECT_OPTION,
      displayName: selectOption.label || 'Select...',
    };
  }

  return null;
}


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

export function buildCustomOptions(optionValues) {
  let options = [];
  const customOptions = optionValues.customOptions.map(buildCustomOption);
  const anyOption = buildAnyOption(optionValues.anyOption);
  const selectOption = buildSelectOption(optionValues.selectOption);
  if (selectOption) {
    options.push(selectOption);
  }

  if (anyOption) {
    options.push(anyOption);
  }

  if (customOptions.length) {
    options.push(...customOptions);
  }
  return options;
}

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',
        }
      ),
    },
  };
}
