import Property from './Property';

export default class PropertyRadio extends Property {
  defaults() {
    return {
      ...Property.prototype.defaults,
      options: [], // Array of options, eg. [{ id: '100', label: 'Set 100' }]
      full: 1
    };
  }

  /**
   * Get available options.
   * @returns {Array<Object>} Array of options
   */
  getOptions() {
    // support old list property
    const { options, list } = this.attributes;
    return (options && options.length ? options : list) || [];
  }

  /**
   * Update options.
   * @param {Array<Object>} value Array of options, eg. `[{ id: 'val-1', label: 'Value 1' }]`
   */
  setOptions(value = []) {
    this.set('options', value);
    return this;
  }

  /**
   * Add new option.
   * @param {Object} value Option object, eg. `{ id: 'val-1', label: 'Value 1' }`
   */
  addOption(value) {
    if (value) {
      const opts = this.getOptions();
      this.setOptions([...opts, value]);
    }
    return this;
  }

  /**
   * Get the option id from the option object.
   * @param {Object} option Option object
   * @returns {String} Option id
   */
  getOptionId(option) {
    return option.id || option.value;
  }

  /**
   * Get option label.
   * @param {String} id Option id
   * @param {Object} [opts={}] Options
   * @param {Boolean} [opts.locale=true] Use the locale string from i18n module
   * @returns {String} Option label
   */
  getOptionLabel(id, opts = {}) {
    const { locale = true } = opts;
    const options = this.getOptions();
    const option = options.filter(o => this.getOptionId(o) === id)[0] || {};
    const label = option.label || option.name || id;
    const propId = this.getId();
    return (
      (locale && this.em?.t(`styleManager.options.${propId}.${id}`)) || label
    );
  }

  initialize(...args) {
    Property.prototype.initialize.apply(this, args);
    this.listenTo(this, 'change:options', this.__onOptionChange);
  }

  __onOptionChange() {
    this.set('list', this.get('options'));
  }
}

@@ -240,16 +240,17 @@
  full: 1
+,
 %0A    %7D;%0A
@@ -502,24 +502,234 @@
 %7C%7C %5B%5D;%0A  %7D%0A%0A
+  /**%0A   * Get current selected option.%0A   * @returns %7BObject %7C null%7D%0A   */%0A  getOption() %7B%0A    const id = this.getValue();%0A    return this.getOptions().filter(o =%3E this.getOptionId(o) === id)%5B0%5D %7C%7C null;%0A  %7D%0A%0A
   /**%0A   * U
@@ -1897,16 +1897,8 @@
 rn (
-%0A      (
 loca
@@ -1967,14 +1967,8 @@
 abel
-%0A    )
 ;%0A  

Add getOption to radio property