<template>
  <ul class="hn-select-group__wrap" v-show="visible">
    <li class="hn-select-group__title">{{ label }}</li>
    <li>
      <ul class="hn-select-group">
        <slot></slot>
      </ul>
    </li>
  </ul>
</template>

<script type="text/babel">
  import Emitter from 'heiner-ui/src/mixins/emitter';

  export default {
    mixins: [Emitter],

    name: 'HnOptionGroup',

    componentName: 'HnOptionGroup',

    props: {
      label: String,
      disabled: {
        type: Boolean,
        default: false
      }
    },

    data() {
      return {
        visible: true
      };
    },

    watch: {
      disabled(val) {
        this.broadcast('HnOption', 'handleGroupDisabled', val);
      }
    },

    methods: {
      queryChange() {
        this.visible = this.$children &&
          Array.isArray(this.$children) &&
          this.$children.some(option => option.visible === true);
      }
    },

    created() {
      this.$on('queryChange', this.queryChange);
    },

    mounted() {
      if (this.disabled) {
        this.broadcast('HnOption', 'handleGroupDisabled', this.disabled);
      }
    }
  };
</script>
