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

const baseProto = {
  buildItem({ label, modelOptions }) {
    return label ? { label, option: this.id, modelOptions } : { option: this.id };
  },

  set selected(value) {
    this.selectedObject = this.buildItem(value);
    if (this.selectedObject) {
      this.selectedObject.chickletLabel = this.chickletLabel(this.selectedObject.label);
    }
  },

  get selected() {
    return this.selectedObject;
  },

  get selectedLabel() {
    return this.selectedObject ? this.selectedObject.label : '';
  },

  get selectedValue() {
    return this.selectedObject ? this.selectedObject.value : '';
  },

  loadDefaultValue() {
    return null;
  },

  chickletLabel(value) {
    const prefix = this.prefix || '';
    return prefix + String(value);
  },
};

const OptionFactory = {
  create(type) {
    return Object.assign(
      Object.create(baseProto),
      this.optionsMap[type],
      { key: type }
    );
  },

  optionsMap: {
    [ANY]: {
      id: ANY,
      displayName: ANY
    }

  }
};

export default OptionFactory;

export const isValidOption = (option) => (Object.keys(OptionFactory.optionsMap).indexOf(option) >= 0);

export const defaultOptions = [ANY];

export const buildCustomOption = ({ label, modelOptions, key }) => {
  return Object.assign(
    Object.create(baseProto),
    {
      id: label + key,
      displayName: label,
      key,
      modelOptions,
      loadDefaultValue() {
        this.selected = { label, modelOptions };
        return true;
      },
    });
};