import client from "webpack-theme-color-replacer/client";
import forElementUI from "webpack-theme-color-replacer/forElementUI";
import store from "@/store";

export let curColor = "#6eb657";

// 动态切换主题色
export function changeThemeColor(newColor) {
  const options = {
    newColors: [
      ...forElementUI.getElementUISeries(newColor),
      "#ff0000",
      "#ffff00"
    ]
  };
  return client.changer.changeColor(options, Promise).then(t => {
    curColor = newColor;
    store.commit("SET_THEME", curColor);
    store.commit("SET_IS_PAGE_LOADING", false);
  });
}

export function initThemeColor() {
  const savedColor = store.getters.theme || curColor;
  if (savedColor) {
    curColor = savedColor;
    changeThemeColor(savedColor).then(t => {
      console.log("切换成功");
    });
  }
}
