text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports["default"] = _default;
  exports.messages = exports.ruleName = void 0;
  
  var _stylelint = require("stylelint");
  
  var _lodash = require("lodash");
  
  var _utils = require("../../utils");
  
  var ruleName = (0, _utils.namespace)("no-duplicate-dollar-variables");
  exports.ruleName = ruleName;
  
  var messages = _stylelint.utils.ruleMessages(ruleName, {
    rejected: function rejected(variable) {
      return "Unexpected duplicate dollar variable ".concat(variable);
    }
  });
  
  exports.messages = messages;
  
  function _default(value, secondaryOptions) {
    return function (root, result) {
      var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
        actual: value
      }, {
        actual: secondaryOptions,
        possible: {
          ignoreInside: ["at-rule", "nested-at-rule"],
          ignoreInsideAtRules: [_lodash.isString]
        },
        optional: true
      });
  
      if (!validOptions) {
        return;
      }
  
      var vars = {};
      /**
       * Traverse the [vars] tree through the path defined by [ancestors], creating nodes as needed.
       *
       * Return the tree of the node defined by the last of [ancestors].
       */
  
      function getScope(ancestors) {
        var scope = vars;
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;
  
        try {
          for (var _iterator = ancestors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var node = _step.value;
  
            if (!(node in scope)) {
              scope[node] = {};
            }
  
            scope = scope[node];
          }
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator["return"] != null) {
              _iterator["return"]();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }
  
        return scope;
      }
      /**
       * Returns whether [variable] is declared anywhere in the scopes along the path defined by
       * [ancestors].
       */
  
  
      function isDeclared(ancestors, variable) {
        var scope = vars;
        var _iteratorNormalCompletion2 = true;
        var _didIteratorError2 = false;
        var _iteratorError2 = undefined;
  
        try {
          for (var _iterator2 = ancestors[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
            var node = _step2.value;
            scope = scope[node];
            if (scope[variable]) return true;
          }
        } catch (err) {
          _didIteratorError2 = true;
          _iteratorError2 = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
              _iterator2["return"]();
            }
          } finally {
            if (_didIteratorError2) {
              throw _iteratorError2;
            }
          }
        }
  
        return false;
      }
  
      root.walkDecls(function (decl) {
        var isVar = decl.prop[0] === "$";
        var isInsideIgnoredAtRule = decl.parent.type === "atrule" && secondaryOptions && secondaryOptions.ignoreInside && secondaryOptions.ignoreInside === "at-rule";
        var isInsideIgnoredNestedAtRule = decl.parent.type === "atrule" && decl.parent.parent.type !== "root" && secondaryOptions && secondaryOptions.ignoreInside && secondaryOptions.ignoreInside === "nested-at-rule";
        var isInsideIgnoredSpecifiedAtRule = decl.parent.type === "atrule" && secondaryOptions && secondaryOptions.ignoreInsideAtRules && secondaryOptions.ignoreInsideAtRules.includes(decl.parent.name);
  
        if (!isVar || isInsideIgnoredAtRule || isInsideIgnoredNestedAtRule || isInsideIgnoredSpecifiedAtRule) {
          return;
        }
  
        var ancestors = [];
        var parent = decl.parent;
  
        while (parent !== null && parent !== undefined) {
          var parentKey = parent.toString();
          ancestors.unshift(parentKey);
          parent = parent.parent;
        }
  
        var scope = getScope(ancestors);
  
        if (isDeclared(ancestors, decl.prop)) {
          _stylelint.utils.report({
            message: messages.rejected(decl.prop),
            node: decl,
            result: result,
            ruleName: ruleName
          });
        }
  
        scope[decl.prop] = true;
      });
    };
  }