import Vue from 'vue';
import initVue from '@assets/js/initVue.js';
import formMixin from '@mixins/formMixin';
import axiosApi, { param } from '@assets/js/axios-api';
const Type = require('../../catalog/view/javascript/types.js');

const AdmVue = Vue.extend({
  delimiters: ['{', '}'],
  components: {
    BaseAlert: () => import(/* webpackChunkName: "BaseAlert" */ '@elements/BaseAlert/index.vue'),
    BaseCard: () => import(/* webpackChunkName: "BaseCard" */ '@elements/BaseCard/index.vue'),
    TheHeader: () => import(/* webpackChunkName: "TheHeader" */ '@components/TheHeader/index.vue'),
  },
  mixins: [formMixin],
  data() {
    return {
      isLoading: true,
      alert: {},
      tabs: [],
    };
  },
  mounted() {
    this.isLoading = false;
    this.console = window.console;
  },
  methods: {
    axiosApi,
    param,
    cloneObject(value) {
      return JSON.parse(JSON.stringify(value));
    },
    applyModel(models, value, parentObject) {
      parentObject = parentObject || this;
      if(typeof models != 'undefined'){
        for(const key in models){
          if (typeof value[key] == 'undefined'){
            continue;
          }
          this.$set(parentObject, key, models[key].format(value[key]));
          delete value[key];
        }
      }
    },
    setData(data) {

      const { configurations, ...restData } = data;

      if(configurations){
        this.applyModel(this.configurationModels, configurations, this.configurations);

        for(const key in configurations){
          this.$set(this.configurations, key, configurations[key]);
        }
      }

      this.applyModel(this.models, restData);

      Object.assign(this, restData);
      const { TypesForJs } = restData;
      if (TypesForJs) {
        for (let i = 0; i < TypesForJs.length; i++) {
          this[TypesForJs[i].name] = new Type(TypesForJs[i]);
        }
      }
    },
    onChangeContentTab(newTabs) {
      // Não implementar aqui
      // Deve ser implementado no na instancia caso a pagina tenha abas
    },
    parseBoolean(value) {
      return Boolean(Number(value));
    },
  }
});

AdmVue.init = initVue;

export default AdmVue;
