import { Component, Vue } from 'vue-property-decorator';
import { Option } from '@/recruits/models';
import { cloneDeep } from 'lodash';

export interface groupDataModelInterface {
  optgroup: string;
  type: string;
  children: Option[] | [];
}

@Component({})
export default class ActionSelectGroupData extends Vue {
  actionSelectGroupDataList(
    filterAny: [string] | null = null,
    filterNotAll: [string] | null = null
  ): groupDataModelInterface[] {
    const optionsObjFull =
      this.$store.getters[
        'ervuDictionaries/militaryRegistrationUpdatingGoalsDictionaryFull'
      ] || null;

    const groupNameObj =
      this.$store.getters[
        'ervuDictionaries/militaryRegistrationUpdatingGoalCategoriesDictionary'
      ] || null;

    const result = Object.entries(groupNameObj).map(
      (group): groupDataModelInterface => {
        return {
          optgroup: group[1] as string,
          type: group[0],
          children: [],
        };
      }
    );

    const optionsObj = cloneDeep(optionsObjFull);

    const filterOptionObj = (condition: any, isRemove = false) => {
      Object.keys(optionsObj)
        .filter((key) => condition.includes(key) === isRemove)
        .forEach((key) => delete optionsObj[key]);
    };

    if (filterAny) {
      filterOptionObj(filterAny);
    }

    if (filterNotAll) {
      filterOptionObj(filterNotAll, true);
    }

    Object.entries(optionsObj).map((option: any) => {
      const value = option[0] || '';
      const label = option[1].value;
      const type = option[1].type;

      for (let i = 0; i < result.length; i++) {
        if (type === result[i].type) {
          (result[i].children as Option[]).push({
            value,
            label,
          });
        }
      }
      return true;
    });

    return result;
  }
}
