<template>
  <input-selectbox
    v-if="languages.length"
    model-name="languages"
    :form-id="formId"
    :input-data="languages"
    :label="$_tr('language')"
    :default-value="languageDefaultValue.uuid"
    object-label-key="title"
    object-value-key="uuid"
    class="language"
  />
</template>

<script>
import { http } from '@inside-library/helpers/Http'

export default {
  name: 'RegisterLanguagesSelectbox',
  $_i18nRootPath: 'customs.FORMS.REGISTER',
  props: {
    formId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      languages: [],
      languageDefaultValue: null
    }
  },
  created() {
    this.$_requestLanguages()
  },
  methods: {
    async $_requestLanguages() {
      try {
        const { data } = await http.get('content/languages', {
          fields: ['uuid', 'title']
        })
        this.languages = data

        this.languageDefaultValue = this.languages[0]
      } catch (err) {
        throw new Error(err)
      }
    }
  }
}
</script>
