"use strict";

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

const diffBasedOnStrLength = (a, b) => b.length - a.length;
/**
 * @param {Array} valueReplacer 
 * [
      {
        "props": ["color", "background-color"],
        "values" : {
          "zd-": "im-"
        }
      }
    ]
 */


const PostcssValueReplacer = (valueReplacer = []) => {
  const options = {};
  valueReplacer.forEach(obj => {
    obj.props.forEach(prop => {
      const valueKeys = Object.keys(obj.values);
      const propOptions = options[prop];

      if (options[prop]) {
        valueKeys.forEach(k => {
          propOptions.values[k] = obj.values[k];
        });
        propOptions.sortedKeys = propOptions.values.sort(diffBasedOnStrLength);
      } else {
        options[prop] = {
          sortedKeys: valueKeys.sort(diffBasedOnStrLength),
          values: obj.values
        };
      }
    });
  }); // Work with options here

  return {
    postcssPlugin: 'postcss-value-replacer',
    Declaration: decl => {
      const propOptions = options[decl.prop];
      propOptions?.sortedKeys.forEach(k => {
        // eslint-disable-next-line no-param-reassign
        decl.value = decl.value.replaceAll(k, propOptions.values[k]);
      });
    }
  };
};

PostcssValueReplacer.postcss = true;
var _default = PostcssValueReplacer;
exports.default = _default;