"use strict";

var _fileHandling = require("../plugins/utils/fileHandling");

var _getOptions = _interopRequireDefault(require("./getOptions"));

var _folderIterator = _interopRequireDefault(require("./folderIterator"));

var _variableConvertorUtils = require("../plugins/variableConvertorUtils");

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

const postcss = require('postcss');

const path = require('path');

const fs = require('fs');

const ignore = require('ignore');

const postcssVariableConvertor = require('../postcss-plugins/variableModificationPlugin/index').plugin;

const cwd = process.cwd();
const src = path.join(cwd, process.argv[2]);
const dist = path.join(cwd, process.argv[3]);
const options = (0, _getOptions.default)();

function watchHandler(fromPath, toPath) {
  const css = fs.readFileSync(fromPath, 'utf-8'); // console.log(css);

  const {
    css: cssOptions
  } = options; // console.log(cssOptions, appOptions);

  const {
    cssVariableReplacementConfig: cssVariableConfigFilePath,
    patterns: filterArr
  } = cssOptions;
  const rootOriginal = postcss.parse(css);
  const variables = {}; // const unassigned = {};

  const rawdata = fs.readFileSync(cssVariableConfigFilePath).toString();
  const cssVariableOptions = JSON.parse(rawdata);
  const {
    settings: settingsObject // errorLog: errorLogStatus,
    // errorInConsole: errorConsoleStatus,
    // errorsAllowed,
    // strictMode

  } = cssVariableOptions; //Variable Webpack Object Generation

  rootOriginal.walkRules(rule => {
    rule.walkDecls(decl => {
      decl.value.split(' ').forEach(val => {
        if (val && val.includes('--') && !new RegExp(_variableConvertorUtils.ignoreVals.join('|'), 'gi').test(val) && decl.prop) {
          const extractedValue = (0, _variableConvertorUtils.extractVariableName)(val);

          if (!variables[extractedValue]) {
            variables[extractedValue] = decl.prop;
          }
        }
      });
    });
  }); // if (
  //   !isFileNameMatchingPluginPattern({
  //     filename: fromPath,
  //     filterArr
  //   })
  // ) {
  //   return;
  // }

  const ig = ignore({
    allowRelativePaths: true
  }).add(filterArr);

  if (ig.ignores(fromPath)) {
    // console.log(fromPath);
    // console.log(ig.ignores(fromPath));
    // console.log(
    //   '----------------------------------------------------------------'
    // );
    return;
  } // Convert code


  const convertedCode = (0, _variableConvertorUtils.variableConverter)(rootOriginal, variables, settingsObject).toString(); // run postcss plugin for css code input

  postcss([postcssVariableConvertor(cssVariableConfigFilePath)]).process(convertedCode, {
    from: undefined
  }).then(result => {
    //write new css file data in file
    fs.writeFileSync(toPath, result.css);
  }); // console.log(variableOptions);
}

(0, _folderIterator.default)(src, dist, ['.css'], false, (fromPath, toPath) => {
  // console.log(fromPath, toPath);
  watchHandler(fromPath, toPath);
}); // if (canWacth) {
//   useExitCleanup(() => {
//     fs.unwatchFile(src, watchHandler);
//   });
// }