import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import { APIV2Api } from '@openapi/fias';

const ApiV2Api = new APIV2Api();
@Component({})
export default class FiasAddressView extends Vue {
  @Prop() id!: string;

  loading = true;
  fias: any = {};

  get outValue(): string | null {
    return this.fias?.fullname || null;
  }

  @Watch('id', { immediate: true })
  async updateFias(newId: string): Promise<void> {
    if (newId) {
      this.loading = true;

      this.fias = await ApiV2Api.searchById(newId).catch(() => {
        console.error(`[fias][dispatchSearchById][${newId}] not found`);
      });
      this.$emit('load', this.fias);
      this.loading = false;
    }
  }
}
