"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.selectorWeightPrefixAdder = selectorWeightPrefixAdder;

var _classHandling = require("./classHandling");

function selectorWeightPrefixAdder(rootOriginal, selector, excludeStrings) {
  rootOriginal.walkRules((rule, index) => {
    const prevNode = rootOriginal.nodes[index - 1];

    if ((0, _classHandling.hasPrevNodeIgnore)(index, prevNode, 'updateselector:ignore')) {
      return;
    } // TODO: need to verify  cases
    // if (!isInsideMediaQuery(rule)) {
    //   return;
    // }


    const selectorArr = rule.selector.split(',');
    selectorArr.forEach((singleSelectorStr, index) => {
      if (excludeStrings.includes(singleSelectorStr.trim())) {
        return;
      }

      if (singleSelectorStr.includes(']')) {
        if (singleSelectorStr.slice(singleSelectorStr.lastIndexOf(']') + 2).trim() === '') {
          return;
        }

        selectorArr[index] = `${singleSelectorStr.slice(0, singleSelectorStr.lastIndexOf(']') + 1).trim()} ${selector}${singleSelectorStr.slice(singleSelectorStr.lastIndexOf(']') + 2).trim()}`;
        return;
      }

      selectorArr[index] = `${selector}${singleSelectorStr}`;
    }); // eslint-disable-next-line no-param-reassign

    rule.selector = selectorArr.join(',\n');
  });
  return rootOriginal;
} // function convertSelectorWithoutAttribute (selector, conjunction) {
//   const front  = selector
//   .slice(0, selector.lastIndexOf(']') + 1)
//   .trim();
//   const back = selector
//   .slice(selector.lastIndexOf(']') + 2)
//   .trim();
// return `${front} ${conjunction}${back}`;
// }