import { ThemeEnum } from "../context";
import "./dark";
import "./light";
/**
 * 设置主题
 * @param {ThemeEnum} theme 主题类型
 * @param {} customVariables 需要自定义的主题变量对象集合
 * @param {string} elementId 当前节点id
 */

export var loadTheme = function loadTheme(theme, customVariables, elementId) {
  var _a;

  if (theme === void 0) {
    theme = ThemeEnum.dark;
  }

  if (customVariables === void 0) {
    customVariables = (_a = {}, _a[ThemeEnum.dark] = {}, _a[ThemeEnum.light] = {}, _a);
  }

  if (elementId === void 0) {
    elementId = '';
  } // 当前的结点 默认documentElement


  var currentDom = elementId ? document.getElementById(elementId) : document.documentElement; // 移除增加的自定义style标签

  var darkStyleDom = document.getElementById('dataThemeDark');
  var lightStyleDom = document.getElementById('dataThemeLight');
  darkStyleDom === null || darkStyleDom === void 0 ? void 0 : darkStyleDom.remove();
  lightStyleDom === null || lightStyleDom === void 0 ? void 0 : lightStyleDom.remove(); // 暗色系自定义变量

  if (theme === ThemeEnum.dark && customVariables[ThemeEnum.dark] && Object.keys(customVariables[ThemeEnum.dark]).length) {
    var element = document.createElement('style');
    element.id = 'dataThemeDark';
    element.type = 'text/css';
    var styleString = Object.entries(customVariables[ThemeEnum.dark]).map(function (_a) {
      var k = _a[0],
          v = _a[1];
      return k + ":" + v;
    }).join(';');
    element.innerText = "[data-theme='dark'] {" + styleString + ";}";
    currentDom === null || currentDom === void 0 ? void 0 : currentDom.append(element);
  } // 亮色系自定义变量


  if (theme === ThemeEnum.light && customVariables[ThemeEnum.light] && Object.keys(customVariables[ThemeEnum.light]).length) {
    var element = document.createElement('style');
    element.id = 'dataThemeLight';
    element.type = 'text/css';
    var styleString = Object.entries(customVariables[ThemeEnum.dark]).map(function (_a) {
      var k = _a[0],
          v = _a[1];
      return k + ":" + v;
    }).join(';');
    element.innerText = "[data-theme='light'] {" + styleString + ";}";
    currentDom === null || currentDom === void 0 ? void 0 : currentDom.append(element);
  } // 设置当前主题


  currentDom === null || currentDom === void 0 ? void 0 : currentDom.setAttribute('data-theme', theme);
};
/**
 * 获取当前主题
 * @returns {ThemeEnum} 当前主题
 */

export var getTheme = function getTheme(elementId) {
  var _a;

  if (elementId === void 0) {
    elementId = '';
  }

  if (elementId) {
    var theme_1 = (_a = document.getElementById(elementId)) === null || _a === void 0 ? void 0 : _a.getAttribute('data-theme');
    return theme_1 ? ThemeEnum[theme_1] : undefined;
  }

  var theme = document.documentElement.getAttribute('data-theme');
  return theme ? ThemeEnum[theme] : undefined;
};

