import { Component, Prop, Vue } from 'vue-property-decorator';

@Component({})
export default class ReferenceSelect extends Vue {
  @Prop() protected dictionary!: string;
  @Prop() protected label: string;
  @Prop({ default: 'description' }) labelKey: string;
  @Prop({ default: 'code' }) valueKey: string;

  protected list: any = {};
  protected loading = true;

  protected get options() {
    const values: any[] = [];
    for (const code in this.list) {
      values.push({
        label: this.list[code][this.labelKey],
        value: code,
      });
    }

    return values;
  }

  async created() {
    this.list = await this.$store.dispatch(
      'registryService/referenceBooks/fetchReferenceBook',
      {
        id: this.dictionary,
      }
    );
    this.loading = false;
  }
}
