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

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

  return label;
}

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

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

export function buildCustomOption({ values, label, operator, unit, selected }) {
  let optionLabel = label;
  if (optionLabel === undefined) {
    optionLabel = buildOptionLabel({ values, operator, unit });
  }
  if (optionsMap[operator]) {
    return {
      operator,
      values,
      id: `custom${operator}${optionLabel}`,
      displayName: optionLabel,
      custom: true,
      isSelected: selected,
      prefix: optionsMap[operator].prefix,
    };
  } else {
    console.error(`Error:karmadata-ui ${operator} is not a valid operator - (Eq|NotEq|Gt|Gte|Lt|Lte|Between)`)
    return;
  }
}
