import { extend } from '../../../vue';
import { PROP_TYPE_STRING } from '../../../constants/props';
import { get } from '../../../utils/get';
import { isPlainObject, isNull, isUndefined } from '../../../utils/inspect';
import { sortKeys } from '../../../utils/object';
import { makePropsConfigurable, makeProp } from '../../../utils/props';
import { props as props$1, formOptionsMixin } from '../../../mixins/form-options';

// --- Props ---

const props = makePropsConfigurable(sortKeys({
  ...props$1,
  labelField: makeProp(PROP_TYPE_STRING, 'label'),
  optionsField: makeProp(PROP_TYPE_STRING, 'options')
}), 'formOptions');

// --- Mixin ---

// @vue/component
const optionsMixin = extend({
  mixins: [formOptionsMixin],
  props,
  methods: {
    normalizeOption(option) {
      let key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
      // When the option is an object, normalize it
      if (isPlainObject(option)) {
        const value = get(option, this.valueField);
        const text = get(option, this.textField);
        const options = get(option, this.optionsField, null);
        // When it has options, create an `<optgroup>` object
        if (!isNull(options)) {
          return {
            label: String(get(option, this.labelField) || text),
            options: this.normalizeOptions(options)
          };
        }
        // Otherwise create an `<option>` object
        return {
          value: isUndefined(value) ? key || text : value,
          text: String(isUndefined(text) ? key : text),
          html: get(option, this.htmlField),
          disabled: Boolean(get(option, this.disabledField))
        };
      }
      // Otherwise create an `<option>` object from the given value
      return {
        value: key || option,
        text: String(option),
        disabled: false
      };
    }
  }
});

export { optionsMixin, props };
