import AdmVue from '@assets/js/adm-vue';

const vm = new AdmVue({
  components: {
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
  },
  data() {
    return {
      configurations: {},
    };
  },
  mounted() {
    this.isLoading = false;
  },
  methods: {
    addModule() {
      this.configurations.category_module.unshift({});
    },
    removeModule(index) {
      this.configurations.category_module.splice(index, 1);
    },
    filterSubCategory(index) {
      const category_module = this.cloneObject(this.configurations.category_module);
      const layoutId = category_module[index].layout_id;
      const filterSubCategory = category_module[index].filtersubcategory;

      if (filterSubCategory != 1) {
        return;
      }

      if (layoutId == Type_IdLayout.Tp_Idcategory || layoutId == Type_IdLayout.Tp_IdSearch) {
        category_module[index].filtersubcategory = 0;
      } else {
        delete category_module[index].filtersubcategory;
      }

      this.configurations = {...this.configurations, category_module};
    },
    duplicatedItem(index) {
      const category_module = this.cloneObject(this.configurations.category_module);
      const item = category_module[index];

      const duplicated = category_module.filter(({layout_id, position}, itemIndex) => layout_id == item.layout_id && position == item.position && itemIndex != index );
      if (duplicated.length) {
        const errors = {};
        errors[`category_module[${index}]layout_id`] = ['Não é permitido adicionar categorias no mesmo layout e posição'];
        this.$refs.validationObserver.setErrors(errors);
      } else {
        this.$refs.validationObserver.reset();
      }
    },
  }
});

AdmVue.init(vm);