<template>
  <div class="vc-radio-tabs">
    <div class="vc-radio-tabs__wrapper">
      <div class="vc-radio-tabs__header">
        <div class="vc-radio-tabs__nav-wrapper">
          <radio-tab-nav
            :options="options"
            :config="config"
            :active-index="modelValue"
            @index-of-active-element="getActiveIndex"
            @change="change"
          />
        </div>
      </div>
      <div class="vc-radio-tabs__content-wrapper">
        <div class="vc-radio-tabs__content">
          <div class="vc-radio-tabs__content--content w-full h-full">
            <slot :name="slotName" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import RadioTabNav from '@/components/VcRadioTabs/UiComponents/RadioTabNav.vue'
import optionMethods from '@/mixins/optionMethods.mixin'
import { KeyAttributeType } from '@/types/VueGeneric.types'
import { OptionType } from '@/types/OptionAndConfig.types'

export default defineComponent({
  name: 'VcRadioTabs',
  components: { RadioTabNav },
  mixins: [optionMethods],
  props: {
    modelValue: {
      type: String,
      required: true
    }
  },
  emits: ['change', 'update:modelValue'],
  data () {
    return {
      activeElement: '' as KeyAttributeType | unknown | undefined
    }
  },
  computed: {
    slotName () {
      const allKeys: KeyAttributeType[] | unknown[] | undefined[] = Object.values(this.options).map((el, index) => typeof el === 'object' && this.optionKey(el, index, this.config))
      const validationIsStringAndNotEmpty = typeof this.activeElement === 'string' && this.activeElement

      return validationIsStringAndNotEmpty && allKeys.includes(this.activeElement) ? this.activeElement : ''
    }
  },
  mounted () {
    this.activeElement = this.modelValue
  },
  methods: {
    getActiveIndex (index: KeyAttributeType | unknown | undefined) {
      this.activeElement = index
      this.$emit('update:modelValue', index)
    },
    change (val: OptionType) {
      this.$emit('change', val)
    }
  }
})
</script>

<style lang="scss">

.vc-radio-tabs {
  @apply box-border max-h-inherit max-w-full h-fit-content border border-secondary-actions rounded-lg;
  &__wrapper {
    @apply flex flex-col w-full;
  }
  &__header {
    @apply h-auto mb-0 min-w-xs relative;
  }
  &__nav-wrapper {
    @apply w-full;
  }
  &__content-wrapper {
    @apply flex-grow h-auto;
  }
  &__content {
    @apply h-full;
    &--content {
      @apply p-6;
    }
  }
}
</style>
