'use strict';

var helpers = require('../helpers'),
    capitalize = require('lodash.capitalize'),
    kebabcase = require('lodash.kebabcase');

// Our nestable selector types, separated by type for ease of use with rules
// we replace ident with 'selector' for readability'
var nestableElements = ['selector', 'class', 'id'],
    nestableAttributes = ['attribute'],
    nestablePseudo = ['pseudoClass', 'pseudoElement', 'nth', 'nthSelector'];

/**
 * Formats a string from camelCase to hyphens and capitalizes when not
 * nth related
 * @param {string} str - The string to be formatted
 * @returns {string} A hyphenated (and possibly capitalized) string
 */
var formatOutput = function (str) {
  str = kebabcase(str);

  if (str.indexOf('nth') === -1) {
    str = capitalize(str);
  }

  return str;
};

module.exports = {
  'name': 'force-pseudo-nesting',
  'defaults': {},
  'detect': function (ast, parser) {
    var result = [],
        elements = nestableElements.concat(nestableAttributes, nestablePseudo);

    ast.traverseByType('ruleset', function (ruleset) {
      var delimOrder = [];

      ruleset.forEach('selector', function (selector) {
        // Where we'll store the previous value
        var previousVal;

        // Keep track of the order of elements & delimeters
        selector.forEach(function (el) {
          var curNode = helpers.mapDelims(el);

          if (curNode !== false) {
            delimOrder.push(curNode);
          }
        });

        selector.forEach('simpleSelector', function (simpleSelector) {
          // check if the next selector is proceeded by a delimiter
          // if it is add it to the curSelector output
          var nextType = delimOrder[0];

          if (nextType === 'd') {
            // Empty the previous value store
            previousVal = null;

            // remove the next delim and selector from our order as we are only
            // looping over selectors
            delimOrder.splice(0, 2);
          }
          else {
            // if no delim then just remove the current selectors marker in the
            // order array
            delimOrder.splice(0, 1);
          }

          // Loop each selector while checking if it should be nested
          simpleSelector.forEach(function (node) {
            // Construct the selector and store the current selector value
            var constructedSelector = helpers.constructSelector(node),
                currentVal = constructedSelector.type;

            if (helpers.isNestable(currentVal, previousVal, elements, nestablePseudo)) {
              helpers.addUnique(result, {
                'ruleId': parser.rule.name,
                'line': node.start.line,
                'column': node.start.column,
                'message': formatOutput(currentVal) + ' `' + constructedSelector.content + '` should be nested within its parent ' + previousVal,
                'severity': parser.severity
              });
            }

            if (currentVal) {
              // Store current value as previous and continue with the loop
              previousVal = currentVal;
            }
          });
        });
      });
    });

    return result;
  }
};

@@ -317,16 +317,32 @@
 s', 'id'
+, 'typeSelector'
 %5D,%0A    n
@@ -372,16 +372,24 @@
 ttribute
+Selector
 '%5D,%0A    
@@ -1090,35 +1090,8 @@
 t) %7B
-%0A      var delimOrder = %5B%5D;
 %0A%0A  
@@ -1156,1378 +1156,141 @@
     
-// Where we'll store the previous value%0A        var previousVal;%0A%0A        // Keep track of the order of elements & delimeters%0A        selector.forEach(function (el) %7B%0A          var curNode = helpers.mapDelims(el);%0A%0A          if (curNode !== false) %7B%0A            delimOrder.push(curNode);%0A          %7D%0A        %7D);%0A%0A        selector.forEach('simpleSelector', function (simpleSelector) %7B%0A          // check if the next selector is proceeded by a delimiter%0A          // if it is add it to the curSelector output%0A          var nextType = delimOrder%5B0%5D;%0A%0A          if (nextType === 'd') %7B%0A            // Empty the previous value store%0A            previousVal = null;%0A%0A            // remove the next delim and selector from our order as we are only%0A            // looping over selectors%0A            delimOrder.splice(0, 2);%0A          %7D%0A          else %7B%0A            // if no delim then just remove the current selectors marker in the%0A            // order array%0A            delimOrder.splice(0, 1);%0A          %7D%0A%0A          // Loop each selector while checking if it should be nested%0A          simpleSelector.forEach(function (node) %7B%0A            // Construct the selector and store the current selector value%0A            var constructedSelector = helpers.constructSelector(node),%0A                currentVal = constructedSelector.type;%0A%0A            if (helpers.isNestable(currentVal
+var previousVal;%0A        selector.forEach(function (item) %7B%0A          if (previousVal) %7B%0A            if (helpers.isNestable(item.type
 , pr
@@ -1298,16 +1298,21 @@
 viousVal
+.type
 , elemen
@@ -1444,20 +1444,24 @@
 'line': 
-node
+selector
 .start.l
@@ -1495,12 +1495,16 @@
 n': 
-node
+selector
 .sta
@@ -1558,62 +1558,22 @@
 put(
-currentVal) + ' %60' + constructedSelector.content
+item.type)
  + '
-%60 
 shou
@@ -1618,16 +1618,21 @@
 viousVal
+.type
 ,%0A      
@@ -1701,17 +1701,16 @@
       %7D%0A
-%0A
         
@@ -1715,170 +1715,38 @@
     
-  if (currentVal) %7B%0A              // Store current value as previous and continue with the loop%0A              previousVal = currentVal;%0A            %7D%0A          %7D)
+%7D%0A          previousVal = item
 ;%0A  

Fix force pseudo nesting