'use strict'

let options
const config = {}
const corePlugin = require('./plugin.js')

function sort (arr) {
  return arr.sort((a, b) => a.priority - b.priority)
}

function optionOrDefault (option, def) {
  return option in options ? options[option] : def
}

function addKey (key, def) {
  config[key] = optionOrDefault(key, def)
}

function main (opts, plugins, hooks) {
  options = opts || {}
  hooks = hooks || {}
  addKey('autoRename', false)
  addKey('autoRenameStrict', false)
  addKey('blacklist', {})
  addKey('clean', true)
  addKey('greedy', false)
  addKey('processUrls', false)
  addKey('stringMap', [])
  addKey('useCalc', false)
  addKey('aliases', {})
  addKey('processEnv', true)

  // default strings map
  if (Array.isArray(config.stringMap)) {
    let hasLeftRight, hasLtrRtl
    for (const map of config.stringMap) {
      if (hasLeftRight && hasLtrRtl) {
        break
      } else if (map.name === 'left-right') {
        hasLeftRight = true
      } else if (map.name === 'ltr-rtl') {
        hasLtrRtl = true
      }
    }

    if (!hasLeftRight) {
      config.stringMap.push({
        name: 'left-right',
        priority: 100,
        exclusive: false,
        search: ['left', 'Left', 'LEFT'],
        replace: ['right', 'Right', 'RIGHT'],
        options: { scope: '*', ignoreCase: false }
      })
    }

    if (!hasLtrRtl) {
      config.stringMap.push({
        name: 'ltr-rtl',
        priority: 100,
        exclusive: false,
        search: ['ltr', 'Ltr', 'LTR'],
        replace: ['rtl', 'Rtl', 'RTL'],
        options: { scope: '*', ignoreCase: false }
      })
    }

    sort(config.stringMap)
  }

  // plugins
  config.plugins = []

  if (Array.isArray(plugins)) {
    if (!plugins.some((plugin) => plugin.name === 'rtlcss')) {
      config.plugins.push(corePlugin)
    }

    config.plugins = config.plugins.concat(plugins)
  } else if (!plugins || plugins.name !== 'rtlcss') {
    config.plugins.push(corePlugin)
  }

  sort(config.plugins)

  // hooks
  config.hooks = {
    pre () {},
    post () {}
  }

  if (typeof hooks.pre === 'function') {
    config.hooks.pre = hooks.pre
  }

  if (typeof hooks.post === 'function') {
    config.hooks.post = hooks.post
  }

  return config
}

module.exports.configure = main

@@ -11,38 +11,8 @@
 t'%0A%0A
-let options%0Aconst config = %7B%7D%0A
 cons
@@ -54,250 +54,297 @@
 ')%0A%0A
-function sort (arr) %7B%0A  return arr.sort((a, b) =%3E a.priority - b.priority)%0A%7D%0A%0Afunction optionOrDefault (option, def) %7B%0A  return option in options ? options%5Boption%5D : def%0A%7D%0A%0Afunction addKey (key, def) %7B%0A  config%5Bkey%5D = optionOrDefault(key, def
+const defaultOptions = %7B%0A  autoRename: false,%0A  autoRenameStrict: false,%0A  blacklist: %7B%7D,%0A  clean: true,%0A  greedy: false,%0A  processUrls: false,%0A  stringMap: %5B%5D,%0A  useCalc: false,%0A  aliases: %7B%7D,%0A  processEnv: true%0A%7D%0A%0Afunction sort (arr) %7B%0A  return arr.sort((a, b) =%3E a.priority - b.priority
 )%0A%7D%0A
@@ -388,19 +388,16 @@
  %7B%0A  opt
-ion
 s = opts
@@ -429,286 +429,56 @@
  %7B%7D%0A
-  addKey('autoRename', false)%0A  addKey('autoRenameStrict', false)%0A  addKey('blacklist', %7B%7D)%0A  addKey('clean', true)%0A  addKey('greedy', false)%0A  addKey('processUrls', false)%0A  addKey('stringMap', %5B%5D)%0A  addKey('useCalc', false)%0A  addKey('aliases', %7B%7D)%0A  addKey('processEnv', true)
+%0A  const config = %7B ...defaultOptions, ...opts %7D
 %0A%0A  

use object spread