<template>
  <button
    :class="className"
    type="button"
    :aria-label="buttonLabel"
    :aria-expanded="calculatedExpansion"
    @click="toggle"
  >
    <component :is="icon" :modifiers="iconModifiers" />
  </button>
</template>

<script>
import IconDash from '@mindful-web/marko-web-icons/browser/dash.vue';
import IconPlus from '@mindful-web/marko-web-icons/browser/plus.vue';
import IconThreeBars from '@mindful-web/marko-web-icons/browser/three-bars.vue';
import IconX from '@mindful-web/marko-web-icons/browser/x.vue';

const validateIcon = (v) => ['dash', 'plus', 'three-bars', 'x'].includes(v);

export default {
  components: {
    IconDash,
    IconPlus,
    IconThreeBars,
    IconX,
  },
  props: {
    className: {
      type: String,
      default: null,
    },
    initiallyExpanded: {
      type: Boolean,
      default: false,
    },
    targets: {
      type: Array,
      default: () => [],
    },
    toggleClass: {
      type: String,
      required: true,
    },
    iconModifiers: {
      type: Array,
      default: () => [],
    },
    buttonLabel: {
      type: String,
      default: 'Menu',
    },
    iconName: {
      type: String,
      default: 'three-bars',
      validator: validateIcon,
    },
    expandedIconName: {
      type: String,
      default: 'x',
      validator: validateIcon,
    },
  },
  data: () => ({
    expanded: false,
  }),
  computed: {
    icon() {
      if (this.expanded) return `icon-${this.expandedIconName}`;
      return `icon-${this.iconName}`;
    },
    calculatedExpansion() {
      if (this.expanded) return 'true';
      return 'false';
    },
  },
  created() {
    this.expanded = this.initiallyExpanded;
    window.addEventListener('keydown', this.onEscapeKeyDown);
  },
  methods: {
    toggle() {
      this.expanded = !this.expanded;
      const elements = document.querySelectorAll(this.targets.join(','));
      Array.prototype.forEach.call(elements, (el) => el.classList.toggle(this.toggleClass));
    },
    onEscapeKeyDown(event) {
      if (event.key === 'Escape' && this.expanded) {
        this.toggle();
      }
    },
  },
};
</script>
