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

function buildNotSelectedOption(notSelectedOption) {
  if (notSelectedOption) {
    return {
      id: ANY,
      displayName: notSelectedOption.label || ANY,
    };
  }
  return null;
}


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

  return null;
}


export function buildCustomOption({ values, label, direction }) {
  return {
    id: values.join(),
    displayName: label,
    direction,
    values,
  };
}

export function buildOptions(options, notSelectedOption, selectOption) {
  const customOptions = options.map(buildCustomOption);
  const notSelected = buildNotSelectedOption(notSelectedOption);
  const select = buildSelectOption(selectOption);
  const baseOptions = [];
  if (notSelected) {
    baseOptions.push(notSelected);
  }
  if (select) {
    baseOptions.push(select);
  }

  return [...baseOptions, ...customOptions];
}

export function buildChickletValue({ option, model, value }) {
  return {
    value,
    values: [value],
    sortable: true,
    chickletLabel: option.displayName,
    option: option.id,
    direction: option.direction,
    kdFilterOptions: {
      model,
      operator: 'Eq',
      itemHandler: ({ value }) => (
        {
          propertyName: value,
          direction: 'desc',
        }
      ),
    },
  };
}
