'use strict';

var themes = require('primevue/themes');

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var index = {
  defaults: {
    variable: {
      prefix: 'p',
      selector: ':root',
      excludedKeyRegex: /^(primitive|semantic|components|directives|variables|colorscheme|light|dark|common|root|states)$/gi
    },
    options: {
      prefix: 'p',
      darkModeSelector: 'system',
      cssLayer: false
    }
  },
  _theme: undefined,
  _layerNames: new Set(),
  _loadedStyleNames: new Set(),
  _loadingStyles: new Set(),
  _tokens: {},
  update: function update() {
    var newValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var theme = newValues.theme;
    if (theme) {
      this._theme = _objectSpread(_objectSpread({}, theme), {}, {
        options: _objectSpread(_objectSpread({}, this.defaults.options), theme.options)
      });
      this._tokens = themes.ThemeUtils.createTokens(this.preset, this.defaults);
      this.clearLoadedStyleNames();
    }
  },
  get theme() {
    return this._theme;
  },
  get preset() {
    var _this$theme;
    return ((_this$theme = this.theme) === null || _this$theme === void 0 ? void 0 : _this$theme.preset) || {};
  },
  get options() {
    var _this$theme2;
    return ((_this$theme2 = this.theme) === null || _this$theme2 === void 0 ? void 0 : _this$theme2.options) || {};
  },
  get tokens() {
    return this._tokens;
  },
  getTheme: function getTheme() {
    return this.theme;
  },
  setTheme: function setTheme(newValue) {
    this.update({
      theme: newValue
    });
    themes.ThemeService.emit('theme:change', newValue);
  },
  getPreset: function getPreset() {
    return this.preset;
  },
  setPreset: function setPreset(newValue) {
    this._theme = _objectSpread(_objectSpread({}, this.theme), {}, {
      preset: newValue
    });
    this._tokens = themes.ThemeUtils.createTokens(newValue, this.defaults);
    this.clearLoadedStyleNames();
    themes.ThemeService.emit('preset:change', newValue);
    themes.ThemeService.emit('theme:change', this.theme);
  },
  getLayerNames: function getLayerNames() {
    return _toConsumableArray(this._layerNames);
  },
  setLayerNames: function setLayerNames(layerName) {
    this._layerNames.add(layerName);
  },
  getLoadedStyleNames: function getLoadedStyleNames() {
    return this._loadedStyleNames;
  },
  isStyleNameLoaded: function isStyleNameLoaded(name) {
    return this._loadedStyleNames.has(name);
  },
  setLoadedStyleName: function setLoadedStyleName(name) {
    this._loadedStyleNames.add(name);
  },
  deleteLoadedStyleName: function deleteLoadedStyleName(name) {
    this._loadedStyleNames["delete"](name);
  },
  clearLoadedStyleNames: function clearLoadedStyleNames() {
    this._loadedStyleNames.clear();
  },
  getTokenValue: function getTokenValue(tokenPath) {
    return themes.ThemeUtils.getTokenValue(this.tokens, tokenPath, this.defaults);
  },
  getCommonCSS: function getCommonCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var params = arguments.length > 1 ? arguments[1] : undefined;
    return themes.ThemeUtils.getCommon({
      name: name,
      theme: this.theme,
      params: params,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    });
  },
  getComponentCSS: function getComponentCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var params = arguments.length > 1 ? arguments[1] : undefined;
    var options = {
      name: name,
      theme: this.theme,
      params: params,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    };
    return {
      variables: themes.ThemeUtils.getPresetC(options)
    };
  },
  getDirectiveCSS: function getDirectiveCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var params = arguments.length > 1 ? arguments[1] : undefined;
    var options = {
      name: name,
      theme: this.theme,
      params: params,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    };
    return {
      variables: themes.ThemeUtils.getPresetD(options)
    };
  },
  getPresetCSS: function getPresetCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var preset = arguments.length > 1 ? arguments[1] : undefined;
    var selector = arguments.length > 2 ? arguments[2] : undefined;
    var params = arguments.length > 3 ? arguments[3] : undefined;
    var options = {
      name: name,
      preset: preset,
      options: this.options,
      selector: selector,
      params: params,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    };
    return {
      variables: themes.ThemeUtils.getPreset(options)
    };
  },
  getLayerOrderCSS: function getLayerOrderCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    return themes.ThemeUtils.getLayerOrder(name, this.options, {
      names: this.getLayerNames()
    }, this.defaults);
  },
  transformCSS: function transformCSS() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var css = arguments.length > 1 ? arguments[1] : undefined;
    var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'style';
    var mode = arguments.length > 3 ? arguments[3] : undefined;
    return themes.ThemeUtils.transformCSS(name, css, mode, type, this.options, {
      layerNames: this.setLayerNames.bind(this)
    }, this.defaults);
  },
  getCommonStyleSheet: function getCommonStyleSheet() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    var params = arguments.length > 1 ? arguments[1] : undefined;
    var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    return themes.ThemeUtils.getCommonStyleSheet({
      name: name,
      theme: this.theme,
      params: params,
      props: props,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    });
  },
  getStyleSheet: function getStyleSheet(name, params) {
    var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    return themes.ThemeUtils.getStyleSheet({
      name: name,
      theme: this.theme,
      params: params,
      props: props,
      defaults: this.defaults,
      set: {
        layerNames: this.setLayerNames.bind(this)
      }
    });
  },
  onStyleMounted: function onStyleMounted(name) {
    this._loadingStyles.add(name);
  },
  onStyleUpdated: function onStyleUpdated(name) {
    this._loadingStyles.add(name);
  },
  onStyleLoaded: function onStyleLoaded(event, _ref) {
    var name = _ref.name;
    if (this._loadingStyles.size) {
      this._loadingStyles["delete"](name);
      themes.ThemeService.emit("theme:".concat(name, ":load"), event); // Exp: ThemeService.emit('theme:panel-style:load', event)
      !this._loadingStyles.size && themes.ThemeService.emit('theme:load');
    }
  }
};

module.exports = index;
