const postcss = require('postcss')
const selectorParser = require('postcss-selector-parser')

const TAGS = Object.keys(require('@enncloud/enn-cli-shared').tags)

const transformSelector = (complexSelector, transformer) => {
  return selectorParser(transformer).processSync(complexSelector)
}
const isInsideKeyframes = function (rule) {
  return (
    rule.parent && rule.parent.type === 'atrule' && /^(-\w+-)?keyframes$/.test(rule.parent.name)
  )
}

let rewriteUrl

module.exports = postcss.plugin('postcss-uniapp-plugin', function (opts) {
  return function (root, result) {
    if (!rewriteUrl) {
      rewriteUrl = require('@enncloud/enn-cli-shared/lib/url-loader').rewriteUrl
    }
    rewriteUrl(root)

    root.walkRules(rule => {
      // Transform each rule here
      if (!isInsideKeyframes(rule)) {
        // rule.selectors == comma seperated selectors
        // a, b.c {} => ["a", "b.c"]
        rule.selectors = rule.selectors.map(complexSelector =>
          // complexSelector => simpleSelectors
          // "a.b#c" => ["a", ".b", "#c"]
          transformSelector(complexSelector, simpleSelectors => {
            // only process type selector, leave alone class & id selectors
            return simpleSelectors.walkTags(tag => {
              if (tag.value === 'page') {
                tag.value = 'body'
              } else if (~TAGS.indexOf(tag.value) && tag.value.substring(
                0, 4) !== 'uni-') {
                tag.value = 'uni-' + tag.value
              }
            })
          })
        )
      }
    })
  }
})
