import { app } from '@/store/mutation-types';
import { getConfig } from '@/utils/auth';
import { conversionSize } from '@/utils/common';

const getDefaultSate = () => {
  const config = getConfig();
  return {
    showProgress: false,
    uploadController: null,
    uploadInfo: {
      loaded: 0,
      progress: 0,
      total: 0,
    },
    config: {
      logo_image_url: config?.logo_image_url ?? '',
    },
  };
};

const state = getDefaultSate();

const mutations = {
  [app.SHOW_PROGRESS]: (state, isShow) => {
    state.showProgress = isShow;
  },
  [app.RESET_UPLOAD_INFO]: (state) => {
    state.uploadInfo = {
      loaded: 0,
      progress: 0,
      total: 0,
    };
  },
  [app.SET_UPLOAD_CONTROLLER]: (state, controller) => {
    state.uploadController = controller;
  },
  [app.SET_UPLOAD_INFO]: (state, { loaded, progress, total }) => {
    state.uploadInfo = {
      loaded: conversionSize(loaded),
      progress: parseInt(String(progress * 100).replace(/(\d+)\.\d+/, '$1')),
      total: conversionSize(total),
    };
  },
};

const actions = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
