import { Component, Prop, Vue } from 'vue-property-decorator';
import { Schema } from '@devsun/eva-models';
import FiasAddressView from './FiasAddressView.vue';

@Component({
  components: {
    FiasAddressView,
  },
})
export default class TableFillView extends Vue {
  @Prop({ default: () => ({}) }) item: object;
  @Prop({ default: () => ({}) }) col: object;
  @Prop({ default: () => ({}) }) schema: Partial<Schema.MetaClassSchemaRaw>;
  @Prop({ default: false }) excludeHeaderColFromCardBody: boolean;

  get headerFieldName(): string | null {
    // @ts-ignore
    const field = this.schema.attrList.find(
      (field) => field?.props?.useValueAsHeader
    );

    return field?.name || null;
  }

  get fieldName(): string | null {
    return this.viewAttribute?.name || null;
  }

  get viewAttribute(): object | null {
    return (
      this?.schema?.attrList?.find((item) => item.name === this.col?.name) ||
      null
    );
  }

  get itemView(): string | null {
    return this.viewAttribute?.view || null;
  }

  get itemDictionary(): string | null {
    if (this.itemView !== 'dictionary') return null;

    // todo add actual typings
    // @ts-ignore
    return this.viewAttribute?.props?.dictionary || null;
  }

  get itemCustomTemplate(): string | null {
    return this.viewAttribute?.template || null;
  }

  displayValue(value: unknown): string {
    if (typeof value === 'boolean') {
      // @ts-ignore
      return value ? this.$t('base.yes') : this.$t('base.no');
    }

    // @ts-ignore
    return value;
  }
}
