"use strict";

var _postcss = _interopRequireDefault(require("postcss"));

var _fs = _interopRequireDefault(require("fs"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

module.exports = _postcss.default.plugin('autoprefixer', prefixConfig => {
  const prefixes = JSON.parse(_fs.default.readFileSync('./defaultPrefixes.json', 'utf-8'));
  const prefixVals = Object.keys(prefixes).filter(key => prefixes[key].key);
  const prefixValObj = {};
  prefixVals.forEach(val => {
    prefixValObj[prefixes[val].value] = prefixes[val];
  });
  return root => {
    root.walkAtRules(rule => {
      const previous = rule.parent.nodes[rule.parent.nodes.indexOf(rule) - 1];

      if (previous && previous.type === 'comment' && previous.text === 'prefix:ignore') {
        return;
      }

      if (rule.name === 'keyframes' && !prefixConfig.exclude.includes('@keyframes') && prefixConfig.include && (Object.keys(prefixConfig.include).length === 0 || Object.keys(prefixConfig.include).includes('@keyframes'))) {
        const newPrefixes = [];
        const replacements = prefixConfig.replacements && prefixConfig.replacements['@keyframes'] ? prefixConfig.replacements['@keyframes'] : {}; // if (!prefixes['@keyframes'] && !replacements) {
        //   return;
        // }

        const mozRule = rule.clone(); // custom value need to change

        mozRule.name = replacements && replacements.mozilla ? replacements.mozilla : prefixes['@keyframes'].mozilla;

        if (mozRule.name && prefixConfig.browsers && prefixConfig.browsers.mozilla) {
          if (!prefixConfig.include['@keyframes'].mozilla) {
            return;
          }

          newPrefixes.push(mozRule);
        }

        const chromeRule = rule.clone(); // custom value need to change

        chromeRule.name = replacements && replacements.chrome ? replacements.chrome : prefixes['@keyframes'].chrome;

        if (chromeRule.name && prefixConfig.browsers && prefixConfig.browsers.chrome) {
          if (!prefixConfig.include['@keyframes'].chrome) {
            return;
          }

          newPrefixes.push(chromeRule);
        }

        const operaRule = rule.clone(); // custom value need to change

        operaRule.name = replacements && replacements.opera ? replacements.opera : prefixes['@keyframes'].opera;

        if (operaRule.name && prefixConfig.browsers && prefixConfig.browsers.opera) {
          if (!prefixConfig.include['@keyframes'].opera) {
            return;
          }

          newPrefixes.push(operaRule);
        }

        const ieRule = rule.clone(); // custom value need to change

        ieRule.name = replacements && replacements.ie ? replacements.ie : prefixes['@keyframes'].ie;

        if (ieRule.name && prefixConfig.browsers && prefixConfig.browsers.ie) {
          if (!prefixConfig.include['@keyframes'].ie) {
            return;
          }

          newPrefixes.push(ieRule);
        }

        rule.parent.insertAfter(rule, newPrefixes); // console.log(`${rule.name} : ${rule.params}`);

        return;
      }
    });
    root.walkRules(rule => {
      rule.walkDecls(decl => {
        const previous = decl.parent.nodes[decl.parent.nodes.indexOf(decl) - 1];

        if (previous && previous.type === 'comment' && previous.text === 'prefix:ignore') {
          return;
        }

        if (Object.keys(prefixes).includes(decl.prop) && !prefixConfig.exclude.includes(decl.prop) && prefixConfig.include && (Object.keys(prefixConfig.include).length === 0 || Object.keys(prefixConfig.include).includes(decl.prop))) {
          const newPrefixes = [];
          const replacements = prefixConfig.replacements && prefixConfig.replacements[decl.prop] ? prefixConfig.replacements[decl.prop] : {}; // if (!prefixes[decl.prop] && !replacements) {
          //   return;
          // }

          const mozDecl = decl.clone(); // custom value need to change

          mozDecl.prop = replacements && replacements.mozilla ? replacements.mozilla : prefixes[decl.prop].mozilla;

          if (mozDecl.prop && prefixConfig.browsers && prefixConfig.browsers.mozilla) {
            if (!prefixConfig.include[decl.prop].mozilla) {
              return;
            }

            newPrefixes.push(mozDecl);
          }

          const chromeDecl = decl.clone(); // custom value need to change

          chromeDecl.prop = replacements && replacements.chrome ? replacements.chrome : prefixes[decl.prop].chrome;

          if (chromeDecl.prop && prefixConfig.browsers && prefixConfig.browsers.chrome) {
            if (!prefixConfig.include[decl.prop].chrome) {
              return;
            }

            newPrefixes.push(chromeDecl);
          }

          const operaDecl = decl.clone(); // custom value need to change

          operaDecl.prop = replacements && replacements.opera ? replacements.opera : prefixes[decl.prop].opera;

          if (operaDecl.prop && prefixConfig.browsers && prefixConfig.browsers.opera) {
            if (!prefixConfig.include[decl.prop].opera) {
              return;
            }

            newPrefixes.push(operaDecl);
          }

          const ieDecl = decl.clone(); // custom value need to change

          ieDecl.prop = replacements && replacements.ie ? replacements.ie : prefixes[decl.prop].ie;

          if (ieDecl.prop && prefixConfig.browsers && prefixConfig.browsers.ie) {
            if (!prefixConfig.include[decl.prop].ie) {
              return;
            }

            newPrefixes.push(ieDecl);
          }

          decl.parent.insertAfter(decl, newPrefixes); // console.log(`${decl.prop} : ${decl.value}`);
        }
      });
    });
  };
});