"use strict";

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

var _os = _interopRequireDefault(require("os"));

var _path = _interopRequireDefault(require("path"));

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

var _getHash = _interopRequireDefault(require("./getHash"));

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

const isWindows = _os.default.platform().toLowerCase() === 'win32';

const isSelectorPackage = (resourcePath, packages) => {
  if (!resourcePath.includes('node_modules')) {
    return false;
  }

  let isValid = false;

  for (let pack of packages) {
    if (isWindows) {
      pack = pack.replace(/\//g, '\\');
    }

    if (resourcePath.includes(pack)) {
      isValid = true;
      break;
    }
  }

  return isValid;
};

function patternBasedClass({
  customClassNamePrefix,
  context,
  relativePath,
  localName
}) {
  let newName = null;
  customClassNamePrefix.forEach(obj => {
    if (obj.enable) {
      if (obj.patterns && obj.prefix && (0, _fileHandling.isFileNameMatchingPluginPattern)({
        filename: context.resourcePath,
        filterArr: obj.patterns
      })) {
        const h = (0, _getHash.default)(`${relativePath}-${localName}`, 10);
        newName = `${obj.prefix}${h}`;
      }
    }
  });
  return newName;
}

var _default = (unique = true, {
  filenames,
  packages
}, classNamePrefix, customClassNamePrefix, patterns) => (context, localIdentName, localName) => {
  // console.log(patterns, context.resourcePath);
  // NOTE:  in build machine we use date as folder path.
  // So every time we create new build there is path will alway different
  // in order to minimize that problem we try in relative path;
  // console.log('context.resourcePath', context.resourcePath, context);
  // let contextResourcePath = context.resourcePath;
  const filePaths = context.resourcePath.split(_path.default.sep);
  const fileName = filePaths[filePaths.length - 1];
  const [fileNameWithoutExt] = fileName.split('.');
  const cleanFileName = fileNameWithoutExt.replace(/-/g, '_').toLowerCase();

  const relativePath = _path.default.relative(context.rootContext, context.resourcePath); // console.log('customClassNamePrefix', customClassNamePrefix);

  /*
    input : 
    context.resourcePath : 'D:/MyWork/..../desk_client_app/supportapp/src/components/Avatar/Avatar.module.css,
  
      patterns.cssVariableReplacement: 
    // include src folder, include deskapp folder, exclude node modules
    "cssUniqueness": [
      "src",
      "deskapp",
      "!node_modules"
    ]
      output : 
    true or false
  */


  if (context.resourcePath.endsWith('.plain.css')) {
    return localName;
  }

  if (!(0, _fileHandling.isFileNameMatchingPluginPattern)({
    filename: context.resourcePath,
    filterArr: patterns.cssUniqueness
  })) {
    return `${classNamePrefix}-${cleanFileName}-${localName}`;
  }
  /* old production mode start without breaking so added. may be removed in future*/


  if (unique) {
    const h = (0, _getHash.default)(`${relativePath}-${localName}`, 10);
    return `${classNamePrefix}${h}`;
  }

  const patternClass = patternBasedClass({
    customClassNamePrefix,
    context,
    relativePath,
    localName
  });

  if (patternClass) {
    return patternClass;
  } //css file has case sensitive selector issue so can't toLowerCase
  //let local = localName.toLowerCase()


  if (isSelectorPackage(context.resourcePath, packages) || filenames.indexOf(cleanFileName) !== -1) {
    const h = (0, _getHash.default)(`${relativePath}-${localName}`, 10);
    return `${classNamePrefix}${h}`;
  }

  return `${classNamePrefix}-${cleanFileName}-${localName}`;
};

exports.default = _default;