"use strict";exports.__esModule = true;exports.EXECUTION_TIME = exports.COMPONENT_DESCRIPTIONS_BY_VARIABLE = exports.COMPONENT_DESCRIPTIONS = exports.CALLS_COUNT = exports.ALL_USED_VARIABLES = void 0;var _globalObject = require("@skbkontur/global-object");


var _LightTheme = require("../../lib/theming/themes/LightTheme");
var _DarkTheme = require("../../lib/theming/themes/DarkTheme");
var _Supports = require("../../lib/Supports");






















var callsCount = 0;
var executionTime = 0;
var ALL_USED_VARIABLES_SET = new Set();

var COMPONENT_DESCRIPTIONS = exports.COMPONENT_DESCRIPTIONS = {};
var COMPONENT_DESCRIPTIONS_BY_VARIABLE = exports.COMPONENT_DESCRIPTIONS_BY_VARIABLE = {};
if (_Supports.IS_PROXY_SUPPORTED) {
  var baseThemes = [];
  baseThemes.push(_LightTheme.LIGHT_THEME);
  baseThemes.push(_DarkTheme.DARK_THEME);

  // @ts-expect-error не очень-то и хотелось
  var componentsContext = require.context('../../../', true, /\.styles.ts$/);
  componentsContext.keys().forEach(function (fileName) {
    var fileNameStart = fileName.lastIndexOf('/') + 1;
    var componentName = fileName.substring(fileNameStart).replace('.styles.ts', '');
    var componentDescription = {};
    Object.keys(componentsContext(fileName)).forEach(function (exportName) {
      var styles = componentsContext(fileName)[exportName];

      Object.keys(styles).forEach(function (elementName) {
        var jsStyle = styles[elementName];
        if (typeof jsStyle !== 'function') {
          return;
        }
        var variablesAccumulator = new Set();
        var dependencies = {};
        var elementProxyHandler = getProxyHandler(variablesAccumulator, dependencies);
        var themes = baseThemes.map(function (t) {return new Proxy(t, elementProxyHandler);});
        themes.forEach(function (t) {return jsStyle(t);});

        var variables = Array.from(variablesAccumulator);

        if (variables.length > 0) {
          componentDescription[elementName] = { variables: variables, dependencies: dependencies };

          variables.forEach(function (variableName) {
            if (!COMPONENT_DESCRIPTIONS_BY_VARIABLE[variableName]) {
              COMPONENT_DESCRIPTIONS_BY_VARIABLE[variableName] = {};
            }

            var variableNode = COMPONENT_DESCRIPTIONS_BY_VARIABLE[variableName];
            if (!variableNode[componentName]) {
              variableNode[componentName] = {};
            }

            var componentNode = variableNode[componentName];
            if (!componentNode[elementName]) {
              componentNode[elementName] = {
                dependencies: dependencies,
                variables: [variableName]
              };
            } else if (!componentNode[elementName].variables.includes(variableName)) {
              componentNode[elementName].dependencies = dependencies;
              componentNode[elementName].variables.push(variableName);
            }

            var dependenciesList = dependencies[variableName];
            if (dependenciesList) {
              dependenciesList.forEach(function (dependencyName) {
                if (!COMPONENT_DESCRIPTIONS_BY_VARIABLE[dependencyName]) {
                  COMPONENT_DESCRIPTIONS_BY_VARIABLE[dependencyName] = {};
                }

                var dependencyNode = COMPONENT_DESCRIPTIONS_BY_VARIABLE[dependencyName];
                if (!dependencyNode[componentName]) {
                  dependencyNode[componentName] = COMPONENT_DESCRIPTIONS_BY_VARIABLE[variableName][componentName];
                }
              });
            }
          });
        }
      });
    });

    COMPONENT_DESCRIPTIONS[componentName] = componentDescription;
  });
}

function getProxyHandler(accumulator, dependencies) {
  var accessLevel = 0;
  var rootProp = '';
  function isThemeVariable(theme, name) {
    return typeof theme[name] === 'string';
  }
  return {
    get: function get(target, prop, receiver) {var _globalObject$perform, _globalObject$perform2;
      var propName = prop;
      if (isThemeVariable(target, propName)) {
        ALL_USED_VARIABLES_SET.add(propName);
        if (accessLevel === 0) {
          rootProp = propName;
          accumulator.add(propName);
        } else if (!dependencies[rootProp]) {
          dependencies[rootProp] = [propName];
        } else if (!dependencies[rootProp].includes(propName)) {
          dependencies[rootProp].push(propName);
        }
      }

      accessLevel++;
      var start = ((_globalObject$perform = _globalObject.globalObject.performance) == null ? void 0 : _globalObject$perform.now()) || 0;
      var result = Reflect.get(target, prop, receiver);
      executionTime += (((_globalObject$perform2 = _globalObject.globalObject.performance) == null ? void 0 : _globalObject$perform2.now()) || 0) - start;
      callsCount++;
      accessLevel--;
      return result;
    }
  };
}

var ALL_USED_VARIABLES = exports.ALL_USED_VARIABLES = Array.from(ALL_USED_VARIABLES_SET);
var CALLS_COUNT = exports.CALLS_COUNT = callsCount;
var EXECUTION_TIME = exports.EXECUTION_TIME = executionTime;