const path = require('path')
const parse = require('./parsers/index')
// TODO Fix ampersand in selectors
// TODO ENFORCE THESE RULES
// value-no-vendor-prefix – don't allow vendor prefixes
// property-no-vendor-prefix – don't allow vendor prefixes

const ignoredRules = [
  // Don't throw if there's no styled-components in a file
  'no-empty-source',
  // We don't care about end-of-source newlines, users cannot control them
  'no-missing-end-of-source-newline'
]

const sourceMapsCorrections = {}

module.exports = (/* options */) => ({
  // Get string for stylelint to lint
  code(input, filepath) {
    const absolutePath = path.resolve(process.cwd(), filepath)
    sourceMapsCorrections[absolutePath] = {}
    const { extractedCSS, sourceMap } = parse(input, absolutePath)
    // Save source location, merging existing corrections with current corrections
    sourceMapsCorrections[absolutePath] = Object.assign(
      sourceMapsCorrections[absolutePath],
      sourceMap
    )
    return extractedCSS
  },
  // Fix sourcemaps
  result(stylelintResult, filepath) {
    const lineCorrection = sourceMapsCorrections[filepath]
    const newWarnings = stylelintResult.warnings.reduce((prevWarnings, warning) => {
      if (ignoredRules.includes(warning.rule)) return prevWarnings
      const correctedWarning = Object.assign(warning, {
        // Replace "brace" with "backtick" in warnings, e.g.
        // "Unexpected empty line before closing backtick" (instead of "brace")
        text: warning.text.replace(/brace/, 'backtick'),
        line: lineCorrection[warning.line]
      })
      prevWarnings.push(correctedWarning)
      return prevWarnings
    }, [])

    if (newWarnings.length === 0) {
      // eslint-disable-next-line no-param-reassign
      stylelintResult.errored = false
    }

    return Object.assign(stylelintResult, { warnings: newWarnings })
  }
})

@@ -101,369 +101,8 @@
 tors
-%0A// TODO ENFORCE THESE RULES%0A// value-no-vendor-prefix %E2%80%93 don't allow vendor prefixes%0A// property-no-vendor-prefix %E2%80%93 don't allow vendor prefixes%0A%0Aconst ignoredRules = %5B%0A  // Don't throw if there's no styled-components in a file%0A  'no-empty-source',%0A  // We don't care about end-of-source newlines, users cannot control them%0A  'no-missing-end-of-source-newline'%0A%5D
 %0A%0Aco
@@ -772,20 +772,17 @@
   const 
-newW
+w
 arnings 
@@ -812,141 +812,28 @@
 ngs.
-reduce((prevWarnings, 
+map(
 warning
-)
  =%3E
- %7B%0A 
+%0A
      
-if (ignoredRules.includes(warning.rule)) return prevWarnings%0A      const correctedWarning =
  Obj
@@ -1112,215 +1112,9 @@
     
-  prevWarnings.push(correctedWarning)%0A      return prevWarnings%0A    %7D, %5B%5D)%0A%0A    if (newWarnings.length === 0) %7B%0A      // eslint-disable-next-line no-param-reassign%0A      stylelintResult.errored = false%0A    %7D
+)
 %0A%0A  
@@ -1167,21 +1167,8 @@
 ings
-: newWarnings
  %7D)%0A

Remove ignoring of stylelint rules