var fs = require('fs')
var path = require('path')
var assign = require('deep-assign')
if (!process) {
  process = require('process') // > v0.4.0
}

var rc = require('rc-loader')
var repeatString = require('repeat-string')
var editorconfig = require('editorconfig')

var defaultIndentWidth = repeatString(' ', 2)

function loadStylelint(from) {
  var ret = {}
  var configNames = ['stylelint', 'stylelint.config']
  configNames.forEach(function (configName) {
    assign(ret, rc(configName, {}, { config: path.join(from, configName) }).rules)
  })
  return ret
}

function getIndentWidthFromEditorConfig(from, hasScss) {
  var config
  if (hasScss) {
    config = editorconfig.parseSync(path.resolve(process.cwd(), '*.scss'))
  } else {
    config = editorconfig.parseSync(path.resolve(process.cwd(), '*.css'))
  }
  if (config == undefined) {
    return defaultIndentWidth
  }
  switch (config.indent_style) {
    case 'tab':
      return '\t'
    case 'space':
      return repeatString(' ', config.indent_size)
    default:
      return defaultIndentWidth
  }
}

function isEmptyObject(obj) {
  for (var name in obj) {
    return false
  }
  return true
}

function hasScssInWorkingDir(wd) {
  var dirs = fs.readdirSync(wd)
  return dirs.some(function (dir) {
    return dir.match(/.*\.scss/)
  })
}

function newParmas(from) {
  var wd = path.dirname(from) || process.cwd()
  var params = {}

  var hasScss = hasScssInWorkingDir(wd)
  params.hasScss = hasScss

  var stylelintConfig = loadStylelint(wd)
  if (isEmptyObject(stylelintConfig)) {
    params.indentWidth = getIndentWidthFromEditorConfig(wd, hasScss)
    params.stylelint = {}
    return params
  }

  var indentaiton = stylelintConfig['indentation']
  switch (typeof indentaiton) {
    case 'string':
      params.indentWidth = '\t'
      break
    case 'number':
      params.indentWidth = repeatString(' ', indentaiton)
      break
    default:
      params.indentWidth = defaultIndentWidth
      break
  }
  params.stylelint = stylelintConfig
  return params
}

module.exports = newParmas

@@ -1670,18 +1670,18 @@
  indenta
-i
 t
+i
 on = sty
@@ -1732,18 +1732,18 @@
  indenta
-i
 t
+i
 on) %7B%0A  
@@ -1874,18 +1874,18 @@
  indenta
-i
 t
+i
 on)%0A    

Fix typo