const optionsMap = {
  anyOption: {
    id: 'defaultAny',
    operator: 'any',
    displayName: 'Any',
    first: true,
  },
  equalToOption: {
    id: 'defaultEq',
    operator: 'Eq',
    displayName: 'Equal To',
    prefix: '',
    first: true,
  },
  noEqualToOption: {
    id: 'defaultNotEq',
    operator: 'NotEq',
    displayName: 'Not Equal To',
    prefix: '!=',
    first: true,
  },
  greaterThanOption: {
    id: 'defaultGt',
    operator: 'Gt',
    displayName: 'Grater Than',
    prefix: '>',
    first: true,
  },
  greaterThanOrEqualToOption: {
    id: 'defaultGte',
    displayName: 'Grater Than Or Equal To',
    operator: 'Gte',
    prefix: '>=',
    first: true,
  },
  lessThanOption: {
    id: 'defaultLt',
    operator: 'Lt',
    displayName: 'Less Than',
    prefix: '<',
    first: true,
  },
  lessThanOrEqualToOption: {
    id: 'defaultLte',
    operator: 'Lte',
    displayName: 'Less Than Or Equal To',
    prefix: '<=',
    first: true,
  },
  betweenOption: {
    id: 'defaultBetween',
    operator: 'Between',
    displayName: 'Specify A Range',
    first: false,
  },
  noneOption: {
    id: 'defaultNone',
    operator: 'None',
    displayName: 'None',
    first: false,
  },
  moreThanZeroOption: {
    id: 'defaultMtz',
    operator: 'Mtz',
    displayName: 'More Than 0',
    first: false,
  },
};

export function buildOptionLabel({ value, operator, customHandler, unit }) {
  const optionKey = Object.keys(optionsMap).find(key => optionsMap[key].operator === operator);
  const prefix = optionsMap[optionKey].prefix;
  const label = unit ? `${value[0]}-${value[1]} ${unit}` : `${value[0]}-${value[1]}`;
  if (prefix) {
    return prefix + value;
  } else if (Array.isArray(value) && operator === 'Between') {
    return customHandler ? customHandler(...value) : label;
  }

  return value;
}

export default {
  create(type, displayName) {
    const obj = displayName ? { displayName } : {};
    return Object.assign(
      {},
      optionsMap[type],
      obj,
    );
  },
};

export function buildEmptyOption() {
  return {
    value,
    id: null,
    displayName: '',
  };
}

export function buildCustomOption({ value, label, operator, unit }) {
  let optionLabel = label;
  if (optionLabel === undefined) {
    optionLabel = buildOptionLabel({ value, operator, unit });
  }

  return {
    operator,
    value,
    id: `custom${operator}${optionLabel}`,
    displayName: optionLabel,
    custom: true,
  };
}
