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

const vm = new AdmVue({
  components: {
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    BaseTabs: () => import(/* webpackChunkName: "BaseTabs" */ '@components/BaseTabs/index.vue'),
    BaseButton: () => import(/* webpackChunkName: "BaseButton" */ '@elements/BaseButton/index.vue'),
    BaseSelectSearch: () => import(/* webpackChunkName: "BaseSelectSearch" */ '@elements/BaseSelectSearch/index.vue'),
  },
  data() {
    return {
      configurations: {
        special_action_title: '',
        special_action_item: '',
        special_action_module: [],
        special_action_promotions_selected: [],
      },
      layouts: [],
      disableForm: false,
      loadingBtn: false,
    };
  },
  methods: {
    convertToArray(value) {
      if (Array.isArray(value)) {
        return value;
      }

      return Object.values(value);
    },
    handleAddModuleClick() {
      let sortOrder = 0;
      const currentModule = this.convertToArray(this.configurations.special_action_module);
      const lastItemInCurrentModule = currentModule[currentModule.length - 1];

      if (lastItemInCurrentModule) {
        const { sort_order } = lastItemInCurrentModule;
        sortOrder = Number(sort_order) + 1;
      }

      currentModule.push({
        limit: '24',
        sort_order: sortOrder,
      });

      this.configurations.special_action_module = currentModule;
    },
    handleRemoveModuleClick(moduleIndex = 0) {
      const currentModule = this.convertToArray(this.configurations.special_action_module);
      const isModuleEmpty = currentModule.length <= 0;

      if (isModuleEmpty) return;

      currentModule.splice(moduleIndex, 1);
      this.configurations.special_action_module = currentModule;
    },
    handleSpecialActionProductCallback(value) {
      if (!value) return;

      const { special_action_options: allOptions = [] } = this;
      const promotionSelected = this.configurations.special_action_promotions_selected || [];

      const optionSelected = allOptions.find(item => item.id === value);
      const isAlreadySelected = promotionSelected.some(item => item.id === value);

      if (!optionSelected || isAlreadySelected) {
        return isAlreadySelected ? alert('Este produto já foi adicionado a lista!') : '';
      }

      promotionSelected.push(optionSelected);

      this.configurations.special_action_promotions_selected = promotionSelected;
      this.configurations.special_action_item = promotionSelected.map(item => item.id).toString();

      return;
    },
    handleSpecialActionProductRemove(id) {
      const { special_action_promotions_selected } = this.configurations;
      const promotionIndex = special_action_promotions_selected.findIndex(promotion => promotion.id === id);

      special_action_promotions_selected.splice(promotionIndex, 1);
      this.configurations.special_action_item = special_action_promotions_selected.map(item => item.id).toString();
    },
  },
  mounted() {
    if (this.configurations.special_action_item && this.special_action_options.length > 0) {
      const allItems = this.configurations.special_action_item.split(',');
      const filterPromotion = this.special_action_options.filter(option => allItems.includes(option.id));

      this.configurations.special_action_promotions_selected = filterPromotion;
    }

    if (this.configurations.special_action_module) {
      this.configurations.special_action_module = this.convertToArray(this.configurations.special_action_module).map(
        item => ({
          ...item,
          limit: item.limit || '24',
        })
      );
    }
  },
});

initVue(vm);
