import { Component, Watch } from 'vue-property-decorator';
import { ReferenceBooksItemOption } from '@/components/common/reference-book-item';
// todo importing types
// import DictionarySelect from "../../../DictionarySelect";
import DictionarySelect from '../../../DictionarySelect.vue';

// https://jira.devsun.ru/browse/PGS-312
const variantA = ['1', '2'];
const variantB = ['3', '4', '5', '6', '7', '8', '9', '10'];
const variantC = [...variantB, '11'];

enum CheckType {
  DOC = 'документарной',
  EXT = 'выездной',
}

enum CheckKind {
  PLAN = 'плановой',
  UNPLAN = 'внеплановой',
}

@Component({})
export default class Goal extends DictionarySelect {
  get disable(): boolean {
    // todo importing types
    // @ts-ignore
    return !this.model.typeCheck || !this.model.kindCheck;
  }

  @Watch('model.typeCheck')
  @Watch('model.kindCheck')
  onTypeChange(): void {
    // @ts-ignore
    const kind: any = this.model?.kindCheck;
    // @ts-ignore
    const type: any = this.model?.typeCheck;

    if (type && kind) {
      let variant: string[] = [];

      if (
        kind === CheckKind.PLAN &&
        [CheckType.DOC, CheckType.EXT].includes(type)
      ) {
        variant = variantA;
      }

      if (kind === CheckKind.UNPLAN && type === CheckType.DOC) {
        variant = variantB;
      }

      if (kind === CheckKind.UNPLAN && type === CheckType.EXT) {
        variant = variantC;
      }

      // @ts-ignore
      this.$set(
        this,
        'options',
        this.filterOptionsByVariant(this.originalOptions, variant)
      );

      return;
    }

    // @ts-ignore
    this.$set(this, 'options', this.originalOptions);
  }

  private filterOptionsByVariant(
    options: ReferenceBooksItemOption[],
    variant: string[]
  ): ReferenceBooksItemOption[] {
    if (!variant) return options;

    return options.filter((option) =>
      variant.includes(option?.value as string)
    );
  }
}
