"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getTheme = exports.loadTheme = void 0;

var context_1 = require("../context");

require("./dark");

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


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

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

  if (customVariables === void 0) {
    customVariables = (_a = {}, _a[context_1.ThemeEnum.dark] = {}, _a[context_1.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 === context_1.ThemeEnum.dark && customVariables[context_1.ThemeEnum.dark] && Object.keys(customVariables[context_1.ThemeEnum.dark]).length) {
    var element = document.createElement('style');
    element.id = 'dataThemeDark';
    element.type = 'text/css';
    var styleString = Object.entries(customVariables[context_1.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 === context_1.ThemeEnum.light && customVariables[context_1.ThemeEnum.light] && Object.keys(customVariables[context_1.ThemeEnum.light]).length) {
    var element = document.createElement('style');
    element.id = 'dataThemeLight';
    element.type = 'text/css';
    var styleString = Object.entries(customVariables[context_1.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);
};

exports.loadTheme = loadTheme;
/**
 * 获取当前主题
 * @returns {ThemeEnum} 当前主题
 */

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 ? context_1.ThemeEnum[theme_1] : undefined;
  }

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

exports.getTheme = getTheme;

