"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractVariableName = extractVariableName;
exports.ignoreVals = void 0;
exports.variableConverter = variableConverter;

var _classHandling = require("./utils/classHandling");

const ignoreVals = ['--zd_size', '--zd_font_size', '--size', '--size_']; // const avoidProps = [];
// -- is issue IO --

/*
issues eg : 
issues = ['--zd_size', '--zd_font_size', '--size', '--size_'];
input : 
--zd_size
output : 
true
comment : 
do not execute when --zd_size comes as prop 
*/

exports.ignoreVals = ignoreVals;

function extractVariableName(val) {
  return val.replace(/calc\((.+)\)/gi, '$1').replace(/var\((.+)\)/gi, '$1').replace('-1', '').replace('*', '').replace('\n', '').trim();
}

function isIgnoreValuePresent(ignoreVals, prop) {
  return ignoreVals.some(ignoreVal => prop && prop.includes(ignoreVal)); // let present = false;
  // return ignoreVals.forEach(issue => {
  //   if (prop && prop.includes(issue)) {
  //     present = true;
  //   }
  // });
  // return present;
}

function parseCalcValue(calcValue) {
  // Remove "calc(" and ")" from the string
  const value = calcValue.replace(/calc\(/gi, '').replace(/\)/gi, '').trim(); // Split the string by "*" or "/"

  const parts = value.split(/[\\/*]/); // Parse the first part as a number

  const num1 = parts[0].trim(); // Parse the second part as a number

  const num2 = parts[1].trim();
  return {
    valOne: num1,
    valTwo: num2
  };
}

function convertToCalc(pxReplacement, value) {
  const parsedValue = parseFloat(value, 10);
  const sign = parsedValue < 0 ? '-' : '';
  const varName = `${parsedValue < 0 ? parsedValue * -1 : parsedValue}`;
  return `calc( ${pxReplacement.replace('$$', varName)} * ${sign}1 )`;
}

function convertCalcValue(pxReplacement, parsedValue) {
  Object.keys(parsedValue).forEach(key => {
    if (pxReplacement && parsedValue[key].includes('px')) {
      parsedValue[key] = pxReplacement.replace('$$', parsedValue[key].replace('px', ''));
    }
  });
  return parsedValue;
}

function variableConverter(rootOriginal, variables, settingsObject) {
  rootOriginal.walkRules(rule => {
    rule.nodes.forEach((decl, index) => {
      const prevNode = rule.nodes[index - 1];
      const currentNode = rule.nodes[index];

      if (decl.prop && decl.prop.includes('--')) {
        if ((0, _classHandling.isThisComment)(prevNode, 'variable:ignore') // prevNode &&
        // prevNode.type === 'comment' &&
        // prevNode.text.toLowerCase() === 'variable:ignore'
        ) {
          return;
        }

        if (isIgnoreValuePresent(ignoreVals, decl.prop)) {
          return;
        }

        if (settingsObject[variables[decl.prop]]) {
          /* if there is no value for property, set it to default so that undefined doesn't get called as key */
          if (!variables[decl.prop]) {
            variables[decl.prop] = 'default';
          }

          const pxReplacement = settingsObject[variables[decl.prop]].replacements.px;
          let valArr = decl.value.split(' ');
          valArr = valArr.filter(x => x.trim() !== '');
          valArr = valArr.map(x => x.replace(/\r|\t|\n/gi, '')); // single values are considered in the above array and converted below

          valArr.forEach((value, index) => {
            if (value.includes('px')) {
              if (value.includes('calc')) {
                const res = convertCalcValue(pxReplacement, parseCalcValue(value));
                valArr[index] = `calc( ${res.valOne.trim()} * ${res.valTwo.trim()} )`;
                return;
              }

              if (/-(\d+)/gi.test(value)) {
                valArr[index] = convertToCalc(pxReplacement, value);
                return;
              }

              const num = value.replace('px', '');

              if (value) {
                valArr[index] = pxReplacement.replace('$$', num);
              }
            }

            if (value.includes('px')) {
              const num = value.replace('px', '');
              valArr[index] = pxReplacement.replace('$$', num);
            }
          });
          currentNode.value = valArr.join(' ');
        }
      }
    });
  });
  return rootOriginal;
}